Diffie-Hellman Key Exchange
How two people can agree on a shared secret key over a completely public channel — even with an eavesdropper listening to every message — using only modular exponentiation.
0 Why Do We Need Diffie-Hellman?
RSA lets you encrypt with a public key and decrypt with a private one. But what if Alice and Bob want to establish a shared symmetric key for fast AES encryption — without ever having met?
RSA solves…
Sending encrypted messages to someone using their public key. One-directional. Computationally heavy for large data.
See RSA e2e demo →DH solves…
Agreeing on a shared secret that neither party ever transmits. Used to bootstrap symmetric encryption (e.g., AES) in TLS, SSH, Signal.
1 The Paint-Mixing Analogy
DH is famously explained with mixing paint. Mixing colors is easy — unmixing is hard. This mirrors modular exponentiation: easy forward, hard backward.
Step 1 — Agree on a shared base color (public)
Alice and Bob publicly agree on a starting color. Eve knows it too.
(public)
Step 2 — Alice adds her secret color
(secret)
(public!)
A = ga mod p where a is Alice's secret.Step 2 — Bob adds his secret color
(secret)
(public!)
B = gb mod p where b is Bob's secret.Step 3 — Both add their own secret to the OTHER's mix
2 Step 1: Agree on Public Parameters
Alice and Bob (and Eve — she knows these too) agree on two public numbers: a large prime p and a generator g.
Public parameters — everyone knows these
Show powers of g mod p (verify it's a generator)
3 Alice & 3 Bob Pick Private Secrets
Each person independently chooses a secret exponent. They never transmit or reveal this number.
Alice's secret: a
Known only to Alice. Random integer in [2, p−2].
Bob's secret: b
Known only to Bob. Random integer in [2, p−2].
4 Compute & Exchange Public Values
Each party computes their "partial result" and sends it across the channel. Eve intercepts both — but can't do anything useful with them.
6 What Eve Sees
Eve intercepts every message on the channel. Let's audit exactly what she knows versus what she'd need.
Eve knows A = ga mod p and needs to find a. This is the Discrete Logarithm Problem: given g, A, p — find a. No efficient classical algorithm exists for large p. Addressed in Section 8.
7 Interactive Demo
Set all four parameters and watch the full exchange. Try small numbers you can verify by hand, then try larger ones.
Parameters
8 Why Eve Can't Break It: The Discrete Log Problem
The security of DH rests on one assumption: given g, A = ga mod p, and p — finding a is computationally infeasible for large p.
Contrast: Easy vs. Hard
Hard (discrete log): g=5, A=8, p=23 → find a? (easy for small p, infeasible for large p)
At scale (2048-bit p): State-of-the-art algorithms still take >1024 operations
Try: Brute-Force the Discrete Log
For small p, Eve CAN brute-force — just try all possible a until ga mod p = A. This demo shows the exponential search:
- RSA security ← Integer Factorization Problem (factor n = p·q) →
- DH security ← Discrete Logarithm Problem (find a from ga mod p)
- ECDH (Elliptic-Curve DH) ← Discrete log on elliptic curves (even harder, smaller key sizes)