Diffie–Hellman key exchange explained
The Diffie–Hellman key exchange is a procedure by which two people agree on a secret number even though someone is listening to their conversation the whole time. Everything they say to each other is heard by the listener too, and yet at the end only the two of them know the shared secret. The number then serves as the key to an ordinary cipher. This is how your browser agrees on a key with your bank’s website, which it has never talked to before.
What’s the problem
Every classical cipher, from Caesar’s to Vigenère’s and Vernam’s, needs a key that the sender and the recipient know and nobody else does. But how do you pass it on? If Adam sends it to Bára by email, everyone who intercepts the email on its way can read it. Armies and banks used to carry keys by courier in a locked briefcase. On the internet, where you exchange data with hundreds of servers in a day, you would need rather a lot of couriers.
Mixing colours
The trick can be shown without a single number, using colours. The aim is for Adam and Bára to end up each holding a cup of the same colour, which Eva does not know, without ever sending it to each other. That colour will then be their key. Eva sees everything they send.
- Adam and Bára publicly agree on a common colour, say yellow. Eva knows it too.
- Each secretly chooses a colour of their own and tells nobody: Adam red, Bára blue.
- Adam mixes the yellow with his red and sends the orange to Bára. Bára mixes the yellow with her blue and sends the green to Adam. Eva now sees the yellow, the orange and the green.
- Adam mixes his red into the green from Bára. Bára mixes her blue into the orange from Adam.
And that’s it: both have the same brown. Both cups contain yellow, red and blue, just added in a different order. Nobody sent this brown anywhere, so Eva does not have it. To mix it, she would need at least one secret colour, for example to pull the red back out of the orange. But mixing colours is easy, unmixing them is impossible. And if Eva pours the orange and the green together, she has yellow twice and her brown comes out different.
With numbers we need a calculation in place of mixing, one that is easy to do and hard to reverse. Counting with remainders is good for that.
Counting with remainders
The notation
A calendar counts the same way: 26 days after Monday is Saturday, because three whole weeks change nothing and the remaining five days move Monday to Saturday. However big the number you divide, the remainder after division by seven is always from 0 to 6. The division with remainder calculator will do division with remainder for you.
Diffie–Hellman with small numbers
The aim is the same as with the colours: Adam and Bára should in the end both know the same secret number, a key that Eva does not know, without ever sending it. The steps are the same too, except that instead of mixing colours we calculate powers and remainders after division.
1. Public numbers (like the yellow colour). Adam and Bára publicly agree on two numbers: a prime number p = 23 and a base g = 5. Everyone knows these, Eva included. A prime is used so that the powers of g can pass through all the remainders and the attacker has no shortcut at hand. The base is chosen so that its powers pass through as many remainders as possible, and five passes through all 22 of them modulo 23.
2. Secret numbers (like red and blue). Each of them privately chooses a secret number: Adam a = 4, Bára b = 3. They tell nobody and send them nowhere.
3. The exchange (like orange and green). Adam raises the base to his secret number, 54 = 625, and takes the remainder after division by 23. Twenty-three goes into 625 twenty-seven times, making 621, and 4 is left:
Bára calculates with her three: 53 = 125, which contains five 23s, making 115:
Adam sends Bára the number 4 and Bára sends him the number 10. Eva sees these two numbers.
4. The key (like brown). Each raises what they received to their own secret number. Adam:
Bára:
Both got 18, and that is their shared key. Nobody sent the number 18, so Eva does not know it, and Adam and Bára can now use it to encrypt whatever they like.
| Who | Secret number | Sends | Key |
|---|---|---|---|
| Adam | a = 4 | 4 | 18 |
| Bára | b = 3 | 10 | 18 |
| Eva | doesn’t know | nothing | doesn’t know, sees only 23, 5, 4 and 10 |
Why they get the same number
Bára sent 53 and Adam raised it to the fourth power. Adam sent 54 and Bára raised it to the third. By the rules for powers, both are the same:
We may take the remainder after every step, and the result does not change. So Adam does not have to work with ten thousand: 102 = 100 and
Why Eva cannot find out a secret number
Eva knows 23, 5, 4 and 10. To get the key she needs at least one secret number, so she faces the opposite task: she knows the result 4 and is looking for the power to which five was raised (technically the discrete logarithm).
With small numbers she simply tries. The powers of five modulo 23 come out as 5, 2, 10, 4, so a = 4 turns up on the fourth try. But the next ones continue 20, 8, 17, 16, 11, 9 and jump back and forth without rhyme or reason, and you cannot tell from the result where to look.
In reality the prime p has around 600 digits. These days browsers more often reach for a variant with elliptic curves, which needs smaller numbers. Raising to a power takes Adam and Bára a fraction of a second, because they take the remainder after every multiplication. Eva would have to try exponents one by one, and there are as many of them as there are numbers up to p, that is, a number with 600 digits. Not even all the computers in the world could manage that. Nobody has proved that no cleverer way exists, but nobody has found one in decades. Raising to a power is easy, finding the exponent is hard, just like with the colours.
One thing, however, Diffie–Hellman does not guard against by itself: Adam does not know whether he is really talking to Bára. If Eva not only listened to the messages but also forged them, she would agree on a key with Adam separately and with Bára separately and would read everything. That is why in practice the messages of the exchange are also digitally signed.
Where you come across Diffie–Hellman
Every time your browser connects over HTTPS or you connect to a VPN. In router or VPN settings it appears as “DH group 14” or “modp2048”, which states the size of p, and the abbreviations ECDH and X25519 denote the variant with elliptic curves. You don’t need to change anything; the defaults are sensible.
The difference from RSA
Diffie–Hellman does not encrypt anything itself; it only agrees on a shared secret number, which is then used to encrypt the messages with an ordinary cipher. The RSA cipher encrypts directly: anyone can encrypt a message with the recipient’s public key and only the recipient can open it.
Calculator
Enter a prime p, a base g and two secret numbers. The calculator shows what Adam and Bára send each other, and the shared key as each of them calculates it. Try for instance the secret numbers 6 and 15: they send each other 8 and 19 and the key comes out as 2.
Who came up with it
The procedure was published by Whitfield Diffie and Martin Hellman of Stanford University in November 1976 in the paper “New Directions in Cryptography”. Independently of them, Ralph Merkle was working on agreeing on a key in public, and Hellman therefore later proposed speaking of the Diffie–Hellman–Merkle exchange. Only much later did it come out that the British intelligence agency GCHQ had a similar procedure as early as the 1970s: it was devised by the mathematician Malcolm Williamson, but his work stayed secret and was published only in 1997.