The Key Distribution Problem
Before 1976, secure communication required a shared secret key — but how do you share a secret securely without already having a secure channel? This chicken-and-egg problem plagued cryptography for centuries. Governments used diplomatic pouches; militaries used trusted couriers. For the nascent internet, these methods were impossible. Whitfield Diffie and Martin Hellman's breakthrough was showing that two strangers could agree on a shared secret over a completely public channel, in full view of eavesdroppers, and the eavesdroppers could not determine the secret.
How the Exchange Works
Alice and Bob publicly agree on a prime p and a generator g. Alice picks a secret number a and computes A = g^a mod p, sending A publicly. Bob picks a secret b and computes B = g^b mod p, sending B publicly. Alice then computes S = B^a mod p. Bob computes S = A^b mod p. Remarkably, both get the same value: g^(ab) mod p. An eavesdropper who sees g, p, A, and B cannot efficiently compute g^(ab) mod p without knowing either a or b — this is the Discrete Logarithm Problem, believed to be computationally intractable for large primes.
The Discrete Logarithm Problem
Given g^a mod p, finding a is called the discrete logarithm. For small numbers (as in this simulation), you can try all possibilities. But for a 2048-bit prime p, the number of possibilities is approximately 2^2048 — more than the number of atoms in the observable universe multiplied by itself several times. The best known classical algorithms (Number Field Sieve for Discrete Logarithm) are sub-exponential but still infeasible at this scale. This mathematical asymmetry — easy to compute, hard to reverse — is the foundation of Diffie-Hellman security.
Modern Usage and Forward Secrecy
Every time you see the padlock icon in your browser, Diffie-Hellman (or its elliptic curve variant ECDH) is at work. Modern TLS uses Ephemeral Diffie-Hellman (DHE or ECDHE), where fresh random values are generated for each session. This provides forward secrecy: even if a server's long-term private key is later compromised, past session keys cannot be recovered. The protocol was declassified in 1997 when GCHQ revealed that James Ellis and Clifford Cocks had independently discovered public-key cryptography at British intelligence in 1969 — seven years before Diffie-Hellman's publication.