Diffie–Hellman key exchange
How two people can agree on a secret key across a completely public channel, without ever sharing it. Explained with paint.
The problem: talking privately in public
Imagine you want to send a private message to a friend over the internet. To encrypt it, you both need to agree on a secret key. But here's the catch: your entire conversation is visible to anyone listening on the network. Your ISP, routers, even an eavesdropper sitting in the same coffee shop can see it all.
How do you agree on a secret without the eavesdropper learning it too? This is exactly the problem that Whitfield Diffie and Martin Hellman solved in 1976. Their algorithm lets two parties conjure a shared secret entirely in public, using some elegant mathematics.
Think of it like mixing paint
Mixing paints is easy, but “unmixing” them is practically impossible. Diffie–Hellman exploits a similar mathematical one-way street. Here's how it works, step by step. Try the sliders in each stage.
Alice and Bob first agree on a public starting colour. This is totally visible. Eve the eavesdropper can see it too, and that's fine. In real maths, this corresponds to two public numbers: a large prime P and a generator G.
Eve can see this colour. Knowing the base colour alone tells her nothing useful.
Alice picks a secret colour she never shows anyone. Bob does the same. Each mixes their private colour into the public colour, creating their own mixed colour to send across. In maths, this is the private key: a secret number each person chooses.
Her private colour stays secret. Only the mix is sent.
His private colour stays secret. Only the mix is sent.
⇄ Alice and Bob swap their mixed colours in public
Alice takes Bob's mixed colour and adds her own private colour. Bob does the same with Alice's mixed colour. Despite doing it in a different order, they both arrive at the exact same final colour. That's their shared secret.
What about Eve the eavesdropper?
Eve saw everything: the public colour, Alice's mixed colour, and Bob's mixed colour. But she's stuck. To work out the shared secret, she'd need to “unmix” a colour to find the private ingredient. That's computationally infeasible.
In real cryptography this one-way difficulty comes from the discrete logarithm problem. It's trivial to compute Ga mod P, but given the result, G and P, working backwards to find a is computationally intractable, especially with the enormous numbers used in practice.
The real mathematics
The paint analogy maps neatly onto real arithmetic. The colour “mixing” is modular exponentiation, a function that's easy to compute in one direction but extremely hard to reverse.
G = (generator)
x = mod
x =
y = mod
y =
KA = mod
KA =
KB = mod
KB =
What's public, what's private?
Here's every value in the exchange, who gets to see it, and its place in the paint analogy.
| Value | Who can see it | Paint analogy |
|---|---|---|
| P and G (prime and generator) | Public | The shared starting colour |
| Alice's public value (x) | Public | The mixture Alice sends to Bob |
| Bob's public value (y) | Public | The mixture Bob sends to Alice |
| Alice's private key (a) | Alice only | Alice's secret colour |
| Bob's private key (b) | Bob only | Bob's secret colour |
| Shared secret (K) | Alice and Bob | The final colour only they can produce |
How this powers every HTTPS connection
Diffie–Hellman isn't just a clever puzzle. It's the engine behind TLS (Transport Layer Security), the protocol that puts the S in HTTPS and secures everything from your banking to this very page.
- Client hello Your browser connects to a server and announces the cryptographic methods it supports. The server responds with its certificate (proof of identity) and its public Diffie–Hellman parameters, the equivalent of the shared starting colour.
- Key exchange Your browser picks a random private key and sends its public value to the server. The server does the same. Both sides then independently compute the same shared secret, just like Alice and Bob. All of this happens in milliseconds.
- Session key derivation The shared secret is fed through a key derivation function to produce a session key. This key is used with a fast symmetric cipher such as AES to encrypt the actual data. Diffie–Hellman handles the handshake; AES does the heavy lifting.
- Encrypted connection established Everything you send and receive is now encrypted with a key that was never transmitted. Even if an attacker recorded every packet, they cannot decrypt it without solving the discrete logarithm problem. For a 2048-bit prime, that would take longer than the age of the universe.
Why this matters
- One-way mixing. Modular exponentiation is fast to compute but practically impossible to reverse. That asymmetry is the mathematical foundation of public-key cryptography.
- Secure over public channels. Diffie–Hellman lets two strangers establish a shared secret in full public view. It was a revolutionary idea in 1976 and remains central to internet security today.
- Powers the modern web. HTTPS connections, end-to-end encrypted messages and VPN tunnels all rely on this simple idea: mixing colours that can never be unmixed.