Diffie-Hellman Mathematics Calculator

Compute public values and shared secret for a small Diffie-Hellman key exchange example. This educational calculator shows the formula, result, and step-by-step interpretation.

Computer ScienceCryptography

Calculator

What this calculator teaches

Diffie-Hellman demonstrates how modular exponentiation can create a shared secret over a public channel.

Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.

Understanding Diffie–Hellman Key Exchange

Diffie–Hellman lets two parties derive the same shared modular value while exchanging only public values. In a small textbook example, both sides agree on a modulus p and a base g. Alice chooses a private exponent a and publishes A=gᵃ mod p; Bob chooses b and publishes B=gᵇ mod p.

Alice then computes Bᵃ mod p, while Bob computes Aᵇ mod p. Both expressions equal gᵃᵇ mod p, so the two parties arrive at the same shared secret without sending a or b directly.

StageExpression
Alice public valueA=gᵃ mod p
Bob public valueB=gᵇ mod p
Shared secrets=Bᵃ mod p=Aᵇ mod p

For educational examples, small integers make the arithmetic visible. Secure real-world systems require carefully selected groups, large parameters, authenticated protocols, and protection against active attacks. Diffie–Hellman by itself establishes a shared value but does not authenticate who is on the other side.

Formula & Symbols

ConceptFormula or rule
Public valuesA = gᵃ mod p, B = gᵇ mod p
Shared secrets = Bᵃ mod p = Aᵇ mod p

Worked example

Example: With p=23 and g=5, Alice and Bob compute the same shared secret without sending private exponents.
Example 2: With p=23, g=5, a=6, b=15, Alice publishes 8, Bob publishes 19, and both obtain shared secret 2.
Example 3: With p=7, g=3, a=2, b=4, A=2, B=4, and the shared secret is 2.
Example 4: If a=0, A=1 and the shared value collapses to 1; this is mathematically valid but unsuitable as a meaningful private exponent.
Example 5: The identity (gᵃ)ᵇ ≡ (gᵇ)ᵃ ≡ gᵃᵇ (mod p) explains why both sides agree.

Common mistakes

⚠️
Using the wrong input format

Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.

⚠️
Ignoring assumptions

Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.

⚠️
Treating small examples as secure keys

Tiny p values are useful only for learning. Real cryptographic parameters are vastly larger and chosen according to a standardized group.

⚠️
Forgetting authentication

Unauthenticated Diffie–Hellman is vulnerable to man-in-the-middle attacks even though the modular arithmetic is correct.

FAQ

Can I use decimals?
Most numerical calculators allow decimals, but modular arithmetic and coding-theory tools usually require integers or binary strings.
Is this for homework checking?
Yes. The page is designed to show both the answer and the reasoning pattern.
Why does the result sometimes say approximate?
Some probability, floating-point, and numerical methods naturally produce approximations.
Why do Alice and Bob get the same secret?
Both compute the same modular power g^(ab): one as (g^b)^a and the other as (g^a)^b.
Does p always have to be prime?
Classic textbook finite-field Diffie–Hellman commonly uses a prime modulus, but other standardized groups, including elliptic-curve groups, are also used.
Must g be a generator?
Parameters should be chosen so g generates a suitable large subgroup. A poorly chosen base can severely reduce the number of possible shared values.
Is the shared number used directly as an encryption key?
Real protocols normally feed the shared secret into a key-derivation function rather than using the raw modular value directly.

How to Verify the Shared Secret

For a small classroom example, compute A=gᵃ mod p and B=gᵇ mod p separately, then verify the final value two ways: Bᵃ mod p and Aᵇ mod p. The two numbers must agree. If they do not, recheck modular exponentiation before proceeding.

Repeated squaring is a useful manual method because it avoids calculating huge ordinary powers. To find g¹⁵ mod p, for example, compute g, g², g⁴, and g⁸ modulo p, then multiply the residues corresponding to 15=8+4+2+1. For security interpretation, arithmetic agreement is only one requirement. Real deployments also validate parameters, use large standardized groups, authenticate peers, and derive symmetric keys through a cryptographic KDF.

Related calculators

These links will work after the calculators are registered in the final Math layout update.

Formula Explorer connections

Interpretation: This formula uses integer divisibility, modular arithmetic, finite fields or coding relationships. Assumption: Inputs are usually integers with specific modulus or coprimality requirements. Cryptographic examples are educational and not a substitute for vetted security libraries.

Factorial Calculator →Finite Field GF(2ⁿ) Calculator →GCF and LCM Calculator – Greatest Common Factor and Least Common Multiple →Math Formula Explorer →