Introduction
Two students, Anika and Babul, want to agree on a secret key, but every message they send goes through a group chat the whole class can read. It sounds impossible: anything Anika writes, everyone sees. In 1976, Whitfield Diffie and Martin Hellman published a way to do exactly this. Each side keeps one secret number, they exchange two public numbers, and both end up with the same key, which never travelled at all.
Every HTTPS connection you open today starts with a key exchange built on this idea.
How it works
The usual picture uses paint. Anika and Babul agree, in public, on a common colour, say yellow. Each mixes in a secret colour of their own and sends the mixture. Each then adds their own secret colour to the mixture they received. Both end with yellow + Anika's colour + Babul's colour, the same shade. Someone watching sees yellow and the two mixtures, but un-mixing paint is hard.
With numbers, "mixing" is raising to a power modulo a prime:
- In public, they agree on a prime
pand a numberg. - Anika picks a secret
aand sendsA = gᵃ mod p. Babul picks a secretband sendsB = gᵇ mod p. - Anika computes
Bᵃ mod p. Babul computesAᵇ mod p. - Both equal
gᵃᵇ mod p: the shared key.
Going forwards (computing gᵃ mod p) is fast with repeated squaring. Going backwards (finding a from A) is the discrete logarithm problem, and for a prime of 2048 bits nobody knows a practical way to solve it.
// An eavesdropper sees p, g, A and B. With a tiny p she can try every secret until g^x mod p = A.
const p = 23, g = 5, A = 8, B = 19;
let tries = 0;
let power = 1;
let found = -1;
for (let x = 1; x < p; x++) {
tries++;
power = (power * g) % p;
if (power === A) {
found = x;
break;
}
}
console.log("found a = " + found + " after " + tries + " tries");
let key = 1;
for (let k = 0; k < found; k++) key = (key * B) % p;
console.log("so the shared key is " + key);The eavesdropper above succeeds only because p is 23: she tries every exponent until one matches. With a 2048-bit prime, the same loop would not finish before the Sun burns out.
Example
The textbook numbers: p = 23, g = 5, Anika's secret 6 and Babul's secret 15.
// Diffie-Hellman with tiny numbers: two people agree on a key over a channel everyone can read.
function modPow(base, exp, m) {
let result = 1;
base = base % m;
while (exp > 0) {
if (exp % 2 === 1) result = (result * base) % m;
base = (base * base) % m;
exp = Math.floor(exp / 2);
}
return result;
}
const p = 23, g = 5; // public: everyone knows these
const a = 6; // Anika's secret, never sent
const b = 15; // Babul's secret, never sent
const A = modPow(g, a, p); // Anika sends A
const B = modPow(g, b, p); // Babul sends B
console.log("sent in the open: A = " + A + ", B = " + B);
console.log("Anika computes B^a mod p = " + modPow(B, a, p));
console.log("Babul computes A^b mod p = " + modPow(A, b, p));Line by line
pandgare public: anyone may know them.- Anika computes
A = 5⁶ mod 23 = 15625 mod 23 = 8and sends it. Babul computesB = 5¹⁵ mod 23 = 19and sends it. - Anika raises what she received to her secret:
19⁶ mod 23 = 2. - Babul does the same with his:
8¹⁵ mod 23 = 2. - Both have 2, and the numbers 6, 15 and 2 never appeared in the chat.
Expected result
sent in the open: A = 8, B = 19
Anika computes B^a mod p = 2
Babul computes A^b mod p = 2
There is a catch. Diffie-Hellman alone does not tell Anika that B really came from Babul. An attacker in the middle can swap in her own numbers and run one exchange with each side, reading everything in between. This man-in-the-middle attack is stopped by signing the exchanged numbers, which needs the signatures and certificates of the next lesson.
In the real world
- TLS 1.3, the protocol behind HTTPS, uses a Diffie-Hellman exchange (usually on an elliptic curve, called ECDHE) in every full handshake to agree on session keys.
- Forward secrecy: if each connection uses fresh secret numbers that are then thrown away, stealing a server's long-term key later does not unlock past conversations.
- Messaging apps run Diffie-Hellman between devices so that the company in the middle never has the keys.
- The same "easy forwards, hard backwards" idea is called a one-way function, and much of cryptography is built on such functions.
Common mistakes
- Using small or badly chosen numbers. Small primes fall to the brute force shown above; some special primes have known shortcuts.
- Skipping authentication. An unauthenticated exchange is open to a man in the middle.
- Reusing the same secret for every connection. Then one stolen secret unlocks every past session.
- Thinking the shared key is sent. It is computed on each side; only
AandBtravel.
Best practices
- Use the key exchange built into TLS or a well-known library, never a hand-made one.
- Always authenticate the exchange, with certificates or signatures.
- Use fresh secrets per session (ephemeral Diffie-Hellman) for forward secrecy.
- Prefer standard elliptic-curve groups, which give strong security with small numbers.
Summary
Diffie-Hellman lets two sides agree on a secret key over a public channel. Each keeps a secret exponent, sends g raised to it modulo p, and raises what it receives to its own secret, so both reach gᵃᵇ mod p. Its security rests on the discrete logarithm problem, which is hopeless to solve for large primes. On its own it does not prove who is on the other end, so real protocols sign the exchange to stop a man in the middle.
Quick check
Key takeaways
- Only p, g, A and B travel; the shared key gᵃᵇ mod p is computed on each side.
- Security comes from the discrete logarithm problem, so real primes are thousands of bits long.
- An unauthenticated exchange is open to a man in the middle.
- Fresh secrets per session give forward secrecy.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.