Diffie-Hellman Key Exchange Calculator
Enter a prime p, a base g and the two private numbers a and b to see the public values and the shared secret computed step by step, and why both parties get the same key.
What is the Diffie-Hellman key exchange?
Diffie–Hellman, published in 1976, lets two people agree on a secret number over a public channel without ever sending the secret itself. Alice and Bob publicly agree on a prime p and a base g. Alice chooses a private number a and sends A = ga mod p; Bob chooses b and sends B = gb mod p.
Alice then computes Ba mod p and Bob computes Ab mod p. Both equal gab mod p, so they now share a secret that an eavesdropper, who sees only p, g, A and B, cannot easily compute.
Why it is secure (and when it is not)
To find the secret, an attacker would need a from A = ga mod p. This is the discrete logarithm problem, which has no known fast algorithm for well-chosen large primes. With the tiny numbers used in lessons, however, the attacker can simply try every exponent. Real deployments use primes of at least 2048 bits or elliptic curves, and they authenticate the messages, because the plain exchange does not protect against a man in the middle who impersonates both parties.
Worked example
Take p = 23 and g = 5, with a = 6 and b = 15. Alice sends A = 56 mod 23 = 8 and Bob sends B = 515 mod 23 = 19. Alice computes 196 mod 23 = 2 and Bob computes 815 mod 23 = 2. The shared secret is 2. Choosing g as a primitive root of p, as 5 is for 23, means the powers of g reach every non-zero residue, which gives the largest possible space of secrets.
What the calculator checks
The calculator verifies that p is prime, that g and both secret numbers lie in the valid range 2 ≤ g ≤ p − 2 and 1 ≤ a, b ≤ p − 2, and that the two computed secrets agree. For primes up to 1015 it also computes the order of g and tells you whether g is a primitive root, which is the standard textbook requirement.
Worked examples
These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.
p=23, g=5, a=6, b=15
- Prime p:
23 - Base g:
5 - Alice's secret a:
6 - Bob's secret b:
15
The order of g modulo p is 22 = p − 1, so g is a primitive root and its powers cover every non-zero residue.
Teaching demonstration only: with small p an attacker can find a or b by trying every exponent. Real systems use primes of 2048 bits or more, or elliptic curves, and authenticate the exchange.
Step-by-step solution
Formula used
A = g^a mod p, B = g^b mod p
s = B^a = A^b = g^(ab) mod p
NotePublic parameters
Prime modulus p = 23
Base g = 5
Both are known to everyone, including an eavesdropper.
CalculateAlice computes her public value
A = g^a mod p
A = 5^6 mod 23 = 8
Alice sends A = 8 to Bob.
CalculateBob computes his public value
B = g^b mod p
B = 5^15 mod 23 = 19
Bob sends B = 19 to Alice.
CalculateAlice computes the shared secret
s = B^a mod p
s = 19^6 mod 23 = 2
CalculateBob computes the shared secret
s = A^b mod p
s = 8^15 mod 23 = 2
CheckWhy both get the same value
B^a = (g^b)^a = g^(ab) = (g^a)^b = A^b (mod p)
Direct check: g^(ab) mod p = 2 = 2 ✓
ResultShared secret
s = 2
p=353, g=3, a=97, b=233
- Prime p:
353 - Base g:
3 - Alice's secret a:
97 - Bob's secret b:
233
The order of g modulo p is 352 = p − 1, so g is a primitive root and its powers cover every non-zero residue.
Teaching demonstration only: with small p an attacker can find a or b by trying every exponent. Real systems use primes of 2048 bits or more, or elliptic curves, and authenticate the exchange.
Step-by-step solution
Formula used
A = g^a mod p, B = g^b mod p
s = B^a = A^b = g^(ab) mod p
NotePublic parameters
Prime modulus p = 353
Base g = 3
Both are known to everyone, including an eavesdropper.
CalculateAlice computes her public value
A = g^a mod p
A = 3^97 mod 353 = 40
Alice sends A = 40 to Bob.
CalculateBob computes his public value
B = g^b mod p
B = 3^233 mod 353 = 248
Bob sends B = 248 to Alice.
CalculateAlice computes the shared secret
s = B^a mod p
s = 248^97 mod 353 = 160
CalculateBob computes the shared secret
s = A^b mod p
s = 40^233 mod 353 = 160
CheckWhy both get the same value
B^a = (g^b)^a = g^(ab) = (g^a)^b = A^b (mod p)
Direct check: g^(ab) mod p = 160 = 160 ✓
ResultShared secret
s = 160
g not a primitive root (p=23, g=2)
- Prime p:
23 - Base g:
2 - Alice's secret a:
4 - Bob's secret b:
9
The order of g modulo p is 11, which is smaller than p − 1 = 22, so g is not a primitive root and the shared secret can only take 11 different values.
Teaching demonstration only: with small p an attacker can find a or b by trying every exponent. Real systems use primes of 2048 bits or more, or elliptic curves, and authenticate the exchange.
Step-by-step solution
Formula used
A = g^a mod p, B = g^b mod p
s = B^a = A^b = g^(ab) mod p
NotePublic parameters
Prime modulus p = 23
Base g = 2
Both are known to everyone, including an eavesdropper.
CalculateAlice computes her public value
A = g^a mod p
A = 2^4 mod 23 = 16
Alice sends A = 16 to Bob.
CalculateBob computes his public value
B = g^b mod p
B = 2^9 mod 23 = 6
Bob sends B = 6 to Alice.
CalculateAlice computes the shared secret
s = B^a mod p
s = 6^4 mod 23 = 8
CalculateBob computes the shared secret
s = A^b mod p
s = 16^9 mod 23 = 8
CheckWhy both get the same value
B^a = (g^b)^a = g^(ab) = (g^a)^b = A^b (mod p)
Direct check: g^(ab) mod p = 8 = 8 ✓
ResultShared secret
s = 8
Large prime 1000000007
- Prime p:
1000000007 - Base g:
5 - Alice's secret a:
123456 - Bob's secret b:
654321
The order of g modulo p is 1000000006 = p − 1, so g is a primitive root and its powers cover every non-zero residue.
Teaching demonstration only: with small p an attacker can find a or b by trying every exponent. Real systems use primes of 2048 bits or more, or elliptic curves, and authenticate the exchange.
Step-by-step solution
Formula used
A = g^a mod p, B = g^b mod p
s = B^a = A^b = g^(ab) mod p
NotePublic parameters
Prime modulus p = 1000000007
Base g = 5
Both are known to everyone, including an eavesdropper.
CalculateAlice computes her public value
A = g^a mod p
A = 5^123456 mod 1000000007 = 603701138
Alice sends A = 603701138 to Bob.
CalculateBob computes his public value
B = g^b mod p
B = 5^654321 mod 1000000007 = 688264863
Bob sends B = 688264863 to Alice.
CalculateAlice computes the shared secret
s = B^a mod p
s = 688264863^123456 mod 1000000007 = 694764134
CalculateBob computes the shared secret
s = A^b mod p
s = 603701138^654321 mod 1000000007 = 694764134
CheckWhy both get the same value
B^a = (g^b)^a = g^(ab) = (g^a)^b = A^b (mod p)
Direct check: g^(ab) mod p = 694764134 = 694764134 ✓
ResultShared secret
s = 694764134
Frequently asked questions
Does Diffie-Hellman encrypt messages?
No. It only lets two parties agree on a shared secret. That secret is then used as a key for a symmetric cipher such as AES.
Why must g be a primitive root?
If g has a small order, the shared secret can only take a few values and an attacker could guess it. A primitive root generates all p − 1 non-zero residues.
Are the numbers a and b sent over the channel?
Never. Only A = g^a mod p and B = g^b mod p are sent. Keeping a and b private is the whole point.
What is a man-in-the-middle attack?
An attacker who can modify messages runs one exchange with Alice and another with Bob, so each party ends up sharing a key with the attacker. Authentication (certificates or signatures) is needed to prevent it.