Diffie-Hellman is a method that lets two people end up with the same secret number even though every message they exchange is public. It was published in 1976 by Whitfield Diffie and Martin Hellman, a year before RSA, and it cannot encrypt or sign anything: its single job is to let two parties agree on a secret. This page starts with paint, moves to clock arithmetic with small numbers you can check by hand, and ends with why real keys have to be so large.
The problem it solves
Alice and Bob want to encrypt their conversation with a fast shared-key cipher such as AES, which needs both of them to hold the same secret key. They have never met, and Mallory hears every byte they send. If Alice simply picks a key and sends it, Mallory copies it. RSA can lock the key with Bob's public key, but then one long-lived private key protects every recording, so a single leak years later opens all of them. Diffie-Hellman avoids both problems because no secret is ever sent.
The paint analogy
Imagine Alice and Bob agree in public on a yellow base paint, and Mallory watches. Each secretly picks a colour of their own and never shows it to anyone.
- Alice mixes her secret colour into a bucket of the yellow base, and Bob does the same with his. Each sends the resulting blend to the other, so Mallory sees both blends.
- Each adds their own secret colour to the blend they received.
- Both buckets now hold the same three ingredients: the base, Alice's colour and Bob's colour, so they match.
Mallory holds only blends, and she cannot pull a secret colour back out of a mixture. Real paint can be roughly unmixed by eye, so the real version uses remainders instead.
The same trick with clock arithmetic
A remainder, written x mod p, is where you land after counting x steps around a clock with p positions, so 17 mod 5 is 2. Alice and Bob agree in public on a prime p = 23 and a base g = 5. Then each picks a secret number: a = 6 for Alice and b = 15 for Bob.
the exchange with p = 23 and g = 5
- Alice sends A = 56 mod 23 = 8.
- Bob sends B = 515 mod 23 = 19.
- Alice computes 196 mod 23 = 2.
- Bob computes 815 mod 23 = 2.
The numbers 8 and 19 are the paint blends, and the shared secret is 2. Both results equal 56×15 mod 23, because (gb)a = (ga)b and the exponents are simply applied in a different order. The two sides never sent 6, 15 or 2.
| Who | Knows | Does not know |
|---|---|---|
| Alice | p, g, a, B | b |
| Bob | p, g, b, A | a |
| Mallory | p, g, A, B | a, b and the secret |
What Mallory would have to solve
To find the secret, Mallory needs a or b, which means solving 5x mod 23 = 8 for x. This is called the discrete logarithm problem. With p = 23 she can simply try all 22 values and find x = 6. With a 2048-bit p there is no known fast method, while Alice and Bob need only a few thousand multiplications. Calculating forwards is cheap and undoing it is not, which is the property every public-key system leans on.
Why it gives forward secrecy
The secret numbers a and b are made fresh for each connection and deleted afterwards. A key stolen from the bank later cannot reopen a recording, because the numbers that made its session key no longer exist anywhere. This property is called forward secrecy, and it is the reason TLS 1.3 uses Diffie-Hellman for every connection.
Why the keys are so large
Every public number in the clock version is as large as p. Staying safe for decades needs a p of about 3,072 bits, so Alice and Bob each send a 384-byte number. The size is forced on them because Mallory does not try every value. Whole numbers mod p have a structure, the same one the best factoring methods exploit, that shortens her search, so each extra bit adds less protection than you would hope.
We measure protection in bits of effort: 128 bits of protection means an attacker needs about 2128 steps, more than all the computers on Earth could perform. The clock version reaches that level only at 3,072 bits. A different kind of arithmetic, adding points on a curve, reaches it with 256-bit keys, which are twelve times smaller. That version is the subject of the elliptic curve page.
The catch: it does not say who you are talking to
Diffie-Hellman will agree a secret with whoever answers. Mallory can run one exchange with Alice and a second one with Bob, then decrypt each message with the first secret and encrypt it again with the second. Both ends would see a working connection while she reads everything. The cure is to prove identity separately, which is why the handshake also carries a certificate and a signature.
What a quantum computer would change
Shor's algorithm solves discrete logarithms efficiently on a large enough quantum computer, so both the clock version and the curve version would fall. No machine of that size exists today. The post-quantum page explains the replacement that browsers already run alongside the elliptic-curve exchange.