Learning LabExplorable explanations
← All artifacts
Cryptography

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-hellmancryptographykey-exchangemodular-arithmeticdiscrete-log
LiveInteractive · drag, toggle, run it
Cryptography · Key Exchange

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.

Generator g
g = 5
g = 5 is a generator of the field: its powers cycle through every nonzero value mod 23, so every shared secret is reachable.
The exchange, one step at a time
Step 0 of 4. Agree on the public setup: a prime p and a generator g. Anyone may know these.
Alice
secret a (private)?
sends A (public)
shared secret?
Bob
secret b (private)?
sends B (public)
shared secret?
Private secrets stay home
Each sends one public number across the wire
Each raises the other's number to its own secret
Both land on the same secret
The paint analogy, in the same steps
Mixing paint is easy, separating a blend back into its ingredients is not. That one-way gap is exactly what the math leans on. Public mixtures travel openly; the private tints never do.
Common paint
public (= g, p)
Alice's tint
private (= a)
Alice sends
mixing
Bob's tint
private (= b)
Bob sends
mixing
Shared color
after swap
Alice stirs her tint into Bob's mixture, Bob stirs his into Alice's, and both reach the same three-paint blend (shown once you finish the steps). Eve saw the two public mixtures but cannot pull a private tint back out.
Eve, the eavesdropper
Eve has copied everything public: p = 23, g = 5, A = 8, and B = 19. To get the shared secret she needs one private exponent. Recovering a from A = ga mod p is the discrete logarithm, and the only move she has on a field this size is to try exponents one by one.
Eve's search space
21
exponents to try (p − 2)
Alice & Bob's work
~4
multiplications via square-and-multiply
Why the secret never travels

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.

Every value is computed with BigInt modular exponentiation; Eve's counter is a step-by-step discrete-log search over the field mod 23.