RSA¶
Generally, data transforming from senders to receivers need to be encrypted. A common way is as follows:
- Senders encrypt plain text with a key K.
- Receivers decrypt the cipher text with the same key K.
It's called Symmetric Cryptography Algorithm. The question is, however, how to store and pass the key in safety.
Then comes another algorithm called Asymmetric Cryptography Algorithm, without key transformation.
- Receivers generate two keys: one is public and another is private.
- Senders get the public one to encrypt the information.
- Receivers use the private key to decrypt the data.
RSA Algorithm is one of asymmetric cryptography algorithms. Below is the basic principle of RSA.
Preparation¶
Coprime¶
First to know is a concept called coprime. It's easy to draw conclusions as follows:
- Any two prime numbers are coprime.
- If the smaller one is prime, the two numbers are coprime as long as the greater ones isn't a multiple of the smaller one.
- If the greater one is prime, the two numbers are coprime.
- Any number and 1 are coprime.
- Any number \(p\) and \(p-1\) are coprime.
- If \(p\) is odd, \(p\) and \(p-2\) are coprime.
Euler's totient function¶
Then comes a question: given any positive integer \(n\), how many integers \(k\) in the range \(1\le{k}\lt{n}\) are relatively prime to \(n\)?
The counting is called Euler's totient function, expressed as \(\varphi(n)\). Steps below are to calculate it.
- If \(n=1\), \(\varphi(n)=1\).
- If \(n\) is prime, \(\varphi(n)=n-1\).
-
If \(n=p^k\) and \(p\) is prime, then
\[ \begin{equation} \varphi(n)=\varphi(p^k)=p^k-p^{k-1}=p^k(1-\frac{1}{p}) \end{equation} \] -
\(\varphi(n)\) is a multiplicative function. That's to say If \(n=p_1·p_2\) and \(p_1\) is coprime to \(p_2\), then
\[ \begin{equation} \varphi(n)=\varphi(p_1·p_2)=\varphi(p_1)·\varphi(p_2) \end{equation} \] -
The fundamental theorem of arithmetic states that if \(n>1\) there is a unique expression, \(n=p_1^{k_1}·p_2^{k_2}···p_r^{k_r}\), combined with the two formulas above, then
\[ \begin{equation} \varphi(n)=n(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_r}) \end{equation} \]
Carmichael function¶
Given a positive integer \(n\), Carmichael function \(\lambda(n)\) is defined as the smallest positive integer \(m\) such that
Besides, \(n\) can by written in a unique way as
Then \(\lambda (n)\) is the least common multiple of the Carmichael function of each of its prime power factors, proved by Chinese Remainder Theorem:
and as given above,
RSA Algorithm¶
Key Generation¶
Assume Zoe wants to communicate with EZ, then how do Zoe generate the public and private keys?
-
Choose two distinct prime numbers \(p\) and \(q\).
- For security purposes, the integers \(p\) and \(q\) should be chosen at random, and should be similar in magnitude but differ in length by a few digits to make factoring harder. Prime integers can be efficiently found using a primality test.
- \(p\) and \(q\) are kept secret.
- Assume that Zoe chooses 61 and 53.
-
Compute \(n = pq\).
- \(n\) is used as the modulus for both the public and private keys. Its length, usually expressed in bits, is the key length.
- \(n\) is released as part of the public key.
- Zoe computes that \(n=61 \times 53=3233\).
-
Compute \(\lambda(n)\).
\[ \begin{equation} \lambda (n) = \operatorname {lcm} (\lambda (p), \lambda (q)) = \operatorname {lcm} (p-1, q-1) \end{equation} \]Zoe computes that \(\lambda (n)=\operatorname{lcm}(60, 52)=780\).
-
Choose an integer \(e\) such that \(1<e<\lambda (n)\) and \(e\) is coprime to \(\lambda (n)\).
- Shorter bit-length and smaller Hamming weight \(e\) has, more efficient encryption is.
- The most commonly chosen value is \(2^{16}+1=65537\).
- \(e\) is released as part of the public key.
- Let \(e = 17\).
-
Determine \(d\), the modular multiplicative inverse of \(e\) modulo \(\lambda (n)\).
- That means \(e·d \equiv1\pmod{\lambda (n)}\). It's computed efficiently by Extended Euclidean algorithm.
- \(d\) is kept secret as the private key exponent.
- By solving the equation \(e·d+k\lambda(n)=1\), Zoe gets \(d=413\).
Now, we get a public key \((n, e)\) and a private key \((n, d)\). For Zoe, they are \((3233, 17)\) and \((3233, 413)\).
Encryption and Decryption¶
For a plaintext message \(m\), the encryption function is
For an encrypted criphertext \(c\), the decryption function is
Assume that Zoe wants to encrypt \(m = 65\), then
EZ gets c and decrypts it to calculate
These calculations can be computed efficiently by Exponentiation by squaring.