← Nitesh Reddy · Today I learnedNext: DNS →
Today I learned · 9 Nov 2026

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.

~12 min readcryptographynetworking
TL;DR
Two people each keep one number private and send one number in public. Each mixes the other's public number with their own private one, and both land on the same secret. Someone watching the line sees both public numbers but cannot get the secret, because undoing the mix (the discrete logarithm) is too slow for big numbers.

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.

Paint mixing analogyAlice and Bob each add a secret colour to a shared public colour, swap the mixes, add their secret again and get the same final colour. Alice Bob public (Eve sees this) common paint (g) secret red (a) secret blue (b) yellow + red yellow + blue swap the mixes + my red = brown + my blue = brown same colour, never sent
Eve sees the yellow and both mixes. To get brown she must "un-mix" a secret colour out of one of them. That is the hard step.
Where the picture breaks
Real paint can be analysed in a lab. The real trick is not paint, it is clock arithmetic (next section), where "un-mixing" has no known fast method on normal computers.

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.

StepAlicePublic lineBob
1. Agree in publica big prime p and a base g (often fixed by a standard, like RFC 3526)
2. Pick a private numbera (random, kept secret)b (random, kept secret)
3. Compute public valueA = g^a mod pA →   ← BB = g^b mod p
4. Mix with own secrets = B^a mod ps = 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

RuleWhy
p big: 2048 bits or more512 and 1024 bits are now breakable (see Logjam below).
p a safe prime: p = 2q + 1 with q also primeThen 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 valueA 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 sourceA 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:

Man in the middle attackMallory does one key exchange with Alice and another with Bob, and relays messages, reading everything. Alicethinks: talking to Bob Mallorytwo keys Bobthinks: talking to Alice key 1 = g^(am) key 2 = g^(mb) decrypts with key 1, reads, re-encrypts with key 2
Both sides finish a "successful" key exchange. Both keys belong to Mallory.

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 valuea number g^a mod pa point: base point added to itself a times
Size of public value256 bytes for 2048-bit p32 bytes
Primea 2048+ bit safe prime2^255 − 19 (the "25519" in the name)
Baseg, often 2point with u = 9
Defined inRFC 3526, RFC 7919RFC 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 handshake with key shareBrowser sends its key share in ClientHello, server replies with its key share and a signature, both derive keys. BrowserServer ClientHello + key_share A (X25519) ServerHello + key_share B both compute shared secret both compute shared secret Certificate + CertificateVerify (signature) secret → HKDF key schedule → traffic keys
One round trip. The DH part gives the secret; the signature stops Mallory.

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

QuestionAnswer
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