Diffie-Hellman Key Calculator

Enter the public parameters and both private keys to derive the shared secret.

How it is calculated

You supply the public prime p, the generator g and two private exponents a (Alice) and b (Bob). The tool computes each public value by fast modular exponentiation (square-and-multiply): A = ga mod p and B = gb mod p. Then each side raises the other's public value to its own private exponent: s = Ba mod p for Alice and Ab mod p for Bob. Both must give the same number, and the page shows both so you can check. All four inputs must be whole numbers below 253, with p above 1, g above 0 and a, b not negative. The modulus p is tested for primality (Miller-Rabin); if p is not prime, a decimal is entered, or any value is out of range, the tool shows no result. Products are computed without overflow, so large primes up to about 9 quadrillion work.

Worked example

Take the textbook values p = 23, g = 5, Alice's a = 6 and Bob's b = 15. Alice's public value is 56 mod 23 = 15,625 mod 23 = 8. Bob's is 515 mod 23 = 19. Alice computes 196 mod 23 and Bob computes 815 mod 23; both arrive at the shared secret 2, and the verification line shows 2 as well. An eavesdropper sees only 23, 5, 8 and 19.

Try this example →

Common mistakes

  • Choosing a composite modulus such as p = 24. The tool checks that p is prime and returns nothing otherwise, because the exchange is not meaningful modulo a composite number.
  • Entering a decimal exponent such as 6.5, or a negative one. Only whole, non-negative private keys are accepted, so no result appears.
  • Swapping the private and public values. a and b are the secret exponents; the public values A and B are outputs, not inputs.
  • Picking a generator that gives a trivial exchange: g = 1 makes every public value and the shared secret equal to 1. A useful g generates many residues modulo p, so check it against p rather than choosing at random.
  • Expecting real security from the result. Practical Diffie-Hellman uses primes thousands of bits long, so a calculator limited to numbers below 253 is for learning and hand-checking only.

About this calculator

The Diffie-Hellman key exchange lets two parties agree on a shared secret over a public channel without ever transmitting the secret itself. Both sides agree on a public prime modulus p and generator g; each picks a private key (a and b) and computes a public key — A = g^a mod p and B = g^b mod p — which they exchange openly. Each side then raises the other's public key to their own private key: the shared secret is s = B^a mod p = A^b mod p = g^(ab) mod p, arriving at the same number without either private key ever crossing the wire.

Security rests on the discrete logarithm problem — computing a from g, p and A = g^a mod p is computationally infeasible for a large enough prime p, even though computing A from a is easy. Computer science students, security engineers, and protocol implementers use small-number examples like this to verify by hand how TLS, SSH, and other protocols establish a shared session key before any application data is sent.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators