← Lesson 1 of 1
Intermediate 12 min read

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.

Step 1 Agree on a public colour

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.

Public colour

Eve can see this colour. Knowing the base colour alone tells her nothing useful.

Step 2 Each person adds their own secret colour

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.

Alice
Private
Public
Sends

Her private colour stays secret. Only the mix is sent.

Bob
Private
Public
Sends

His private colour stays secret. Only the mix is sent.

⇄ Alice and Bob swap their mixed colours in public

Step 3 Each adds their private colour to what they received

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.

Alice's final mix
Bob's mix
Private
Secret
Bob's final mix
Alice's mix
Private
Secret
Both arrived at the same secret colour. They share a key.

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.

Try it Change the private keys and watch both sides reach the same secret

Shared public values
P = (prime)
G = (generator)
Private keys (secret)
Alice's public value
x = Ga mod P
x = mod
x =
Sends publicly: x =
Bob's public value
y = Gb mod P
y = mod
y =
Sends publicly: y =
Alice derives the secret
KA = ya mod P
KA = mod
KA =
Bob derives the secret
KB = xb mod P
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.

ValueWho can see itPaint analogy
P and G (prime and generator)PublicThe shared starting colour
Alice's public value (x)PublicThe mixture Alice sends to Bob
Bob's public value (y)PublicThe mixture Bob sends to Alice
Alice's private key (a)Alice onlyAlice's secret colour
Bob's private key (b)Bob onlyBob's secret colour
Shared secret (K)Alice and BobThe 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.