ViewWiki

Lesson 4 of 4

The Diffie–Hellman key exchange.

About 30 minutes · 6 practice questions

How two strangers agree on a secret key over a channel everyone can hear: mixing paint, the same idea in modular arithmetic, and why Eve cannot work out the key herself.

Step 1 of 6 · Mixing paint

Step 1 of 6

Mixing paint

In 1976 Whitfield Diffie and Martin Hellman published a way for two people who have never met to agree on a secret key, while everything they say to each other is overheard. Look inside is open at Mixing paint, a picture that explains the idea.

Alice and Bob agree in public on one colour, yellow. Each secretly chooses a colour of their own, and tells it to nobody. Each mixes their secret into the yellow and sends the mixture down the tube. Watch what passes through the tube, and what each side does with what it receives.

To the question

Step 2 of 6

From paint to numbers

The paint is only a picture: paint is easy to mix and hard to unmix. In the real exchange, mixing is replaced by a power mod a prime, from the Lesson on clock arithmetic. The public values are the prime p=23p = 23⁠ and the generator g=5g = 5⁠. Alice chooses a secret aa⁠ and sends A=ga mod pA = g^a \bmod p⁠; Bob chooses a secret bb⁠ and sends B=gb mod pB = g^b \bmod p⁠.

Look inside is still on the paint, and the numbers are hidden: you will do the arithmetic yourself.

To the question

Step 3 of 6

Alice’s key

Alice has received BB⁠ from Bob. She raises it to the power of her secret, and gets the shared key:

s=Ba mod ps = B^a \bmod p⁠

To the question

Step 4 of 6

Bob’s key

Bob does the same with what he received: he works out Ab mod pA^b \bmod p⁠. Alice and Bob each used a different secret, and never saw the other’s.

To the question

Step 6 of 6

Public and secret

The Diffie–Hellman exchange opened up public-key cryptography: methods in which part of the key is known to everyone and part is kept secret. In RSA, for example, everyone publishes an open padlock anyone can snap shut, and only they hold the key that opens it; it rests on how hard it is to factor a product of two large primes. Every time a browser connects to a secure website, it runs a key exchange of this kind, with numbers hundreds of digits long.

To the question

Interactive scene

Loading the interactive version…

Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.

To the experiment

Practice · Choose

Alice and Bob have never met and share no secret at all. They can talk only through the open tube, and Eve copies everything in it. Can they agree on a shared secret colour?

To the experiment

Practice · Calculate

Now in numbers. The public values: p=23p = 23⁠ and g=5g = 5⁠. Alice’s secret is a=6a = 6⁠. What does she send, A=56 mod 23A = 5^6 \bmod 23⁠?

To the experiment

Practice · Calculate

Bob’s secret is b=15b = 15⁠, and he sent B=19B = 19⁠. Alice combines it with her own secret, a=6a = 6⁠. What is her shared key, s=196 mod 23s = 19^6 \bmod 23⁠?

To the experiment

Practice · Predict, then look

Bob got A=8A = 8⁠ from Alice, and combines it with his own secret: he works out 815 mod 238^{15} \bmod 23⁠. What will he get? Choose, and then Look inside moves to The maths.

To the experiment

Practice · Choose

Eve has copied g=5g = 5⁠, p=23p = 23⁠, A=8A = 8⁠ and B=19B = 19⁠. What else does she need to work out the shared key ss⁠?