Productivity and Utilities

Diffie-Hellman Key Exchange Calculator

Enter a prime, a base and two private numbers to see the public values and the shared secret.

Your numbers

A prime number. The page checks it.

Between 2 and p - 2. A primitive root is best.

Result

Shared secret

2

Alice sends 8 and Bob sends 19; both reach the same value.

Alice sends A = g^a mod p
8
Bob sends B = g^b mod p
19
Alice computes B^a mod p
2
Bob computes A^b mod p
2
Shared secret
2
Is p prime?
Yes
Eavesdropper by trial
Found a = 6 after 6 steps, secret 2
How this was calculated
  1. Alice computes A = g^a mod p and Bob computes B = g^b mod p, using fast modular exponentiation (square and multiply). They exchange A and B in the open.
  2. Alice computes B^a mod p = 2 and Bob computes A^b mod p = 2. Both equal g^(ab) mod p.
  3. An eavesdropper sees p, g, A and B but not a or b. For a small p the page shows that a simple search finds the exponent. With a large prime of thousands of bits that search cannot finish, which is what protects the secret.
  • This is a teaching tool. Do not use numbers from a web page to protect anything real. Real systems use standardized large primes or elliptic curves, authenticated exchanges and well tested libraries.

Calculated using SumPanda.com · https://sumpanda.com/diffie-hellman-key-exchange-calculator/

Results are estimates for general information. Verify important figures before relying on them.

Next step

How to use the diffie-hellman key exchange calculator

  1. Enter a prime modulus p. The page tests that it is prime.
  2. Enter the base g, a number from 2 to p - 2. A primitive root is best.
  3. Enter the two private numbers: Alice's a and Bob's b, each below p - 1.
  4. Read the public values A and B, the shared secret computed by each side, the primality result and what an eavesdropper would have to do.

Formula

Alice sends A = g^a mod p

Bob sends B = g^b mod p

Alice computes s = B^a mod p, and Bob computes s = A^b mod p

Both get the shared secret s = g^(a x b) mod p

The page computes the powers with fast modular exponentiation on exact integers, so it handles numbers of up to 40 digits without rounding.

The textbook example with p = 23 and g = 5

Alice picks a = 6 and sends A = 5^6 mod 23 = 8. Bob picks b = 15 and sends B = 5^15 mod 23 = 19. Alice computes 19^6 mod 23 = 2 and Bob computes 8^15 mod 23 = 2, so the shared secret is 2.

An eavesdropper who sees 23, 5, 8 and 19 can try exponents one by one. The page shows that this finds a = 6 after 6 steps and gives the same secret of 2. With a prime of hundreds of digits that search is not feasible, which is why real systems use very large numbers.

Why it works

Raising to a power modulo a prime is easy to compute but hard to undo: finding a from g^a mod p is the discrete logarithm problem. Each side combines its own secret with the other side's public value, and the powers commute, so both arrive at the same number without ever sending it.

To practice powers, inverses and remainders on other numbers, use the modular arithmetic calculator. To check a larger number for primality, use the prime number calculator.

This page is for learning

Real key exchange uses primes of 2048 bits or more, or elliptic curves, with vetted libraries, and it needs authentication to stop a person in the middle. Do not use numbers from this page as keys. The page limits numbers to 40 digits so that the steps stay readable and the eavesdropper search stays short.

For related number theory, see the Chinese remainder theorem solver.

Assumptions and limits

  • The modulus must be a prime, and the page reports whether it is.
  • Numbers are whole numbers of up to 40 digits, and private numbers are below p - 1.
  • The eavesdropper search tries exponents in order and runs only when p is at most 5 million.
  • This is an educational demonstration and not a tool for creating keys.

Formulas reviewed October 10, 2026. See the calculation methodology for how SumPanda rounds, tests and sources its formulas.

Frequently asked questions

What is Diffie-Hellman key exchange?

It is a method for two people to agree on a shared secret over an open channel. They exchange public values, and each combines the other's value with a private number.

What does mod mean?

It means the remainder after dividing. For example, 5^6 is 15625, and 15625 divided by 23 leaves a remainder of 8.

Why must p be prime?

A prime modulus gives the structure that makes the discrete logarithm hard and the exchange well behaved. The page tests it and tells you if it is not.

Is the shared secret safe to use?

Not from this page. Real systems use much larger numbers, vetted software and authentication.