Diffie-Hellman Key Exchange Explainer
Enter prime p, generator g, and two private keys to see every step of Diffie-Hellman key exchange, including public keys and the derived shared secret.
Parameters
Must be prime. Try 23, 47, 97, 257.
Primitive root mod p. Try 5 for p=23.
Private Keys
Quick Presets
Set parameters on the left and click Compute Key Exchange
Alice and Bob publicly agree on a prime p and a generator g. Anyone can see these — they are not secret.
Each party picks a secret integer. These values never leave their owner — not even the other party sees them.
Each party raises g to their private key mod p. The result is their public key, safe to transmit openly.
Each party raises the other's public key to their own private key mod p. Due to the commutativity of exponents, both sides arrive at the same number.
gab mod p = — and also —
Security note for small primes
Real-world DH uses primes of 2048 bits or larger. Small values like these are trivially breakable by brute-forcing the discrete logarithm — use them only for learning.
Summary
Enter prime p, generator g, and two private keys to see every step of Diffie-Hellman key exchange, including public keys and the derived shared secret.
How it works
- Choose a public prime p and generator g (a primitive root mod p).
- Each party picks a private key (a secret integer smaller than p).
- Each party computes their public key: g raised to their private key, mod p.
- The parties exchange public keys over the (potentially insecure) channel.
- Each party raises the other's public key to their own private key, mod p.
- Both sides arrive at the same shared secret — proven by the math below.
Use cases
- Learn how TLS/HTTPS establishes session keys without pre-shared secrets.
- Understand why intercepting only public keys does not reveal the shared secret.
- Verify your understanding of modular arithmetic and discrete logarithms.
- Teach or study public-key cryptography with concrete numeric examples.
- Prepare for security certification exams that cover key-exchange protocols.
- Explore why small primes are insecure compared to 2048-bit DH groups.