-
Modulus operation
mod
Take over operations%
-
The original root:
- A primal root is a mathematical symbol. Let’s say
m
Is a positive integer,a
Is an integer, ifa
diem
The order is equal to thePhi (m)
Is said,a
For the mouldm
A primitive root of(includingPhi (m)
saidm
Euler function of) - Let’s say I have a number
g
isP
The primal root of thetag^i mod P
The results are different, and there are1<g<P
.0<i<P
What it comes down to isg^(P-1) = 1 (mod P)
If and only if the exponent isP-1
Is true when(hereP
Is a prime number) - In short,
G ^ I mod p ≠ g^j mod p
(p
For prime), includingI indicates j
andi, j
between1
to(p-1)
Between,g
forp
The original root - Ex. :
3^x mod 17 = y
At this time,3 < 17
and17
Is prime, thenx
The value of is1 ~ 16
When, as a result,y
And the range of1 ~ 16
andx ! = y
.3
That is17
The original root
- A primal root is a mathematical symbol. Let’s say
-
The euler function
phi
- Coprime: If two positive integers, except
1
If there are no other common divisor, then the two numbers are mutually prime - Find less than or equal to
n
And with then
A function of the number of mutualprimes, called the Euler function, expressed asPhi (n)
- For example,
Phi (8) = 4
, i.e.,1 hc-positie
.Phi (7) = 6
, i.e.,6
- Coprime: If two positive integers, except
-
Euler function characteristics
- when
n
When it’s prime,φ n is equal to n minus 1
- if
n
Can be decomposed into the product of two mutually prime integers, such asn = A * B
, then:φ A times B is equal to φ A times φ B.
- According to the above two points: if
N
It’s two prime numbersP1
andP2
Product of, thenPhi of N is equal to phi of P1 times phi of P2 is equal to phi of P1 minus phi of P2 minus phi.
- when
-
Euler’s theorem
- If you have two positive integers
m
andn
Each other is prime. Som
theφ n to the power minus 1
Can ben
Divisible, that is,M ^φ(n) mod n ≡ 1
- Fermat’s little theorem: a special case of Euler’s theorem if two positive integers
m
andn
Each other is prime, andn
It’s also a prime number, soPhi (n)
As a result ofn - 1
, i.e.,M to the n minus 1 mod n ≡ 1
- If you have two positive integers
-
Formula translation
∵ ≡ 1 * 1 m ≡ 1 ^ k m ∴ m ^ phi mod n (n) ≡ 1 ∴ phi m ^ k * (n) ≡ 1 ∴ mod n phi m ^ k * (n + 1) mod n = m (equal on both sides are multiplied by the m)Copy the code
-
Die inverse element
- If you have two positive integers
e
andx
Mutual prime, then you must find integersd
,ed - 1
bex
Divisible. Sod
ise
forx
The modulo inverse element of.
- If you have two positive integers
E times d mod x ≡ 1 e times d is kx plus 1 (Ed is a multiple of x plus 1) m to the Ed mod n ≡ mCopy the code
- Finally 'm ^ Ed mod n = m' (see 6. Formula translation, where ` e ` and ` phi (n), ` co-prime ` d ` is ` e ` relative to ` phi (n) ` module of the element) - features: as long as ` d ` is ` e ` relative to ` phi (n) ` mode against the elements, then ` m ` and ` n ` relationship can not co-prime, as long as ` m < n ` equation can be established.Copy the code
-
Diffie Hermann key exchange
M ^ Ed mod n ≡ m
The formula continues to be deduced (D is the modulo inverse of e with respect to φ(n)
)- –>
m ^ e mod n = C
The encryption,e&n
Is the public key - –>
C ^ d mod n = m
Namely,d&n
Is the private key - In the same way
m ^ d mod n = C C ^ e mod n = m
, i.e.,e & d
It can be encrypted or decrypted.
-
Terminal Experience RSA
openssl genrsa -out private.pem 1024
Example Generate an RSA private key of 1024 bitsopenssl rsa -in private.pem -pubout -out public.pem
Extract the public key from the private keyopenssl rsa -in private.pem -text -out private.txt
Turn the private key into plain text for viewingopenssl rsautl -encrypt -in msg.txt -inkey public.pem -pubin -out encrypt.txt
Use the public key to encrypt the msg.txt file to enctypt.txtopenssl rsautl -decrypt -in encrypt.txt -inkey private.pem -out decrypt.txt
Decrypt encrypt. TXT to decrypt.txt using the private key
-
Generate a certificate
openssl req -new -key private.pem -out rsacert.csr
Requesting a CSR Certificateopenssl x509 -req -days 3650 -in rsacert.csr -signkey private.pem -out rsacert.crt
Use the CSR to request the CRT certificateopenssl pkcs12 -export -out p.p12 -inkey private.pem -in rsacert.crt
Exporting a P12 Certificateopenssl x509 -outform der -in rsacert.crt -out rsacert.der
Exporting the DER CertificateiOS
Code implementationRSA
Encryption and decryption,Der certificate
Is the private key,p12
Is the public key