06 / 11 · Modern cryptography
Diffie–Hellman key exchange
Two strangers agree on a secret while everyone is listening.
- Published
- 1976, Diffie & Hellman
- Hard problem
- Discrete logarithm
- Used in
- TLS, SSH, Signal, VPNs
Before 1976, two parties had to meet, or trust a courier, to share a secret key. Whitfield Diffie and Martin Hellman showed how to create a shared secret over a public channel, which is the idea that made the secure web possible.
Each side keeps a private number, publishes a value derived from it, and combines the other side's public value with its own secret. Both arrive at the same result; an eavesdropper who saw every message cannot.
01 / 14 · Inside Diffie–Hellman
Agree on a secret, in public
Alice and Bob need a shared key: a secret number that locks and unlocks their messages. Everything they send crosses a channel Eve can read. If Alice just sends a key, Eve copies it.
A key sent in the clear is a key Eve has too. They need to build a key without ever sending it.
Playground
Agree on a key in public, with your own numbers
Pick a prime p, a generator g and two secrets. Step through every modular exponentiation, watch both sides land on the same key, and see what an eavesdropper can do with what she sees.
Your input
A prime below 1 000 000 keeps every step readable.
- A = gᵃ
- 8
- B = gᵇ
- 19
- Key
- 2
g reaches 22 of the 22 non-zero values
p, g
Public parameters: p and g
What happens
Alice and Bob agree, in the open, on a prime p, the modulus (every result is the remainder after dividing by p), and a generator g, a number whose powers g¹, g², g³… mod p run through many different values before coming back to 1.
On your values
p = 23: ÷ 2, 3 → prime
g = 5: g22 mod 23 = 1 → 22 values
Why it matters
None of this is secret. What matters is how many distinct values the powers of g reach: that is the number of possible keys, so a g with few powers leaves Eve few keys to try.
In paintThey agree publicly on a common color. Eve sees it too.
Start
← → to step · space to playp, g
2048 bits
The same exchange at real size
RFC 3526 group 14: a 2048-bit prime (617 digits), generator 2, the group TLS and SSH used before elliptic curves. Fresh random secrets, computed in your browser.
3231700607…5361090559 (617 digits)
32317006071311007300338913926423828248817941241140239112842009751400741706634354222619689417363569347117901737909704191754605873209195028853758986185622153212175412514901774520270235796078236248884246189477587641105928646099411723245426622522193230540919037680524235519125679715870117001058055877651038861847280257976054903569732561526167081339361799541336476559160368317896729073178384589680639671900977202194168647225871031411336429319536193471636533209717077448227988588565369208645296636077250268955505928362751121174096972998068410554359584866583291642136218231078990999448652468262416972035911852507045361090559
How it works
- 1Everyone agrees on a large prime p and a generator g. These are public.
- 2Alice picks a secret a and sends A = gᵃ mod p. Bob picks a secret b and sends B = gᵇ mod p.
- 3Alice computes Bᵃ mod p; Bob computes Aᵇ mod p.
- 4Both equal gᵃᵇ mod p, the shared secret, which is then fed into a key derivation function to make AES keys.
Why it holds
- One-way function
- Computing gᵃ mod p takes a few thousand multiplications, even for 2048-bit numbers. Recovering a from gᵃ mod p, the discrete logarithm, has no known efficient algorithm on classical computers.
- Scale
- With a 2048-bit prime the best known attack (the number field sieve) needs around 2¹¹² operations. The elliptic-curve version (X25519) reaches 128-bit security with 256-bit numbers.
- Forward secrecy
- Fresh secrets are generated for each session and then thrown away. Stealing a server's long-term key later does not unlock past conversations.
What can go wrong
- Man in the middle
- Plain Diffie–Hellman doesn't say who you are talking to. An active attacker can run one exchange with each side. TLS prevents this by signing the exchange with a certificate.
- Quantum computers
- Shor's algorithm solves discrete logarithms efficiently. That's why TLS is moving to hybrid key exchange with ML-KEM, a post-quantum algorithm standardized in 2024.