08 / 11 · Modern cryptography
RSA
Easy to multiply two primes, astronomically hard to split the result.
- Published
- 1977, Rivest, Shamir & Adleman
- Hard problem
- Integer factorization
- Used in
- Certificates, signatures, SSH keys
RSA was the first practical public-key system: anyone can encrypt with your public key or check your signature, but only you, holding the private key, can decrypt or sign.
Its security rests on a simple asymmetry. Multiplying two large primes is instant. Given only their product, finding the primes again is, as far as anyone knows, infeasible.
no divisor up to √11 ≈ 3.3: prime
01 / 14 · Inside RSA
Two secret primes
RSA starts with two primes, numbers divisible only by 1 and themselves. Here p = 5 and q = 11; a real key uses two primes about 309 digits long.
Everything secret in RSA comes from these two numbers: whoever learns p or q can rebuild the private key.
The basics: Prime numbers →Playground
Build your own RSA key, use it, then break it
Pick two primes and a public exponent, encrypt a number, and step through every calculation on your values: n, φ(n), d by extended Euclid, square-and-multiply, decryption, and the attack that factors n.
Your input
Primes below 100 000 keep every step readable.
Must share no factor with φ(n).
- n
- 3233
- φ(n)
- 3120
- d
- 2753
- c
- 2790
Primes
Two secret primes
What happens
RSA starts from two primes, p and q: numbers divisible only by 1 and themselves. To check one, try every divisor up to its square root: a bigger divisor would pair with a smaller one already tried.
On your values
p = 61: ÷ 2, 3, 5, 7 → none divides it · primetried 4 divisors up to √61 ≈ 7.8
q = 53: ÷ 2, 3, 5, 7 → none divides it · primetried 4 divisors up to √53 ≈ 7.3
Why it matters
The formula φ(n) = (p − 1)(q − 1) that gives the private key only holds for primes. With a composite number, decryption breaks and n has more small factors for an attacker to find.
Start
← → to step · space to playPrimes
Break
Now race your browser against bigger keys
Your browser generates keys of growing size and factors each n by trial division. The times are measured, then extrapolated to real key sizes.
Trial division doubles in cost every 2 bits. Better algorithms exist (the number field sieve), but even with them the public record is RSA-250, 829 bits, and real keys use 2048 bits or more.
How it works
- 1Pick two large primes p and q, and compute n = p × q.
- 2Compute φ(n) = (p − 1)(q − 1).
- 3Choose a public exponent e coprime to φ(n), usually 65537, and compute the private exponent d = e⁻¹ mod φ(n).
- 4Encrypt: c = mᵉ mod n. Decrypt: m = cᵈ mod n. Publish (n, e) and keep d secret.
Why it holds
- Factoring is hard
- Computing d requires φ(n), which requires p and q. The best known method, the general number field sieve, grows faster than any polynomial in the size of n.
- Real-world records
- The largest RSA number factored publicly is RSA-250 (829 bits), in 2020, at a cost of about 2,700 CPU core-years. 2048-bit RSA is estimated to be billions of times harder.
- Padding
- Real RSA never encrypts raw numbers. OAEP padding adds randomness, so encrypting the same message twice gives different ciphertexts.
What can go wrong
- Textbook RSA is broken
- Without padding, RSA is deterministic and malleable. Small exponents, shared primes or bad randomness have broken real keys.
- Quantum computers
- Shor's algorithm factors in polynomial time on a large enough quantum computer. None exists yet, but data recorded today could be decrypted later, so migration to post-quantum signatures has started.