Diffie-Hellman Key Exchange
Two strangers end up holding the same secret number, yet that number is never sent across the wire. Watch them build it in the open while an eavesdropper reads every message and still cannot follow.
Diffie-Hellman Key Exchange
Alice and Bob have never met and share no password, yet they need one secret number that only the two of them know. Everything they say travels over a wire Eve is listening to. The trick is that each keeps a private number, folds it into a public number they are happy to shout, and after one swap both can compute the same secret. Eve hears every public number and still cannot reconstruct it. Step through it and watch where the secret comes from.
Public versus private. The prime p, the generator g, and the two transmitted numbers A and B are all public. The two exponents a and b are the only secrets, and neither one is ever sent. What crosses the wire is g raised to a private power, which tells you nothing about the power itself.
Why both sides match. Alice computes Ba = (gb)a and Bob computes Ab = (ga)b. Exponents multiply the same way in either order, so both equal gab mod p. The shared secret is built from both private numbers, yet each side supplies only its own.
The asymmetry that protects it. Going forward is cheap: ga mod p takes about as many multiplications as p has digits, using square-and-multiply. Going backward, finding a from ga mod p, is the discrete logarithm, and the best known methods still scale roughly with the size of the field. Add one digit to p and Alice's work barely moves while Eve's search grows by a whole factor.
What real systems use. The toy primes here top out at a few digits so every number fits on screen. Production Diffie-Hellman uses primes of 2048 bits or more, or switches to elliptic curves where the same one-way trick holds with much smaller keys. The brute force you watched stall on a five-digit prime becomes astronomically out of reach.