Diffie-Hellman Shared Key Calculator

Diffie-Hellman lets two parties build a shared secret over an open channel. This calculator steps through the exchange with a small prime modulus and generator so you can see every value. Enter the prime, the generator and the two private keys to see each side's public value and the shared secret both parties reach. Use it to learn the protocol; real-world Diffie-Hellman uses primes thousands of bits long.

0.00
0.00
0.00
0.00

Diffie-Hellman formulas

A = g^a mod p
B = g^b mod p
shared (A side) = B^a mod p
shared (B side) = A^b mod p
both equal g^(a*b) mod p

Each party raises the generator to their private key modulo the prime to get a public value, then raises the other party's public value to their own private key. Both reach g to the product of the private keys modulo p, the shared secret, which matches on both sides.

Worked example

With p = 23, g = 5, a = 6, b = 15: A = 5^6 mod 23 = 8, B = 5^15 mod 23 = 19. Party A computes 19^6 mod 23 = 2; party B computes 8^15 mod 23 = 2. Both reach the shared secret 2, equal to 5^(6 times 15) mod 23.

Diffie-Hellman: frequently asked questions

What is the Diffie-Hellman key exchange?

Diffie-Hellman lets two parties agree on a shared secret over a public channel without ever sending the secret. They publicly agree on a prime modulus p and a generator g, each picks a private key, exchanges a public value, and combines the other's public value with their own private key to reach the same shared secret.

What are the formulas?

Public values: A = g^a mod p, B = g^b mod p, where a and b are the private keys. Shared secret: s = B^a mod p = A^b mod p. Both parties compute g^(a*b) mod p and arrive at the same value, which an eavesdropper cannot easily derive from g, p, A and B.

Why use small numbers here?

This tool uses small numbers so you can see every step of the exchange clearly. Real Diffie-Hellman uses primes of 2,048 bits or more; the security rests on the difficulty of the discrete logarithm problem at that scale. Never use small primes for actual key agreement.

What makes Diffie-Hellman secure?

Its security relies on the discrete logarithm problem: given g, p and g^a mod p, recovering the private key a is computationally hard for large, well-chosen parameters. With tiny numbers, as in this demonstration, it is trivial to break.

Sources and references

Reviewed by the CalculatorHub team, edited by James Graham, 19 June 2026. See our methodology.