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.
Alice: B^a mod p = 4^6 mod 23 = 2 · Bob: A^b mod p = 8^4 mod 23 = 2 · both sides agree
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
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.
Bob keeps b secret and publishes B = g^b mod p. Neither A nor B reveals the private exponent.
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.