This demonstration shows how two people (Alice and Bob) can use the Diffie-Hellman key exchange protocol to securely create a shared secret over an insecure channel. This shared secret can then be used for encrypted communication.
Imagine Alice and Bob each have a secret color of paint that only they know. They also agree on a common color that everyone can see. Each person mixes their secret color with the common color, then they swap mixtures. Now each person mixes in their own secret color again. The result? They both end up with the same final color — but an eavesdropper who saw only the swapped mixtures cannot figure out what that final color is. Diffie-Hellman works the same way, but with numbers and math instead of paint.
Every time you see the lock icon in your browser's address bar, HTTPS is using a key exchange very similar to this one. It is how your browser and a website agree on a secret key to protect your passwords, messages, and credit card numbers — all without ever sending that secret key where someone could steal it.
The process involves two public numbers: a public base (g) and a public modulus (p), which are numbers both people agree upon (in the open — it is okay if other people know them), and private numbers chosen separately by Alice (a) and Bob (b). These private numbers are never shared with anyone. To be clear, Bob does NOT know Alice's secret number, and Alice does not know Bob's. In real life, all of these numbers are very, very big.
Use the defaults, or enter your own values for the public base (g), public modulus (p), and private numbers for Alice (a) and Bob (b). (p) MUST be a prime number.
After performing the key exchange (button below), the results will show:
Here's what happens behind the scenes:
What does "mod" mean? "Mod" means "remainder after division." For example, 17 mod 5 = 2 because 17 ÷ 5 = 3 with a remainder of 2. Think of a clock: after 12 comes 1 again, not 13.
The security of the Diffie-Hellman key exchange relies on the difficulty of solving the discrete logarithm problem, which makes it computationally infeasible (aka almost impossible) for an eavesdropper to determine the shared secret, even if they know g, p, A, and B.
In real-world applications of Diffie-Hellman, the numbers used are much larger, for security reasons:
See also: Public-Private Key Pair Generation — how RSA creates the public and private keys used for encryption and digital signatures.
Or step back to the classical methods that came first: Traditional Cryptography · Early Modern Cryptanalysis.
Disclaimer: these pages are educational demos provided as-is, with no warranty of any kind. The author is not responsible for any consequences arising from their use.
Send comments and bug reports to chris@chrisspackman.com.
Last updated: 2026-02-21
This page is Copyright © 2025 Chris Spackman.
This web site developed entirely on GNU/Linux with Free / Open Source Software.
This work is licensed under a
Creative Commons
Attribution-ShareAlike 4.0 International License.