Sharing a secret without sharing the secret
Diffie–Hellman key exchange, from the paint-mixing picture down to the maths, the attacks, and the exact place it sits inside every HTTPS connection you open.
- Easy one way:
g^a mod ptakes microseconds. Hard the other way: findingaback takes longer than the age of the universe for good sizes. - On its own it has no identity check. A man in the middle can fool both sides. TLS fixes this with a signature from a certificate.
- Today it is mostly X25519 (elliptic curve version), with fresh keys per connection, so a stolen server key later cannot open old traffic.
01 · THE PROBLEMEverybody can hear you
To encrypt a chat, both sides need the same key (a long random number). But the only line between them is the internet, and anyone on the path (Wi-Fi owner, ISP, router) can read every packet. If Alice sends the key, the listener (called Eve, short for eavesdropper) now has it too.
Before 1976 the answer was: meet first, or send the key by a trusted courier. Whitfield Diffie and Martin Hellman's paper New Directions in Cryptography showed a third way: build the key together, in public, so it is never sent at all.
02 · THE PICTUREMixing paint
The classic way to picture it. Mixing two paints is easy. Splitting a mixed paint back into its two colours is practically impossible.
03 · THE REAL MATHSClock arithmetic
mod p means "keep the remainder after dividing by p". It is a clock with p hours: after p−1 you wrap back to 0. So 17 mod 12 = 5, like 17:00 being 5 o'clock.
| Step | Alice | Public line | Bob |
|---|---|---|---|
| 1. Agree in public | a big prime p and a base g (often fixed by a standard, like RFC 3526) | ||
| 2. Pick a private number | a (random, kept secret) | b (random, kept secret) | |
| 3. Compute public value | A = g^a mod p | A → ← B | B = g^b mod p |
| 4. Mix with own secret | s = B^a mod p | s = A^b mod p | |
Why both get the same s: B^a = (g^b)^a = g^(ab) and A^b = (g^a)^b = g^(ab). Powers multiply, and order does not matter. Taking mod p at every step does not change the final remainder.
What Eve has: p, g, A, B. To get s she needs a or b, meaning: "which power of g gives A on this clock?" That question is the discrete logarithm problem. On a clock the powers jump around with no pattern, so there is no "getting warmer".
Try it: a live calculator
Change the numbers. Small primes are only for learning; Eve's brute force below cracks them instantly.
Why going forward is fast
Computing g^a mod p with a 2048-bit a does not multiply g by itself a times. It uses square-and-multiply: read a in binary, square at each bit, multiply when the bit is 1. That is about 2048 squarings, not 22048 steps:
function modpow(g, a, p) { // all BigInt
let r = 1n; g %= p;
while (a > 0n) {
if (a & 1n) r = r * g % p; // bit is 1: multiply
g = g * g % p; // square for the next bit
a >>= 1n;
}
return r;
}
Going backward has no such shortcut. The best known methods for big primes (the number field sieve) are far faster than trying every number, but still out of reach at 2048 bits.
04 · CHOOSING p AND gNot any numbers will do
| Rule | Why |
|---|---|
p big: 2048 bits or more | 512 and 1024 bits are now breakable (see Logjam below). |
p a safe prime: p = 2q + 1 with q also prime | Then powers of g cover a huge set. If they only covered a small set (a small subgroup), Eve could search it quickly. |
| Check the other side's value | A value like 1 or p−1 forces the secret into a tiny set. Good code rejects them. |
Fresh a, b each time, from a real random source | A guessable private number breaks everything, no matter how big p is. |
05 · THE WEAK SPOTThe man in the middle
Diffie–Hellman proves "I share a key with whoever sent this number". It does not prove who that was. An attacker who can change packets (call him Mallory) can sit in the middle:
The fix is authentication: one side signs its public value with a long-term key that the other side can check. In HTTPS the server signs the handshake with the private key of its certificate, and the browser checks the certificate chain up to a trusted authority. Diffie–Hellman gives secrecy; the signature gives identity. You need both.
06 · TODAYElliptic curves and X25519
The same idea works on any group where "repeat an operation a times" is easy and "find a" is hard. On an elliptic curve (a set of points with a special "add two points" rule) the hard problem is much harder per bit, so keys are far smaller.
| Classic DH (finite field) | X25519 (elliptic curve) | |
|---|---|---|
| Public value | a number g^a mod p | a point: base point added to itself a times |
| Size of public value | 256 bytes for 2048-bit p | 32 bytes |
| Prime | a 2048+ bit safe prime | 2^255 − 19 (the "25519" in the name) |
| Base | g, often 2 | point with u = 9 |
| Defined in | RFC 3526, RFC 7919 | RFC 7748 |
RFC 7748 also says code may check whether the shared secret came out all zeros and abort, because that happens when the other side sends a deliberately bad point.
Where it sits in HTTPS (TLS 1.3)
- TLS 1.3 only allows (EC)DHE for key exchange: the E means ephemeral, a fresh private number per connection (RFC 8446). The older "encrypt the key with the server's RSA key" mode was removed.
- That gives forward secrecy: if the server's certificate key is stolen next year, recorded old traffic still cannot be opened, because the ephemeral
aandbwere thrown away. - The raw shared secret is never used as the key directly. It goes through HKDF (a function that stretches and mixes it) to make separate keys for each direction.
See it yourself: openssl s_client -connect nitesh.lol:443 -tls1_3 printed Peer Temp Key: X25519, 253 bits when I ran it on 10 Nov 2026.
07 · WHEN IT WENT WRONGLogjam, 2015
Researchers found that many servers still accepted 512-bit "export grade" DH, left over from 1990s US export rules. An attacker could downgrade a connection to it and break it. Per weakdh.org, 8.4% of the top 1 million domains were vulnerable at first.
The deeper lesson: the slow part of breaking DH depends only on the prime, not on each connection. Many servers shared the same few 1024-bit primes, so one big precomputation could unlock many of them. The paper estimated that breaking one common 1024-bit prime would allow passive listening to 18% of the top million HTTPS domains. Hence: 2048+ bits, or elliptic curves.
08 · WHAT'S NEXTQuantum computers
A big enough quantum computer running Shor's algorithm could solve discrete logs fast, breaking both classic DH and X25519. None exists yet, but traffic recorded today could be opened later ("harvest now, decrypt later"). So browsers now send a hybrid key share, X25519MLKEM768: classic X25519 plus ML-KEM (a NIST post-quantum method). The final secret mixes both, so it is safe if either one holds. Chrome turned it on by default in version 131.
09 · CHEAT SHEETWhat to remember
| Question | Answer |
|---|---|
| What is public? | p, g, A, B |
| What is never sent? | a, b, and the shared secret s |
| Why is it safe? | Discrete log is hard for big, well-chosen groups |
| Does it prove identity? | No. Add signatures / certificates |
| What does the web use? | Ephemeral X25519 in TLS 1.3, now often hybrid with ML-KEM |
| Is it encryption? | No. It only agrees on a key. AES or ChaCha20 then encrypts the data |
SOURCESWhere these facts come from
- W. Diffie, M. Hellman, New Directions in Cryptography, IEEE Trans. Information Theory, 1976
- RFC 7748: X25519, prime
2^255−19, 32-byte keys, baseu=9, all-zero check (section 6.1) - RFC 8446: TLS 1.3, key_share, (EC)DHE only, HKDF key schedule
- RFC 3526 and RFC 7919: standard finite-field DH groups
- weakdh.org: Logjam,
8.4%and18%figures - BleepingComputer: Chrome 131 switches to ML-KEM hybrid