RSA (Rivest, Shamir and Adleman, 1977) lets anyone encrypt a message to you with a public key, while only you can decrypt it. It is Euler's theorem plus modular inverses plus one fact: multiplying primes is easy, but factoring their product is hard.
- Setup. Choose primes $p \ne q$. Let $n = pq$ and $\varphi(n) = (p - 1)(q - 1)$.
- Choose $e$ with $\gcd(e, \varphi(n)) = 1$, and compute $d \equiv e^{-1} \pmod{\varphi(n)}$.
- Public key: $(n, e)$. Private key: $d$ (and keep $p$, $q$, $\varphi(n)$ secret).
- Encrypt a message $0 \le m < n$: $c = m^{e} \bmod n$. Decrypt: $m = c^{d} \bmod n$.
Why it works
Worked example: Making keys
- $n = 11 \cdot 17 = 187$ and $\varphi(n) = 10 \cdot 16 = 160$.
- $\gcd(7, 160) = 1$. Extended Euclid: $160 = 7 \cdot 22 + 6$, $7 = 6 + 1$, so $1 = 7 - 6 = 7 - (160 - 22 \cdot 7) = 23 \cdot 7 - 160$.
- So $d = 23$. Check: $7 \cdot 23 = 161 = 160 + 1$.
Answer: Public $(187, 7)$, private $d = 23$.
Worked example: Encrypting and decrypting
- $42^{2} = 1764 \equiv 1764 - 9 \cdot 187 = 81$, $\ 42^{4} \equiv 81^{2} = 6561 \equiv 16$.
- $42^{7} = 42^{4} \cdot 42^{2} \cdot 42 \equiv 16 \cdot 81 \cdot 42$: $16 \cdot 81 = 1296 \equiv 174$, then $174 \cdot 42 = 7308 \equiv 15$. So $c = 15$.
- Decrypt: $15^{23} \bmod 187$ by repeated squaring ($23 = 16 + 4 + 2 + 1$) gives $42$, the original message.
Answer: $c = 15$, and $15^{23} \equiv 42$.
Worked example: Breaking a tiny key
- Trial division up to $\sqrt{187} \approx 13.7$: $187 = 11 \cdot 17$.
- Then $\varphi(187) = 10 \cdot 16 = 160$, and $d = 7^{-1} \bmod 160 = 23$, exactly Alice's private key.
- For a 600-digit $n$, the best known factoring methods would run for far longer than the age of the universe on ordinary computers. That gap is the whole security of RSA.
Answer: $d = 23$, once $n$ is factored.
Common mistake
Key idea