Here's a strange question: can two people agree on a secret number while a third person listens to every single word they say?
It sounds impossible. If Alice and Bob don't already share a code, and everything they say to each other is public, what's stopping the eavesdropper from just picking up the same secret they do?
The answer is yes, they can, and it happens every time you load a website starting with https. It doesn't come from hiding information. It comes from a strange corner of number theory where some operations are easy to run forward and, practically speaking, impossible to run backward.
A clock is just numbers that reset themselves
Start with something you already know: a 12-hour clock. If it's 9 o'clock and 5 hours pass, the clock doesn't say 14. It says 2. The number wrapped around.
That wrapping has a name: modular arithmetic. We write "9 + 5 wraps to 2 on a 12-clock" as:
The symbol just means "gives the same clock position as." Nothing about this is exotic. You do it every day without calling it math.
Keep adding the step. The number never stops growing, but its position on the clock only ever revisits these 12 spots.
Keep hitting the button. Notice the raw total keeps climbing forever, but the dot on the clock only ever lands on one of 12 spots. That's the entire idea behind "mod": it turns an infinite number line into a small, finite loop. Cryptography lives almost entirely inside loops like this one, just with a clock that has hundreds of digits' worth of positions instead of 12.
Not every number sees the whole clock
Now swap addition for something more interesting: repeated multiplication. Instead of adding a step over and over, multiply by a fixed base over and over, always keeping the answer inside a clock of size .
Here's the part that isn't obvious until you watch it happen: some choices of visit every single position on the clock before they loop back to the start. Others get stuck cycling through a tiny handful of positions, no matter how long you run them.
Watch the powers of g march around the clock (mod 13). Some bases visit every position before looping. Others get stuck in a short loop.
Slide around. Some values sweep the entire clock face. Others trace out a small closed loop and never touch most of the numbers. The number of steps it takes for to first loop back to 1 is called its order. A base whose order covers the whole clock is called a generator, or a primitive root. Generators matter because the whole clock being "in play" is what makes a search over it genuinely hard. A small loop is a small haystack.
Climbing up is easy. Climbing back down is not.
Here's the one-way street that makes all of this useful. Given , , and an exponent , computing is fast. Computers do it with a trick called repeated squaring: square the running total, and occasionally multiply in one more copy of , once per bit of . A handful of squarings gets you there even when is enormous.
Now flip the question. Given , , and the result , find . This is called the discrete logarithm problem, and nobody knows a fast way to solve it in general. The obvious approach is to just try exponents one at a time until one matches.
Same modulus, two directions. Computing the power finishes almost instantly. Searching for the exponent barely gets going. Bars use a log scale.
Slide the modulus up and hit race. The forward bar barely has to move: doubling the size of costs it one extra squaring. The backward bar, brute-forcing its way through possible exponents, has to cover a search space that grows with itself, and grows exponentially with its digit count. Push up to the primes real cryptography uses, hundreds of digits long, and that gap stops being "hard" and starts being "not happening this century, on any computer we know how to build."
That asymmetry, cheap forward, brutal backward, is the raw material every protocol in this post is built from.
Two strangers, one secret, and a line everyone can hear
Now we can answer the opening question. This protocol is called Diffie-Hellman key exchange, and it lets Alice and Bob land on the same secret number without ever sending that number to each other.
Everyone, including any eavesdropper, agrees to use a public base and a public clock size . Alice picks a secret number that she never tells anyone, and sends Bob . Bob picks his own secret , and sends Alice .
Here's the trick. Alice takes what Bob sent and raises it to her own secret power: . Bob does the mirror move: . Because exponents multiply the same way no matter what order you apply them, and land on exactly the same number:
Neither of them ever typed into a message. They each arrived at it separately, using a number the other person never revealed.
Alice and Bob never send their secret numbers, or the shared key itself, over the line. They each compute it independently and land on the same value.
Slide Alice's secret and watch both sides always land on the same shared value. Now look at what's sitting in the public column: , , , and . That's everything an eavesdropper, traditionally named Eve, ever gets to see. And hey, notice what recovering the shared secret from just those four numbers requires: solving for or from . That's the discrete logarithm problem from the last section, the one whose backward bar barely moved.
What the eavesdropper actually sees
It's worth being precise about this, because the security here doesn't come from Eve missing some piece of the conversation. She sees every message. She sees and from the start. She watches and cross the wire in plain sight.
What she's missing is a fast way to turn back into . With the small numbers in the demo above, brute force cracks it in a blink. With a real 2048-bit , the same search would take longer than the sun has left to burn, using every computer humanity has ever built.
So the secret doesn't hide in a locked box. It hides in a computation that's easy to walk forward and, for all practical purposes, impossible to walk backward. That's the entire trick, dressed up in prime numbers big enough to matter.
The short version
Modular arithmetic turns numbers into a clock that resets itself. Some bases, run through repeated multiplication on that clock, visit every position before repeating, and those are the generators worth building a protocol on. Computing a power on that clock is fast. Reversing it, the discrete logarithm problem, has no known fast solution. Diffie-Hellman turns that one gap into a way for two strangers to agree on a shared secret while broadcasting every other step of the process in public.
Your browser runs a version of this exchange, with primes hundreds of digits long, every time you open a padlocked website. Same clock. Same trick. Just numbers too big for anyone to climb back down.
All visualizations are interactive React components running entirely in your browser. The generator orbits and Diffie-Hellman exchange use exact modular exponentiation on the numbers shown, not simulated results. No libraries beyond React.