Suppose two people want to send messages that no one else can read. The obvious approach is encryption. Scramble the message with a key, and only someone with the same key can unscramble it. But that raises an immediate question. How do they agree on a key without anyone else seeing it?
Before 1976, the only answer was to meet in person and exchange keys physically. That works at small scale. It does not work for the internet, where millions of strangers need secure sessions without ever having met.
Whitfield Diffie and Martin Hellman answered it in 1976, in a paper called “New Directions in Cryptography”. Their key exchange lets two parties derive a shared secret purely through public messages. Every HTTPS connection today descends from that idea.
# One-way operations
The whole protocol depends on one idea. Some operations are easy to do and hard to reverse. Computing (raise to the power , divide by , keep the remainder) takes a fraction of a second. But given only the result, recovering is believed to be infeasible for large enough numbers. There is no known fast way to run it backwards. This operation is called modular exponentiation, and that asymmetry is the only thing DH needs.
# Parameters
The exchange uses two public parameters, a prime and a generator .
is a large prime. All calculations happen mod , meaning results are taken as remainders after division by . Every value stays in the range through . In practice, is chosen as a safe prime of the form , where is also prime. If factors into small primes, an attack called Pohlig-Hellman can solve the discrete logarithm cheaply by factoring the problem into small pieces and combining them. Making with prime leaves only the large factor to attack.
is an integer whose powers cycle through much of that range. Raise to successive powers mod and you visit distinct values before looping back to . The length of that cycle is the order of . A short cycle makes recovering a secret cheap regardless of how large is, so is chosen to generate the subgroup of order , giving its cycle exactly elements. The brute-force cost of recovering a secret from this subgroup is on the order of operations. When is thousands of bits long, that number is out of reach.
Both are agreed on in advance and sent in the clear. An attacker knows and too. The security comes not from hiding them but from the gap between what is easy to compute and what is hard to reverse.
The 2 parties, Alice and Bob in our case, pick their secret keys and respectively.
Parameters
Alice’s and Bob’s secret values are hidden by default to reflect that they stay private during the exchange. Click the eye icon to reveal them and watch how each secret feeds into the computations below.
# The exchange
They don’t exchange their secret keys. Each uses their secret to compute a public value from and , and sends only that result across the channel.
Alice sends
Bob sends
# Why it works
Bob’s public value is . When Alice raises that to , she gets , which equals . Alice’s public value is . When Bob raises that to , he gets , which also equals .
The eavesdropper saw , , and . To find the shared secret, they need to recover from or from . That is the discrete logarithm problem. For small numbers, you try every possibility until something matches. For a 2048-bit prime, the search space exceeds the number of atoms in the observable universe. No computer can exhaust it.
The best known classical algorithms, like the General Number Field Sieve, do better than brute force, but not enough to matter at proper parameter sizes. This is why implementations use primes of at least 2048 bits.
Quantum computers break this. Shor’s algorithm solves the discrete logarithm problem efficiently, so a sufficiently powerful quantum computer breaks standard DH outright. Modern TLS increasingly favors elliptic curve Diffie-Hellman (ECDHE), which offers stronger security per bit even against classical attackers, and post-quantum cryptography exists precisely because Shor’s threat is real.