The Secret You Can Say Out Loud
Two people who have never met, who share no secret, need to agree on one: a private key for a locked conversation. And they have to do it out loud, in a room where you are standing, writing down every word. It sounds impossible, because whatever they say to reach a shared secret, you hear it too. Yet it can be done, and the whole of secure internet traffic depends on it. Below is a real, working version of the trick (Diffie-Hellman, 1976), shrunk to toy size so you can watch the secret form, try to steal it, and meet the one attack it does not stop.
The move rests on a single lopsided piece of arithmetic, something easy to do forwards and brutally hard to undo. Raising a number to a power modulo a prime is fast; working backwards from the answer to the power, the discrete logarithm, has no known shortcut. Alice and Bob each do the easy direction once; an eavesdropper is left facing the hard one. Don't take that on faith. Build the secret, then try to break it yourself and watch the difficulty climb.
1. Build a shared secret, in public
Everyone agrees on two public numbers: a prime
p and a base g. Alice picks a secret
number a she tells no one; Bob picks his own secret b. Each
sends the other g raised to their secret, mod p, and those two
values travel across the open channel, in full view. Then each raises what they
received to their own secret. Watch what comes out.
Alice
sends A = ga mod p = …
Bob
sends B = gb mod p = …
B = … → travels to Alice
p of 2048 to 3072 bits (600+ decimal digits), or its elliptic-curve
cousin. Nothing here is secure; that is the point. You are meant to see the machinery,
not hide behind it.Both sides landed on the same number, by different routes: Alice raised Bob's value to
a, Bob raised Alice's to b, and both are really
gab mod p, the exponents multiplied, in whichever order. That
shared number is the secret key. Eve heard p, g,
A = ga and B = gb, every one of them, and
to reach gab she would need one of the private exponents. She
would have to run a power backwards.
2. Now try to steal it
You are Eve. You have the current p, g, A, B above and nothing
else. To recover the secret you must solve a discrete logarithm: find
the exponent a with ga = A (mod p). For a toy prime
you can. The fastest simple method is baby-step giant-step, which does about
√p steps instead of p. Here it is, running for real:
press “Recover the secret”
It falls in a blink, because √p for a toy prime is tiny. The security is
entirely in the size of that square root. Double the bit-length of p and you
square p, so √p, the work, itself doubles in
bit-length. The table is honest about how fast the wall becomes unclimbable, and about a
sharper attack than the one above:
3. The attack it does not stop
Here is what the paint-mixing pictures never tell you. Diffie-Hellman defeats a passive listener, someone who only reads the wire. It does nothing against an active attacker who can also change what crosses it. Meet Mallory, sitting in the middle. She lets no message through untouched: to Bob she pretends to be Alice, to Alice she pretends to be Bob, and runs two handshakes at once.
Alice, thinks she shares a key with Bob
key she holds = …
Bob, thinks he shares a key with Alice
key he holds = …
With Mallory off, the message is just a locked number on the wire and she learns
nothing. Switch her on and the two green “shared” keys quietly become different
numbers, one she shares with Alice, one she shares with Bob, and she reads and relays
everything, invisible to both. Diffie-Hellman handed each of them a private line to
someone. It never promised that someone was who they thought. That missing
guarantee is authentication, proof of who is on the other end,
and it is a separate job. It is the reason the exchange in a real secure connection is
signed: the server proves its identity with a certificate, so a forged
gb is caught. Secrecy without authentication is exactly this
trap.
The check, shown not asserted
Every number on this page is produced by from-scratch modular arithmetic (square-and-multiply exponentiation, a modular inverse by the extended Euclidean algorithm, baby-step giant-step for the attack) running in your browser, no crypto library. Here it reproduces the canonical textbook example, live, right now:
Those are exactly the numbers in the standard worked example (p = 23, g = 5, a = 4, b = 3 → shared secret 18). The reproducible verifier goes further. It confirms the handshake identity Ba ≡ Ab ≡ gab (mod p) holds in 4,000 of 4,000 random exchanges, that baby-step giant-step recovers the secret exponent exactly in 400 of 400 toy exchanges (and its step count tracks √p), and that a man in the middle ends up holding both parties' keys in 2,000 of 2,000, no number ever wrong, the protocol simply blind to who.
Reproduce it all from a clean checkout: node research/diffie-hellman/verify.mjs.
Four things worth getting straight
The secret is the number, not the message.
Diffie-Hellman does not encrypt anything. Its one job is the handshake: letting two parties agree on a shared random key over an open wire. What they do with that key afterwards is a separate, faster cipher (like AES) for the actual traffic. This is the problem passwords and symmetric ciphers cannot solve on their own: how do two strangers agree on a key without first having a key to protect it? That was the 1976 breakthrough.
The public values are meant to be public.
Handing out p, g, A and B gives an
eavesdropper no shortcut, because recovering a secret from A = ga
is the discrete logarithm, and for a well-chosen 2048-bit prime no one on Earth can do it
in reasonable time. So Alice can shout A across the room. The whole security
rests on a belief that the discrete log is hard, a belief no one has overturned,
not a theorem anyone has proved. Honesty about assumed-versus-proved is the whole game.
Secrecy is not authentication.
As instrument 3 shows, an active attacker who can rewrite the channel breaks plain Diffie-Hellman completely, not by solving any hard problem, but by being the person on the other end of two separate handshakes. The exchange gives you a private line; it does not tell you who you are talking to. Every real deployment fixes this by authenticating the exchanged values with signatures or certificates. The 512-bit “export” Diffie-Hellman that the 2015 Logjam attack tore open combined both weaknesses at once: a breakable prime and a downgrade an active attacker could force.
There are inventors you were not told about.
Whitfield Diffie and Martin Hellman published this in 1976, and Hellman himself has asked that it be called Diffie–Hellman–Merkle, for Ralph Merkle's parallel work on the same idea. But it was discovered earlier, in secret: at Britain's GCHQ, James Ellis conceived “non-secret encryption” in 1969, Clifford Cocks devised the equivalent of RSA in 1973, and Malcolm Williamson worked out this very key exchange in 1974, all classified, and unknown to the world until GCHQ declassified the history in 1997. The public inventors were genuinely first to publish; they simply were not first.