Lesson №11 1976–1977 25 min
Public keys: Diffie–Hellman and RSA
How do you agree on a secret with a stranger when someone is listening from the very first word? For three thousand years it seemed impossible. In 1976 it turned out it wasn’t.
In this lesson
- what the key distribution problem is
- how Diffie–Hellman agree on a secret in front of witnesses
- how RSA works: p, q, n, φ, e, d on small numbers
- why multiplying is easy and factoring is hard
- what a digital signature is
The Cold War. A man walks down an airport corridor with a briefcase chained to his wrist. Nothing in it is secret in itself: pages of random digits, next month’s keys for an embassy’s cipher machines. But without that briefcase the embassy falls silent. Couriers carried keys around the world by the thousand.
Every cipher we’ve met so far, from Caesar to the one-time pad and AES, works the same way: sender and receiver must hold the same key. Such ciphers are called symmetric. And they all share one headache: before you can encrypt, the key has to get there somehow. Secretly. And to send something secretly you need… a cipher with a key.
A vicious circle
This is the key distribution problem. For two people one meeting solves it. But picture a network where everyone must talk to everyone in private. Ten people need 45 different keys; a thousand need 499,500; a billion internet users would need about half a quintillion. And every one of those keys has to be delivered by hand.
Now picture yourself visiting your bank’s website for the first time. You’ve never met the bank, no courier has called, and your whole conversation runs from the first byte over other people’s wires: café Wi-Fi, your provider, a dozen internet nodes. How do you agree on a secret key when strangers hear every word?
Here’s a thought experiment that suggests it isn’t hopeless. Alice puts a letter in a box, snaps her padlock on it and sends it to Bob. Bob can’t open it, but he adds his own padlock and sends it back. Alice removes her padlock and sends it again. Now only Bob’s padlock is on the box: he opens it and reads. The box passed through the postman’s hands three times and was never open. And nobody exchanged keys!
With real ciphers this trick doesn’t always work. If the “padlock” is XOR with a key, the postman XORs the three boxes he intercepted and gets the letter: the keys cancel out, as in the two-time pad of the XOR lesson. You need a cleverer operation.
Diffie–Hellman: mixing paints
In 1976 Whitfield Diffie and Martin Hellman of Stanford published a paper called “New Directions in Cryptography”. In it was a way for two people to agree on a shared secret while talking only in front of witnesses. Their colleague Ralph Merkle came up with an important part of the ideas.
It’s easiest to understand with paint. Mixing paint has just the property we need: mixing is easy, while separating a mix back into its colours is practically impossible.
- Alice and Bob openly agree on a common paint, say yellow. Everyone sees it.
- Each picks a secret paint and shows it to no one.
- Each mixes the common paint with their secret one and sends the mix to the other, in the open.
- Each adds their own secret paint to the mix they received.
Now both have the same colour: common + Alice’s secret + Bob’s secret, only mixed in a different order. Eve, eavesdropping, saw the yellow and both mixes. Mixing those together won’t do: her pot has two portions of yellow. And she can’t extract a secret out of a mix.
Diffie and Hellman actually mixed numbers, not paint. The common “paint” is two public numbers: a large prime p and a number g. Alice’s secret is a number a. To “mix” is to compute A = ga mod p: raise g to the power a and take the remainder after dividing by p. We met remainders in the Caesar cipher: the same counting “round the clock”.
Bob receives A and raises it to his power b: Ab = gab. Alice does the same with B = gb: Ba = gba. It’s the same number, the shared secret. To get it, Eve must find a from A = ga mod p. This is called the discrete logarithm problem, and for primes hundreds of digits long nobody knows how to solve it in less than astronomical time.
Alice and Bob agreed in public on p = 23 and g = 5. Alice sent the number 10, Bob sent 2. You heard it all. What is their shared secret?
Find Alice’s secret a: which power of 5 leaves remainder 10 when divided by 23? The numbers are small, so just try: 5, 25 mod 23 = 2, 2 · 5 = 10, …
a = 3. Now do what Alice would do: raise Bob’s number 2 to the power 3, modulo 23.
Powers of 5 modulo 23: 5, 2, 10: the third. So a = 3. Then 23 = 8, and 8 mod 23 = 8. The shared secret is 8. Check it from the other side: Bob’s secret is b = 2 (52 = 25, which leaves 2), and 102 = 100 = 4 · 23 + 8. Also 8.
With p = 23 Eve succeeds by trial in a minute. Real systems use a p of 2048 bits, more than 600 digits, and trying powers one by one becomes hopeless.
A letterbox with a slot: RSA
Diffie and Hellman described one more idea but couldn’t find a way to build it. Let everyone have two keys: one public, one private. The public one encrypts, the private one decrypts. Like a letterbox with a slot: any passer-by can drop a letter in, but only the owner of the key can take letters out. You could print the public key in a newspaper: it can’t read anything.
A year later, in 1977, three researchers at MIT built such a letterbox: Ron Rivest, Adi Shamir and Leonard Adleman. The cipher is named after their initials: RSA. Here’s the recipe:
- Pick two large primes p and q. They are the core secret.
- Multiply them: n = p · q. This number is public.
- Compute φ(n) = (p − 1)(q − 1), Euler’s function. It’s secret again: without p and q you can’t get it.
- Choose e, a number sharing no factor with φ(n). The pair (n, e) is the public key.
- Find d such that e · d leaves remainder 1 when divided by φ(n). That’s the private key.
To encrypt: the message is a number m (smaller than n), and the cipher is c = me mod n. To decrypt: m = cd mod n. Raising to e and to d are two turns of a key that undo each other.
Why cd gives back m
Since e · d = 1 + k · φ(n), we get cd = me·d = m · (mφ(n))k. By Euler’s theorem, if m shares no factor with n, then mφ(n) leaves remainder 1 when divided by n. So (mφ(n))k leaves 1 too, and m is what remains. (For m divisible by p or q the equality also holds; the proof goes through the Chinese remainder theorem.)
So where’s the protection? To find d you need φ(n), and for that you need p and q. Eve knows n. So she has to factor n. For n = 143 that takes a minute: 143 = 11 · 13. For a 600-digit n, nobody knows how to do it within the lifetime of the universe.
The safe’s public key is n = 187, e = 3. Each letter of the password (A = 1, B = 2, … Z = 26) was encrypted on its own:
45 9 27 22
What’s the password? Work it out in the workbench above or in the RSA tool.
Factor 187 into two primes. Try dividing by 7, 11, 13…
187 = 11 · 17, so φ = 10 · 16 = 160. You need a d for which 3 · d leaves remainder 1 when divided by 160. Try 107.
187 = 11 · 17, so φ(n) = 10 · 16 = 160. Look for d: 3 · 107 = 321 = 2 · 160 + 1, so d = 107. Now raise each number to the 107th power modulo 187: 45 → 12, 9 → 15, 27 → 3, 22 → 11. The numbers 12, 15, 3, 11 are L, O, C, K. The password is lock.
Notice that encrypting one letter at a time is a bad idea: equal letters would give equal numbers, and the cipher would turn into a simple substitution. Real RSA encrypts big numbers with random padding.
Easy to multiply, hard to factor
Multiplying two hundred-digit numbers takes a few minutes with a pencil and a microsecond on a computer. The reverse, finding the factors from the product, is another matter. You can try divisors, you can use clever methods, but for large numbers every known approach takes unimaginably long. A task that’s easy one way and hard the other is called a one-way function; RSA also has a “trapdoor”, the knowledge of p and q, which makes the way back easy.
In August 1977 Martin Gardner described RSA in Scientific American and printed a message encrypted with a 129-digit number, offering $100 for the solution. The authors reckoned it would take an unimaginably long time. The answer came in 1994: some 600 volunteers around the world split the work among their computers. The message read: “The Magic Words are Squeamish Ossifrage”.
Computers and algorithms have grown stronger since. In 2020 RSA-250, a 250-digit number, was factored; it took about 2,700 years of computer time. That’s why RSA keys today use an n of 2048 bits (617 digits) or more. The only known threat to that length is a large quantum computer: in 1994 Peter Shor found a fast factoring algorithm for one. No such machine exists yet, but people are already preparing: in 2024 the US approved its first post-quantum cryptography standards.
Signatures
A key pair has a second use, just as important. If you “encrypt” a message with the private key, s = md mod n, then anyone can “decrypt” it with the public one, se mod n. That gives no secrecy, but it proves authorship: only the owner of d could have produced that s. That’s a digital signature. In practice you sign not the message itself but its hash from the last lesson, which is why the hash must be free of collisions.
You want people to be able to send you secret letters. What do you publish on your website?
The public key (n, e) only lets people encrypt. Only someone who knows d can decrypt, and d is computed from p and q, which you keep to yourself.
In real life the two inventions work together. When your browser opens a site over HTTPS, it uses a Diffie–Hellman exchange (usually the elliptic-curve version) to agree on a secret key with the server, and the server’s signature proves it really is your bank and not an impostor. After that the data is encrypted with a fast symmetric cipher such as AES. All of this in a fraction of a second, in front of witnesses, with not a single courier.
Public-key cryptography rests on one-way functions: raising to a power modulo p is easy, finding the power from the result is hard; multiplying is easy, factoring is hard. That’s why one key can be handed to everybody while the other stays with you.
As it turned out, the Americans weren’t first. At the British intelligence agency GCHQ, James Ellis came up with “non-secret encryption” as early as 1970; in 1973 Clifford Cocks found a scheme essentially the same as RSA, and in 1974 Malcolm Williamson found the equivalent of Diffie–Hellman. Their work was classified and only became public in 1997.
Why can’t Eve, who heard the whole Diffie–Hellman exchange, simply mix the two public mixes?
The shared secret is the common paint plus both secrets, one portion each. Eve can only get common + Alice’s secret + common + Bob’s secret. Removing the extra portion, that is finding a secret from a mix, is exactly the hard problem.
What to take with you
- Symmetric ciphers need a shared key, and it has to be delivered secretly. That’s the key distribution problem.
- Diffie–Hellman (1976): two people agree on a secret in the open. Mixing (powers modulo p) is easy; unmixing (the discrete logarithm) is hard.
- RSA (1977): n = p · q, φ = (p − 1)(q − 1), e is public, d is such that e · d leaves remainder 1 modulo φ. Cipher c = me mod n, decryption m = cd mod n.
- RSA holds as long as n can’t be factored. At 617 digits that’s beyond any computer that exists.
- A signature is the same thing in reverse: sign with the private key, check with the public one.
You now hold the codebreaker’s whole toolkit, from counting letters to factoring numbers. All that’s left is to see it at work on real cases, from a plot against a queen to the ciphers of a serial killer. Next: famous ciphers →
Tools for this lesson
Everything from the lesson, working: paste your own text.