Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The Diffie–Hellman key exchange lets two people who have never met agree on a shared secret key over a channel that anyone can listen to. They can then use that key with a fast symmetric cipher for the rest of their conversation. It’s the paint-mixing idea from the previous section, with modular arithmetic in place of paint.

Whitfield Diffie and Martin Hellman published the method in 1976, in a paper called “New Directions in Cryptography,” building on ideas from Ralph Merkle. It was the first published method for agreeing on a key in public. It later came out that cryptographers at GCHQ, the British signals intelligence agency, had found the same ideas a few years earlier: James Ellis had the idea of “non-secret encryption” around 1970, and Malcolm Williamson found this key exchange around 1974. Their work stayed classified until 1997.

The Protocol

Alice and Bob get the same number because of a rule of exponents you already know:

Ba=(gb)a=gab=(ga)b=AbB^a = \left(g^b\right)^a = g^{ab} = \left(g^a\right)^b = A^b

Reducing modulo pp along the way doesn’t change that.

PaintDiffie–Hellman
white, the public starting colorthe public values pp and gg
Alice’s secret redAlice’s private exponent aa
the pink paint Alice sendsA=ga(modp)A = g^a \pmod{p}
the light blue paint Bob sendsB=gb(modp)B = g^b \pmod{p}
the light purple they both end up withthe shared secret s=gab(modp)s = g^{ab} \pmod{p}

Example

Alice and Bob agree on p=23p = 23 and g=5g = 5. Alice picks a=4a = 4 and Bob picks b=3b = 3.

  • Alice sends A=54(mod23)=625(mod23)=4A = 5^4 \pmod{23} = 625 \pmod{23} = 4.

  • Bob sends B=53(mod23)=125(mod23)=10B = 5^3 \pmod{23} = 125 \pmod{23} = 10.

  • Alice computes s=104(mod23)=18s = 10^4 \pmod{23} = 18.

  • Bob computes s=43(mod23)=18s = 4^3 \pmod{23} = 18.

Here’s the same exchange as a diagram. Eve sits between Alice and Bob and sees only the public values.

Python’s built-in pow(base, exponent, modulus) does modular exponentiation, so you can check every step:

Alice sends A = 4
Bob sends B = 10
Alice's secret: 18
Bob's secret:   18

Eve saw p=23p = 23, g=5g = 5, A=4A = 4 and B=10B = 10. She never saw aa, bb or the secret 18.

Why Eve Is Stuck: The Discrete Log Problem

To find the secret, Eve needs aa or bb. To find aa, she has to solve

5a≡4(mod23)5^a \equiv 4 \pmod{23}

for aa. Undoing an exponent is a logarithm, and since we’re working with whole numbers mod pp, this is called the discrete logarithm problem.

For p=23p = 23, Eve can just try every exponent:

4

With p=23p = 23 that takes at most 22 tries. But a brute force search might have to try every exponent up to pp, so each extra digit in pp makes the search about ten times longer. Real Diffie–Hellman uses primes with at least 2048 bits (about 617 digits). Mathematicians know cleverer methods than brute force, which is why the primes have to be so large, but even the best known methods can’t handle primes that big.

Easy One Way, Hard the Other

There’s a catch. Alice also has to compute ga(modp)g^a \pmod{p} with an exponent aa that has hundreds of digits. If she multiplied by gg one step at a time, she’d be as slow as Eve. Instead she uses square and multiply (also called fast modular exponentiation):

For example, to find 513(mod23)5^{13} \pmod{23}:

  • 51≡55^1 \equiv 5, 52≡25^2 \equiv 2, 54≡22=45^4 \equiv 2^2 = 4, and 58≡42=16(mod23)5^8 \equiv 4^2 = 16 \pmod{23}.

  • 13=8+4+113 = 8 + 4 + 1, so 513=58⋅54⋅51≡16⋅4⋅5=320≡21(mod23)5^{13} = 5^8 \cdot 5^4 \cdot 5^1 \equiv 16 \cdot 4 \cdot 5 = 320 \equiv 21 \pmod{23}.

The number of squarings is the number of bits in the exponent, so even a 2048-bit exponent needs only about 2048 squarings. This is what Python’s pow(g, a, p) does. Here’s the same idea written out:

21
21

So computing ga(modp)g^a \pmod{p} is fast, while undoing it seems to be extremely slow. That’s the one-way function Diffie–Hellman depends on, just like mixing paint is easy and un-mixing it isn’t.

Choosing gg: Use a Generator

Not every base is a good choice. Compare the powers of 5 and the powers of 2 modulo 23:

powers of 5 mod 23: [5, 2, 10, 4, 20, 8, 17, 16, 11, 9, 22, 18, 21, 13, 19, 3, 15, 6, 7, 12, 14, 1]
powers of 2 mod 23: [2, 4, 8, 16, 9, 18, 13, 3, 6, 12, 1, 2, 4, 8, 16, 9, 18, 13, 3, 6, 12, 1]

The powers of 5 hit all 22 nonzero values before repeating, so Alice’s public value AA could be any of them. The powers of 2 repeat after 11 steps, so only 11 values are possible, and Eve’s search is cut in half.

Diffie–Hellman should use a generator, or at least a base whose powers repeat only after a very long time.

Diffie–Hellman Today

A version of Diffie–Hellman runs every time your browser opens an HTTPS connection: TLS 1.3, the current version of the protocol behind HTTPS, uses it to set up connections. Today it usually runs on elliptic curves (you’ll see these at the end of this chapter), but the idea is the same. Browsers and servers pick fresh secret exponents for every connection and delete them afterwards. That gives forward secrecy: even if someone steals a server’s long-term keys later, they can’t decrypt old conversations they recorded, because the secrets for those conversations no longer exist.

Diffie–Hellman gives Alice and Bob a shared number. It doesn’t send a message by itself. The next section, ElGamal Encryption, shows how to turn the same mathematics into a way to encrypt messages. And Diffie–Hellman has one big weakness that has nothing to do with the discrete log problem: it never checks who sent each public value. That’s the subject of Man-in-the-Middle Attacks.

Exercise for the Reader