History of Math

Diffie–Hellman Key Exchange

In 1976 Diffie and Hellman showed how two parties can agree on a shared secret over an open channel. This toy version uses p = 23 and g = 5: pick private keys a and b, publish A and B, and watch both sides compute the same shared secret.

7 minLevel 3/3The interactive stage stays ad-free.
Prime p23Generator g5A = g^a mod p8B = g^b mod p4Shared secret2
Alice — private key a = 6
a = 6
g^a mod p = 5^6 mod 23 = 8
public value A = 8
public channel
A = 8p = 23, g = 5B = 4
shared secret = 2 · match
Bob — private key b = 4
b = 4
g^b mod p = 5^4 mod 23 = 4
public value B = 4

Alice: B^a mod p = 4^6 mod 23 = 2 · Bob: A^b mod p = 8^4 mod 23 = 2 · both sides agree

Paint-mixing analogyAlice and Bob each pick a secret color. They mix it with a shared public color and send the mixed result over an open channel. An eavesdropper sees only muddy mixtures. Mixing is easy, but separating the original secret color back out is believed to be hard — that is the one-way idea behind Diffie–Hellman.
Toy numbers onlyWith p = 23 and g = 5 an attacker can try every exponent by brute force in an instant. Real Diffie–Hellman uses primes with hundreds or thousands of bits, so that the discrete logarithm is infeasible. This demo is for understanding the exchange, not for protecting data.

The public channel exposes p = 23, g = 5, A, and B, but never a or b. Alice raises B to her secret a; Bob raises A to his secret b. Modular exponentiation gives both the same value, because (g^b)^a = (g^a)^b = g^(ab) mod p. Change either private key and the shared secret changes for both sides at once, while an attacker would need to solve a discrete logarithm.

What you are seeing

1

Alice and Bob publicly agree on a prime p and a generator g. Alice keeps a secret exponent a and publishes A = g^a mod p.

2

Bob keeps b secret and publishes B = g^b mod p. Neither A nor B reveals the private exponent.

3

Alice computes B^a mod p and Bob computes A^b mod p. Both equal g^(ab) mod p, so they share a secret that never crossed the channel.

Why it matters

Diffie–Hellman was the first practical public-key key exchange and underpins TLS, SSH, and VPNs. It turned the ancient problem of sharing secrets into a computational hardness assumption.

Complexity & details

Each exponentiation uses O(log e) modular multiplications. The toy demo is trivial; real systems need primes large enough that discrete logarithms are infeasible.

Frequently asked questions

Why can an eavesdropper not just compute a or b?+

Recovering a from A = g^a mod p is the discrete logarithm problem. For large primes it is believed to be computationally infeasible, even though modular exponentiation itself is fast.

Is this demo secure?+

No. With p = 23 and g = 5, every possible exponent can be tried instantly. The demo exists to show the exchange, while real deployments use huge primes and authenticated key exchange.