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.
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.
| Stage | Expression |
|---|---|
| Alice public value | A=gᵃ mod p |
| Bob public value | B=gᵇ mod p |
| Shared secret | s=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
| Concept | Formula or rule |
|---|---|
| Public values | A = gᵃ mod p, B = gᵇ mod p |
| Shared secret | s = Bᵃ mod p = Aᵇ mod p |
Worked example
Common mistakes
Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.
Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.
Tiny p values are useful only for learning. Real cryptographic parameters are vastly larger and chosen according to a standardized group.
Unauthenticated Diffie–Hellman is vulnerable to man-in-the-middle attacks even though the modular arithmetic is correct.
FAQ
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.