Two people who have never met, talking on a line that everyone can hear, can still end up holding the same secret number. Nobody listening can work it out. It sounds impossible, and for most of history it was assumed to be. It is the trick that starts almost every secure connection on the internet today.
Imagine Alice and Bob agree out loud on a common colour of paint, say yellow. Each secretly picks a private colour, and mixes it with the yellow. They swap the mixtures in public. Then each adds their own private colour to the mixture they received. Both end up with yellow plus Alice's colour plus Bob's colour, so the same final shade. An eavesdropper saw yellow and both mixtures, but cannot unmix paint to pull out the private colours.
Numbers do the same job. The "mixing" that is easy to do and very hard to undo is modular exponentiation.
Pick a prime p and a base g. Raise g to a power and keep only the remainder after dividing by p. With p = 23 and g = 5: 51 = 5, 52 = 25 → 2, 53 = 10, 54 = 50 → 4, and so on. The remainders jump around with no visible pattern.
Going forward is quick, even for huge numbers, by repeated squaring. Going backward is the problem: given the answer A, find the exponent a with ga mod p = A. This is the discrete logarithm problem. For well-chosen large primes, no efficient method is known on ordinary computers.
Both equal gab mod p, because exponents multiply the same way in either order. That shared number becomes the key for a fast ordinary cipher. Try it, then play the eavesdropper.
A real exchange uses a prime of 2048 bits or more (617 digits), giving roughly 10616 possible secrets. Here Eve needs at most p − 1 guesses, so she wins every time. That gap is the whole point.
Slide Alice's secret and the public number A jumps unpredictably, yet the shared key always matches on both sides. Switch to the larger prime and Eve's search takes visibly longer. Each step up in prime size multiplies her work, while Alice and Bob's work barely changes. Their cost grows slowly with the size of the numbers; Eve's, for a brute-force search, grows with the number itself.
Eve has smarter methods than guessing one by one, and they do help. That is why real systems use primes of 2048 bits or more, or the same idea on elliptic curves, rather than 3 or 4 digits.
Whitfield Diffie and Martin Hellman published the idea in 1976 in the paper "New Directions in Cryptography", and Hellman has said Ralph Merkle's work on public-key puzzles was part of the story. In 1997 the British signals agency GCHQ revealed that Malcolm Williamson had found an equivalent method in 1974 and that it had stayed classified.
The exchange hides the key from listeners, but it does not tell Alice who she is talking to. If Mallory sits in the middle and runs one exchange with Alice and another with Bob, both think they have a private channel. Real protocols such as TLS pair the exchange with certificates and signatures so each side can check who is at the other end. Modern TLS 1.3 also uses a fresh exchange for every connection, so a stolen key from today does not unlock yesterday's traffic.
The other threat is a large quantum computer. Shor's algorithm would solve the discrete logarithm problem efficiently, which is why browsers and servers have begun adding post-quantum key exchange alongside the classic kind. The paint trick remains a lovely idea: a shared secret from nothing but public numbers and a one-way street.