Thought Toys · Computation & information · Exhibit 44

Diffie–Hellman key exchange

Two people who have never met need a shared password, but the only channel between them is a megaphone in a crowded room. Everything they say, an eavesdropper hears. And yet they can still walk away knowing a number she can't — using nothing but a one-way arithmetic trick.

Public channel · everyone hears this prime p = 23  ·  generator g = 5

Alice

private
a = 6 (kept secret)
sends A = ga mod p = 8

Bob

private
b = 15 (kept secret)
sends B = gb mod p = 19
Both compute the identical secret Alice: Ba mod p  =  Bob: Ab mod p  =  2
Alice & Bob~10 mults
Eve, guessing≤22 tries

Eve's bar counts the simplest attack — trying every exponent. Cleverer methods exist and are what really set key sizes, but even the best known ones still explode past reach at real primes.

your turn — drag p up and watch Eve's workload explode while Alice's barely moves

What you're seeing

Alice and Bob first agree, out loud, on two public numbers: a prime p and a base g. Then each picks a private number — Alice's a, Bob's b — and tells no one. Each publishes only g raised to their secret, wrapped around p: Alice shouts A, Bob shouts B. Eve hears p, g, A and B — everything.

Now the magic. Alice takes Bob's public B and raises it to her secret a; Bob takes A and raises it to his secret b. Both are really computing g to the power a×bthe same number, reached from two directions — so they land on an identical secret without either exponent ever crossing the room.

Eve is stuck. To copy the trick she'd need one of the private exponents, which means undoing "g to the what? gives A" — the discrete logarithm. For the toy primes here she can just try every power (drag p to 23 and her whole search is a couple dozen guesses). The bars below count that simplest attack. Cleverer methods than brute force exist — they're what actually decides how big p has to be — yet even the best known ones still blow up fast enough that at the ~2048-bit primes real systems use, no attack comes close to finishing. Meanwhile Alice's and Bob's work barely moves. Drag p up and watch the two bars split apart: that widening gap is the security.

The rule, exactly. Public: prime p, generator g (a primitive root, so its powers cycle through every nonzero residue). Alice sends A = ga mod p,   B = gb mod p and the shared secret is Ba = Ab = gab mod p. Verified in node (improve/verify/44-diffie.js): across 400 random exchanges over ten primes both sides land on the identical value and it always equals gab mod p; the eavesdropper's brute-force discrete log recovers the secret for every toy prime; and the work ratio Eve/Alice climbs from ~3× at p = 23 to ~768× at p = 12289 — the one-way gap widening on cue. Negative controls: a base that isn't a primitive root only reaches a small subgroup of residues (why g must be a generator), and the public product A·B mod p — which equals ga+b — is not the secret gab, confirming no public combination of the overheard numbers yields the key; you genuinely need a private exponent.

Also in Computation & information: Freeze too fast, stay stuck →

All 20 in Computation & information
  1. 101Noise sets a speed limit, not an accuracy limit.
  2. 111Any party of six hides a trio
  3. 120How one bit of ink makes a grey
  4. 125The ambulance that arrives sooner by going slower
  5. 131Every number is a sum of Fibonacci numbers
  6. 23Sorting algorithms
  7. 24PageRank & the random surfer
  8. 25Huffman coding
  9. 26Dijkstra's shortest path
  10. 33The learning-rate cliff
  11. 36A* pathfinding
  12. 44Diffie–Hellman key exchange — you are here
  13. 49Freeze too fast, stay stuck
  14. 56Catch one error, miss the next
  15. 57Why more processors stop helping
  16. 58Why a busy line explodes
  17. 59The set that's only sure when it says no
  18. 60The fit that memorizes instead of learns
  19. 61When the wire breaks, pick one
  20. 82Your computer can't hold one tenth

← the cabinet · Thought Toys — a cabinet of explorable explanations. Exhibit 44.