How to use the diffie-hellman key exchange calculator
- Enter a prime modulus p. The page tests that it is prime.
- Enter the base g, a number from 2 to p - 2. A primitive root is best.
- Enter the two private numbers: Alice's a and Bob's b, each below p - 1.
- 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.
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.