Every cipher so far needs a shared secret key. But how do two people who have never met agree on a key, over a line that anyone can listen to? In 1976 Diffie and Hellman found a way, built on discrete logarithms: easy to go forward, hard to go back.
- Public: a large prime $p$ and a primitive root $g$ mod $p$.
- Alice picks a secret $a$ and sends $A = g^{a} \bmod p$. Bob picks a secret $b$ and sends $B = g^{b} \bmod p$.
- Alice computes $B^{a}$, Bob computes $A^{b}$. Both get the shared key $K = g^{ab} \bmod p$.
Why it works
Worked example: A small exchange
- $A = 2^{9} = 512 = 17 \cdot 29 + 19$, so $A = 19$.
- $B = 2^{13} = 8192 = 282 \cdot 29 + 14$, so $B = 14$.
- Alice: $K = 14^{9} \bmod 29 = 3$. Bob: $K = 19^{13} \bmod 29 = 3$.
- Check: $K = 2^{9 \cdot 13} = 2^{117}$, and $117 \equiv 5 \pmod{28}$, so $K \equiv 2^{5} = 32 \equiv 3$.
Answer: $A = 19$, $B = 14$, $K = 3$.
Worked example: Eve with a tiny prime
- Eve lists $2^{1}, 2^{2}, \dots \bmod 29$ until she sees $19$: $2^{9} \equiv 19$, so $a = 9$.
- Then she computes $B^{a} = 14^{9} \equiv 3$, the key. With $p$ near $10^{600}$ the list would be far too long to search.
Answer: A small prime makes the discrete logarithm easy.
Common mistake
Why a primitive root? Then $g^{a}$ runs through every nonzero residue as $a$ varies, so the shared key can be any of $p - 1$ values and Eve gains nothing from guessing. If $g$ had a small order, say 4, there would be only 4 possible keys.
Key idea