# Schoolyard Example of the Diffie–Hellman Key exchange

Alice wants to give a secret message to Bob in class. Unfortunately she has to pass all her letters past Eve ^{[1]} (who's jealous of their fun, so always reads them).

They agree to encrypt all their messages by substituting each letter by a key ^{[2]}, unfortunately before they can agree on what number to substitute by, Eve joins them.

They've found themselves in class, unable to now tell each other the key. They pass messages through Eve explaining to each other they'll use the Diffie-Hellman key exchange. Eve reads all of this and is increasingly worried^{[3]}.

Alice and Bob agree to the following mathematics.

$$Let\ g = 11,\ p = 13$$

$$g^{Private\ Key}mod(p) = Public\ Key$$

$$Public\ Key^{Private\ Key}mod(p) = Shared\ Secret$$

Alice randomly picks \(31\)^{[4]} as her private-key, meaning:

$$Alice's\ Public\ Key\ = 11^{Alice's\ Private\ Key\ =\ 31}mod(13) = 2$$

Bob decides his private-key is \(17\), meaning his public-key is \(7\).

Bob passes Alice a message saying his public-key is \(7\), Eve reads this gleefully and (knowing already \(g\) & \(p\)) types \((11^x)mod(13) = 7\) into Wolfram-Alpha.

Giggling at Eve's frustration as Wolfram crashes, Alice uses Bob's public-key and her private-key to work out the shared secret.

$$Bob's\ Public\ Key^{Alice's\ Private\ Key}mod(p) = Shared\ Secret$$

$$7^{31}mod(13) = 6$$

She then passes him her public-key and Bob performs the same calculation using Alice's public-key and his private-key.

$$Alice's\ Public\ Key^{Bob's\ Private\ Key}mod(p) = Shared\ Secret$$

$$2^{17}mod(13) = 6$$

Alice and Bob both now know to use \(6\) as the key to substitute all their messages whilst Eve is still fuming over trying to derive the key from only the public data they exchanged.^{[5]}^{[6]}

This involves adding to each letter by a number, and wrapping if you go past Z, then subtracting to decrypt. See Caesar cipher. ↩︎

Sharp-witted friends will realise this would have been a great time for Eve to sabotage the original formula. ↩︎

In reality, this number is a incredibly long prime number. See RSA Key generation. ↩︎

If it helps the reader, try imagine that Alice and Bob are planning a suprise birthday for Eve, and not just being mean... ↩︎

Meanwhile, their teacher Merlin laughs at Eve's lack of a stable quantum computer. See (and cry) Shor's Algorithm. ↩︎