The Key Distribution Problem and Diffie-Hellman
See why shared keys don't scale, work through Diffie-Hellman key exchange with small numbers, and learn why man-in-the-middle attacks need authentication.
Welcome back!
Step 1 of 13Learn
Strangers who need a secret
Symmetric encryption is fast and strong, but it needs both sides to share a key first. Think about what that means at internet scale.
If every pair of people in a group needs its own private key, a group of n people needs n(n − 1) ÷ 2 keys. Ten people need 45 keys. A thousand people need 499,500. And each key must be delivered through some secure channel in advance. Your browser, meanwhile, wants a secure connection to a shopping site it has never contacted before, run by a company you've never met, over a network full of strangers. There's no courier and no prior secret.
For most of history, this key distribution problem seemed fundamental: secure communication required a secure channel first. Then, in 1976, Whitfield Diffie and Martin Hellman published New Directions in Cryptography, describing how two parties could agree on a shared secret over a completely public channel, with an eavesdropper watching every message, and still end up with a key the eavesdropper can't compute. (It later emerged that researchers at the UK's GCHQ had discovered similar ideas a few years earlier, in secret.)
This was the birth of public-key cryptography, also called asymmetric cryptography. It underpins HTTPS, secure messaging, software updates, cryptocurrencies and nearly every secure connection between strangers today.
Step 2 of 13Practice · Work it out
A company of 50 employees wants every pair of employees to have its own symmetric key. How many keys are needed?
Step 3 of 13Explore
Keys needed when every pair shares a secret
The curve shows n(n − 1)/2: the number of symmetric keys for n people. Move the slider to read the count for a group size.
- Keys for n people
- 4,950
- With public keys (one pair each)
- 100
Step 4 of 13Learn
One-way functions and modular arithmetic
Public-key cryptography relies on one-way functions: operations that are easy to compute in one direction but practically impossible to reverse. Some have a secret trapdoor that makes reversing easy for whoever knows it.
The key example uses modular exponentiation. Recall that x mod p is the remainder after dividing x by p. Computing ga mod p is fast even for numbers hundreds of digits long, thanks to a trick called repeated squaring. But going backward, given g, p and the result, find a, is the discrete logarithm problem, and for large, well-chosen p, no efficient method is known.
A small example with p = 23 and g = 5:
- 51 mod 23 = 5
- 52 = 25, mod 23 = 2
- 53 = 125, mod 23 = 10 (since 23 × 5 = 115)
- 54 = 625, mod 23 = 4 (since 23 × 27 = 621)
The results jump around with no obvious pattern. With p this small, you could recover the exponent by trying all values. With p around 2048 bits, more than 600 decimal digits, that's hopeless.
A helpful rule for working by hand: you can reduce mod p at every step. To get 54 mod 23, square 52 mod 23 = 2 to get 4. You never need to handle the full number 625.
Step 5 of 13Practice · Diagram
Powers of 3 mod 7 go around a cycle
Computing forward is easy. Going backward (which power gave 5?) means walking the cycle. With 600-digit numbers, nobody can.
Which exponent gives 3k mod 7 = 5? (This is the discrete logarithm.)
Tap the shape that answers the question, then press Check.
- 3^1 → 3
- 3^2 → 2
9 mod 7 = 2
- 3^3 → 6
2 × 3 = 6
- 3^4 → 4
6 × 3 = 18, and 18 mod 7 = 4
- 3^5 → 5
4 × 3 = 12, and 12 mod 7 = 5
- 3^6 → 1
5 × 3 = 15, and 15 mod 7 = 1; then the cycle repeats.
Step 6 of 13Practice · Work it out
Compute 34 mod 7.
Step 7 of 13Learn
Diffie-Hellman with paint and numbers
A popular analogy uses paint. Alice and Bob publicly agree on a common color, yellow. Each picks a secret color: Alice red, Bob blue. Each mixes their secret with yellow and sends the mixture across the open channel: Alice sends orange, Bob sends green. Now each adds their own secret color to what they received. Alice gets yellow + blue + red; Bob gets yellow + red + blue: the same brown. Eve saw yellow, orange and green, but separating mixed paint back into its components is impractical, so she can't make the brown.
With numbers, using the public values p = 23 and g = 5:
- Alice picks a secret a = 4 and sends A = 54 mod 23 = 4.
- Bob picks a secret b = 3 and sends B = 53 mod 23 = 10.
- Alice computes Ba mod p = 104 mod 23 = 10,000 mod 23 = 18.
- Bob computes Ab mod p = 43 mod 23 = 64 mod 23 = 18.
Both get 18 because (gb)a = (ga)b = g^(ab) mod p. Eve knows p, g, A and B but would need to solve a discrete logarithm to find a or b.
Real systems use primes of 2048 bits or more, or, far more commonly today, elliptic curve Diffie-Hellman (ECDH), which achieves similar security with much smaller numbers: a 256-bit elliptic curve key, such as Curve25519 (X25519), is roughly as strong as 3072-bit classic Diffie-Hellman, and much faster. The shared value is then fed into a key derivation function to produce symmetric keys for AES or ChaCha20.
Step 8 of 13Practice · Diagram
Diffie-Hellman step by step
Which values never travel over the network?
Tap the shape that answers the question, then press Check.
- Agree on p and g
Public. Example: p = 23, g = 5.
- Each picks a secret
Alice picks a, Bob picks b. Never sent.
- Send g^a and g^b
A = ga mod p and B = gb mod p cross the network openly.
- Each raises the other's value
Alice computes Ba, Bob computes Ab mod p.
- Same shared secret
Both get g^(ab) mod p. An eavesdropper sees only p, g, A and B.
Step 9 of 13Practice · Work it out
Alice and Bob use p = 23 and g = 5. Alice's public value is A = 4. Bob's secret is b = 2. What shared secret does Bob compute?
Step 10 of 13Learn
The man in the middle
Diffie-Hellman defeats a passive eavesdropper. It does nothing, on its own, against an active attacker.
Suppose Mallory sits between Alice and Bob and can intercept and replace messages, for example by running a rogue Wi-Fi hotspot. When Alice sends A, Mallory blocks it and sends Bob her own value M. When Bob sends B, Mallory sends Alice M instead. Now Alice shares one key with Mallory, thinking it's Bob, and Bob shares a different key with Mallory, thinking it's Alice. Mallory decrypts every message from Alice, reads or alters it, re-encrypts it for Bob, and neither notices anything wrong. This is a man-in-the-middle (MITM) attack.
The fix is authentication: Alice must be able to verify that the value she received really came from Bob. In practice this is done with digital signatures: Bob signs his Diffie-Hellman value with his long-term private key, and Alice checks the signature with Bob's public key. But then Alice needs to be sure she has Bob's real public key, which leads to certificates, the topic of the last lesson in this module.
The lesson generalizes: key exchange gives you a shared secret with someone. Authentication tells you who. Every secure protocol needs both.
Step 11 of 13Diagram
The man in the middle runs two exchanges
Tap a shape with to see more.
- Alice
Thinks she is talking to Bob.
- Key 1 (Alice–Mallory)
Mallory answers Alice with her own DH value.
- Mallory decrypts, reads, re-encrypts
- Key 2 (Mallory–Bob)
A second exchange with Bob.
- Bob
Thinks he is talking to Alice. Fix: authenticate the DH values with signatures.
Step 12 of 13Note
Step 13 of 13Practice · What would you do?
What would you do?
In a café, Jamal connects to a free Wi-Fi network named Cafe_Guest, which is actually run by an attacker. His app performs a plain Diffie-Hellman exchange with a server but doesn't verify any signatures or certificates. What can the attacker do?
Lesson complete!