Asymmetric Cryptography · 3.4

🤝 Diffie-Hellman Key Exchange

Agreeing on a secret key in public — the magic trick of cryptography⏱ ~2 min

🎨DH = mixing paint colors

Alice and Bob both start with a public yellow. Alice secretly picks red and mixes it in → sends orange to Bob. Bob secretly picks blue and mixes in → sends green to Alice. Alice adds her red to Bob's green → gets brown. Bob adds his blue to Alice's orange → gets the same brown. An eavesdropper sees yellow, orange, and green — but cannot separate the secret colors to reconstruct brown. This is the intuition behind Diffie-Hellman.

Both start with the same PUBLIC yellow 🟡👩 Alice🟡 + 🔴secret red🟠sends orange →👨 Bob🟡 + 🔵secret blue🟢← sends green👁 eavesdropper sees these — but can't un-mix paint!🟢 + 🔴 = 🟤🟠 + 🔵 = 🟤Same brown = shared secret! 🤝
The paint-mixing intuition — mixing is easy, un-mixing is impossible

The Math: Discrete Logarithm Problem

math
Public (known to everyone): prime p, generator g
Alice chooses secret a, computes: A = g^a mod p → sends A to Bob
Bob chooses secret b, computes: B = g^b mod p → sends B to Alice
Alice computes: B^a mod p = (g^b)^a mod p = g^(ab) mod p
Bob computes: A^b mod p = (g^a)^b mod p = g^(ab) mod p
Both reach the same shared secret: g^(ab) mod p
Eavesdropper sees g, p, A=g^a, B=g^b — cannot find ab without solving discrete log

Forward Secrecy — The Key Advantage of Ephemeral DH

🔒 SecurityIf Alice and Bob generate new DH keys for every session (ephemeral keys), then compromising a long-term private key later does NOT expose past sessions. Each session's shared secret existed only in memory, never stored. This property is called Perfect Forward Secrecy (PFS) and is mandatory in TLS 1.3. Without PFS, an adversary who records encrypted traffic today and steals the server's private key later can decrypt all stored traffic retroactively.

Modern Variants

VariantBased OnUsed In
DHE (Ephemeral DH)Classic DH with ephemeral keysTLS 1.2 (being phased out)
ECDHE (Ephemeral ECDH)Elliptic curve DH with ephemeral keysTLS 1.3 (mandatory), modern HTTPS
X25519ECDH on Curve25519WireGuard, Signal, modern TLS — fastest and safest
💡 TipTLS 1.3 removed all non-ephemeral key exchange methods. Every TLS 1.3 connection uses ECDHE — Forward Secrecy is no longer optional. If you see a TLS 1.2 connection using RSA key exchange (not DHE), that connection has no Forward Secrecy.
🧠Quick Checkfirst try = +5 XP

Perfect Forward Secrecy means…

0 XP🔥 0 days