Alice
Alice's secret paint
Unlocks at step 2
Alice's mix to send
Unlocks at step 3
Alice's final pot
Bob's mix + Alice's secret
Unlocks at step 4
Diffie-Hellman lets two people who have never met agree on a secret while someone listens to every word. Start with paint, then follow the same idea into the math your browser uses on every HTTPS connection.
Alice and Bob want the same secret paint. Everything they send crosses a public channel, and Eve copies all of it. Mixing paint is easy; un-mixing it is not.
Unlocks at step 2
Unlocks at step 3
Bob's mix + Alice's secret
Unlocks at step 4
Unlocks at step 3
Unlocks at step 2
Unlocks at step 3
Alice's mix + Bob's secret
Unlocks at step 4
Paint is a great picture of the idea, but it isn't a perfect model. A chemist with a spectrometer could probably work out which pigments went into a mix, so "un-mixing" is hard rather than impossible. Real Diffie-Hellman replaces paint with modular exponentiation, where reversing the step (the discrete logarithm problem) has no known fast method for well-chosen numbers on classical computers.
Paint also hides how much secret is in play. There are only a few thousand paint colors anyone could tell apart, while a 2048-bit Diffie-Hellman secret has a number of possibilities with more than 600 digits. The Play as Eve and Brute-force race tabs make that difference concrete.
You copied the public paint and Alice's mix off the channel. Your job: find Alice's secret paint. Pick a candidate and you'll see what it makes when mixed with the public paint. If your test pot matches the intercepted pot exactly, you've cracked it.
Pick a candidate paint below to test it.
Eve only listens. Mallory can also grab pots off the channel and send her own in their place. Plain Diffie-Hellman never checks who a pot came from, so watch what happens.
Sends
Receives "from Bob"
Alice's key
Mallory's own mix
Key she shares with Alice
Key she shares with Bob
Sends
Receives "from Alice"
Bob's key
In TLS 1.3 the server signs the handshake, including its key share, with the private key behind its certificate. Your browser checks that signature against a certificate chain it trusts. Mallory can still swap key shares, but she can't produce a valid signature for someone else's domain, so the connection fails instead of quietly leaking. SSH does the same job with host keys, which is why SSH warns you when a server's host key changes.
The public paint becomes a number g, and mixing in a secret a becomes ga mod p. "Mod p" means you keep only the remainder after dividing by p, so every answer lands somewhere on a clock with p − 1 positions.
Watch the powers of g hop around the clock. A good generator visits every position, so every secret gives a different public value.
Uses JavaScript BigInt, so the answers stay exact from two digits up to a real 2048-bit group.
Eve's simplest attack: try g1, g2, g3… until one equals Alice's public value A. Your device is the attacker. Every extra bit in p roughly doubles the work.
| Size of p | Worst-case time |
|---|---|
| Run a crack first to measure your device's speed. | |
Real attackers use smarter algorithms than counting (the number field sieve), which is why 2048-bit Diffie-Hellman is rated at about 112 bits of security rather than 2048. That's the last row above, and it's still out of reach.
Pair up. One person is Alice and the other is Bob, each on their own phone or laptop. Everyone else in the room is Eve, and gets to hear every code you say out loud. This runs real Diffie-Hellman with p = 999,983 and g = 5, and turns each number into a paint color.
Each paint hides a different random secret number. Keep your screen away from Eve.
Your code for your partner (Eve can hear it too):
––– –––
Eve's challenge. Students playing Eve write down both codes, then open the Brute-force race tab and choose "A classmate's code." With p under a million, a laptop cracks it in well under a second. Ask the room what that says about key size.
Mallory variation. Seat a third student between the pair. Alice and Bob pass codes on paper through Mallory, who keeps them and passes along her own code instead (Mallory pairs with each of them separately on two browser tabs). Then Alice and Bob hold up their pots: different colors, and each matches one of Mallory's.
Fresh secrets. "Shuffle paints" picks new random secrets, which is what ephemeral Diffie-Hellman (DHE/ECDHE) does for every session.
You've almost certainly used Diffie-Hellman today. Modern systems mostly use the elliptic-curve version (ECDH), which gives the same security with much smaller numbers.
Start a capture, visit any HTTPS site, and filter with tls.handshake.type == 1 to find the Client Hello. Expand Extension: key_share and you'll see the group your browser offered and its public value, the number Eve gets to see. The Server Hello carries the server's half. Nothing in the capture reveals the shared secret.
Forward secrecy comes from ephemeral exchanges (DHE, ECDHE), with new secrets every session, so stealing a server's long-term key later doesn't unlock old recordings. Authentication isn't built in; pair the exchange with signatures and certificates or Mallory wins. Parameters matter: the 2015 Logjam attack forced connections down to 512-bit "export" groups and showed that widely shared 1024-bit primes were within reach of precomputation. Use standard groups (RFC 7919 for finite-field DH) of at least 2048 bits, or prefer X25519. Quantum computers running Shor's algorithm would break both DH and ECDH, which is why hybrid post-quantum key exchange is already rolling out.