✖

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.

  1. Adam and Bára publicly agree on a common colour, say yellow. Eva knows it too.
  2. Each secretly chooses a colour of their own and tells nobody: Adam red, Bára blue.
  3. 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.
  4. Adam mixes his red into the green from Bára. Bára mixes her blue into the orange from Adam.

Adam, Eva and Bára: a public yellow, Adam adds red and sends orange, Bára adds blue and sends green, Eva sees both mixtures; finally Adam mixes the green with red and Bára the orange with blue and both have the same brown, Eva does not Adam, Eva and Bára: a public yellow, Adam adds red and sends orange, Bára adds blue and sends green, Eva sees both mixtures; finally Adam mixes the green with red and Bára the orange with blue and both have the same brown, Eva does not

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 26mod7 is read “26 modulo 7” and means the remainder after division. Three sevens go into 26, making 21 altogether, and 5 is left:

26mod7=5

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:

54mod23=625mod23=4

Bára calculates with her three: 53 = 125, which contains five 23s, making 115:

53mod23=125mod23=10

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:

104mod23=10,000mod23=18

Bára:

43mod23=64mod23=18

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

At the top the public numbers p = 23 and g = 5. Adam has the secret a = 4 and sends Bára 5⁴ mod 23 = 4, Bára has the secret b = 3 and sends Adam 5³ mod 23 = 10. Adam calculates 10⁴ mod 23 = 18, Bára 4³ mod 23 = 18. Eva in the middle does not know the secret numbers. At the top the public numbers p = 23 and g = 5. Adam has the secret a = 4 and sends Bára 5⁴ mod 23 = 4, Bára has the secret b = 3 and sends Adam 5³ mod 23 = 10. Adam calculates 10⁴ mod 23 = 18, Bára 4³ mod 23 = 18. Eva in the middle does not know the secret numbers.

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:

(53)4=(54)3=512

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 100mod23=8, then 82 = 64 and 64mod23=18. The result is the same as before. In general, when Adam sends A=gamodp and Bára sends B=gbmodp, we have

Bamodp=Abmodp

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.