← Codes you can try

Diffie–Hellman Key Exchange

Two people agree on a secret while everyone watches, without ever sending the secret itself.

This page shows how it works with colours, and then with small numbers. Everything runs in your browser. Nothing you pick here is sent or kept.

I The idea

The idea

Alice and Bob want to agree on a secret. A secret key is a number they can use to lock and unlock messages. But they have to talk on a line that anyone can listen to. Eve is listening in.

They can’t just say the key out loud, because Eve would hear it.

Diffie–Hellman key exchange solves this. Each person keeps one secret to themselves and sends a mixed-up version of it. After a little more mixing, both of them end up with the same result. Eve hears everything that is sent. The secret itself is never sent.

The exchange hides the secret from Eve, but it does not prove who you are talking to. Someone could sit in the middle and pretend to be Bob. Real systems add a way to check who is on the other end.

II Part 1

Part 1: Mix colours

Follow the steps from 1 to 6. Change any colour and everything updates. Every colour has a name and numbers, so you never have to rely on how it looks. Steps 4 and 5 happen on both sides, so each one shows up in Alice’s panel and again in Bob’s.

The colour-mixing tool

1. Pick the public colour

Everyone can see this one: Alice, Bob and Eve.

Alice

The public colour
2. Alice picks a secret colour

Nobody else knows Alice’s secret.

4. She mixes the public colour with her secret and sends it
5. She adds her secret again to the mixture Bob sent

Bob

The public colour
3. Bob picks a secret colour

Nobody else knows Bob’s secret.

4. He mixes the public colour with his secret and sends it
5. He adds his secret again to the mixture Alice sent

What Eve sees

Eve is listening in. She sees what is sent in the open, and nothing else.

The public colour
The mixture Alice sent
The mixture Bob sent

Eve never sees either secret colour.

6. Compare

Alice’s final colour
Bob’s final colour

III Why it works

Why the colours match

Look at what each person has at the end. Alice has the public colour, Bob’s secret (inside the mixture he sent) and her own secret. Bob has the public colour, Alice’s secret and his own secret. That is the same three colours in the same amounts. The order you mix in does not change the result, so both end up with exactly the same colour.

Eve saw the public colour and the two mixtures. She never saw either secret colour on its own.

A note about paint. Real paint mixing isn’t what computers use. Colour is a stand-in for maths that is easy to do one way and very hard to undo. Mixing paint is easy, and unmixing it is very hard. In this model Eve could subtract the public colour from a mixture and work a secret colour back out, because these colour sums are easy to undo. That is why real systems use the maths in Part 2.

IV Part 2

Part 2: The same idea with small numbers

Computers use numbers instead of paint. Everyone can see two public numbers: p = 23 and g = 5. Alice and Bob each pick a secret number from 1 to 22.

The small-numbers tool

Use a whole number from 1 to 22.

    Show the working step by step

    Multiply by the base one time after another, and keep the remainder each time.

      V The maths

      What “mod” means

      “Mod” means the remainder after dividing, like a clock face: on a 12-hour clock, 14 o’clock is 2 o’clock, because 14 mod 12 = 2.

      Alice sends ga mod p, and Bob sends gb mod p. Alice then raises Bob’s number to the power a, and Bob raises Alice’s number to the power b. Both of them get ga×b mod p, so the results match.

      VI Eve

      Why Eve can’t just work it out

      Eve knows p, g and the two numbers that were sent. To find a secret number, she can try every possible power until one gives the number she saw.

      With numbers this small, Eve finds a secret in at most 22 tries. Real systems use much bigger numbers: the classic form of this exchange uses numbers hundreds of digits long. There are far too many to try one by one.

      VII History

      A little history

      Whitfield Diffie and Martin Hellman published the idea in 1976, in a paper called New Directions in Cryptography. It showed how two people who have never met can agree on a key over a line anyone can listen to. The step it uses is the one in Part 2: a number raised to a power, mod a prime number.

      Ralph Merkle, a student at the University of California, Berkeley, had come to the same problem on his own. In 1974 he found a different way for strangers to agree on a key, now called Merkle’s Puzzles. The 1976 paper credits his work. Hellman’s own page says he invented public-key cryptography with Diffie and Merkle. The IEEE’s Milestone page on the invention of public-key cryptography calls this key exchange “the Diffie, Hellman and Merkle public-key exchange”.

      The idea was also found earlier, in secret. James Ellis, Clifford Cocks and Malcolm Williamson, who worked at GCHQ, a British government agency, worked out public-key ideas between about 1970 and 1974. In 1974 Williamson found a way of agreeing on a key that was essentially the one Diffie and Hellman published two years later. It stayed secret until 1997.

      Sources give 1969 or 1970 for the year of Ellis’s idea, so this page says “about 1970”.

      1. About 1970 to 1974In secret at GCHQJames Ellis, Clifford Cocks and Malcolm Williamson work out public-key ideas.
      2. 1974At BerkeleyRalph Merkle, a student, finds his own way for strangers to agree on a key.
      3. 1976In publicWhitfield Diffie and Martin Hellman publish “New Directions in Cryptography”.
      4. 1997The secret is outGCHQ makes its earlier work public.
      The same story as a timeline, drawn in code.

      VIII Keep going

      Keep going

      Try the other pages in Codes you can try, such as the Caesar cipher.

      IX Sources

      Sources

      Whitfield Diffie and Martin E. Hellman, “New Directions in Cryptography” (opens in a new tab), IEEE Transactions on Information Theory, vol. IT-22, no. 6, November 1976, pages 644–654.

      IEEE Milestone, “Invention of Public-key Cryptography, 1969–1975” (opens in a new tab), on the work at GCHQ, whose text also uses the name “the Diffie, Hellman and Merkle public-key exchange”.

      Duncan Campbell, “Going to War over Prime Numbers” (opens in a new tab) (Times Higher Education Supplement, 22 April 1999).

      Ralph Merkle, “History of Public Key Cryptography” (opens in a new tab), and the Computer History Museum’s profile of Ralph Merkle (opens in a new tab). Martin Hellman’s page at Stanford (opens in a new tab). Checked 19 November 2026.