← back to cryptography

RSA

Wikipedia · wpRSA (cryptosystem) · CC BY-SA 4.0

RSA is an asymmetric cipher: the encryption key (public) and decryption key (private) are different. Anyone can encrypt a message with the public key, but only the private key holder can decrypt it. Security rests on the difficulty of factoring the product of two large primes. The math works because of Euler's theorem: med mod n = m when ed = 1 mod phi(n).

plain m^e mod n public key (e, n) cipher c^d mod n private key (d) plain Anyone encrypts with (e, n). Only the holder of d decrypts.

Key generation

Pick two large primes p and q. Compute n = pq and phi(n) = (p-1)(q-1). Pick e coprime to phi(n) (commonly 65537). Compute d = e-1 mod phi(n) using the extended Euclidean algorithm. Public key: (e, n). Private key: d. Discard p, q, phi(n).

Scheme

Encrypt and decrypt

To encrypt message m: compute c = me mod n. To decrypt ciphertext c: compute m = cd mod n. This works because med mod n = m (by wpEuler's theorem, since ed = 1 mod phi(n)).

Scheme

Why it works: Euler's theorem

Euler's theorem states: if gcd(m, n) = 1, then mphi(n) mod n = 1. Since ed = 1 mod phi(n), we can write ed = 1 + k*phi(n) for some integer k. Then med = m1 + k*phi(n) = m * (mphi(n))k = m * 1k = m (mod n). The security relies on the fact that computing phi(n) requires factoring n = pq, which is hard for large primes.

Scheme
Neighbors

Cross-references

Foundations (Wikipedia)