Diffie-Hellman key exchange
How can two people who have never met agree on a secret key while someone listens to every word they say? In 1976 Whitfield Diffie and Martin Hellman found the answer that now protects almost every secure connection on the internet. Follow Alice and Bob through the five steps, once as paint mixing and once with real numbers, and then let the eavesdropper Eve attack: with small numbers she wins in a blink, with the sizes used in practice she would need longer than the age of the universe. Press Next step to follow Alice and Bob as they agree on a secret while Eve reads everything they send; Back goes one step back. Each value is shown as a paint and as a number. Locked values never leave their owner. Choose a larger prime or other secrets on the right, or press New secrets. At the last step, Let Eve attack makes her try every exponent until one fits: easy for small primes, hopeless for real ones.
How it works
- Press Next step to follow the exchange: the public agreement, the secrets, the messages and the shared result.
- Watch each value as a paint and as a number, and see which ones stay private and which ones Eve can read.
- At the last step press Let Eve attack, then choose a bigger prime and see how quickly her chances vanish.
How the exchange works
Alice and Bob first agree in public on a prime p and a number g. Each then picks a secret number, a for Alice and b for Bob, and keeps it to themselves. Alice sends Bob A = gᵃ mod p, and Bob sends Alice B = gᵇ mod p. Finally Alice computes Bᵃ mod p and Bob computes Aᵇ mod p. Both results equal g to the power a·b mod p, so they now share a number that never crossed the wire. Eve, who saw p, g, A and B, has no such shortcut.
Why Eve is stuck
Computing gᵃ mod p is quick, even for numbers with hundreds of digits, thanks to repeated squaring. Going back, finding the exponent a from the result, is the discrete logarithm problem, and no fast method is known for well-chosen primes. For the small primes in this demo Eve can simply try every exponent; the app counts her guesses and estimates how long a full search would take. Each extra bit of the prime doubles the work, so the 2048-bit primes used on the internet put a brute-force search far out of reach. The paint version captures the same idea: mixing colours is easy, but separating a mixture back into its colours is practically impossible.
Frequently asked questions
Why does the paint analogy work?
Mixing paint is easy and cannot be undone, just as raising g to a power mod p is easy to do and hard to undo. Alice and Bob each add their secret colour to a common one and swap the mixtures; then each adds their own secret colour to the mixture they received. Both end with the common colour plus both secrets, while Eve only sees the two two-colour mixtures and cannot separate them. Here paints are mixed in equal parts, so the order does not matter, just as (gᵃ)ᵇ = (gᵇ)ᵃ.
What is g and why must it be a primitive root?
g is a generator: its powers g¹, g², …, g^(p−1) mod p run through every number from 1 to p − 1. That makes the public values spread over all possibilities, so Eve learns as little as possible from them. The app picks the smallest such number for each prime, for example g = 5 for p = 23.
Does Diffie-Hellman encrypt my messages?
No. It only agrees on a shared secret. That secret is then used as the key for a fast symmetric cipher such as AES, which encrypts the actual data. Every HTTPS connection works this way: a key exchange, usually with elliptic curves, followed by symmetric encryption.
Can Eve pretend to be Bob?
On its own, yes. A man in the middle could run one exchange with Alice and another with Bob and relay everything. Real protocols prevent this by having the server prove its identity with a digital signature and a certificate, so the exchange is authenticated.
Is anything sent to a server?
No. All the numbers, the paint mixing and Eve's attack are computed in your browser, and your settings stay in this browser's local storage.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Go to Editor