Diffie–Hellman Key Exchange
Explore Diffie–Hellman, the original public-key protocol for two parties to agree on a shared secret over an insecure channel without ever transmitting it. The public parameters are a large prime p and a generator g. Alice keeps a private number a and publishes A = gᵃ mod p; Bob keeps private b and publishes B = gᵇ mod p. Each raises the other's public value to their own private exponent: Alice computes Bᵃ mod p, Bob computes Aᵇ mod p — and both get the same shared secret, because gᵃᵇ = gᵇᵃ. An eavesdropper sees p, g, A, and B but recovering a or b requires solving the discrete logarithm problem. This tool generates safe primes with a Miller–Rabin test, computes public keys and the shared secret with exact BigInt modular exponentiation, and can brute-force the discrete log for small primes to show why it's hard. Everything runs locally in your browser.
Public parameters
Private keys
Alice
Bob
Shared secret
Eavesdropper (discrete logarithm)
A safe prime is a prime p = 2q + 1 where q is also prime; such primes give a large subgroup of order q, making the discrete logarithm as hard as possible for a given size. The discrete logarithm problem — finding x given g and gˣ mod p — has no known efficient classical algorithm; the best general attacks (number-field sieve) are sub-exponential but still infeasible for large p, which is why real DH uses ≥2048-bit primes. This tool's brute-force attack only works for the toy primes generated here and is meant to illustrate the asymmetry: computing gˣ mod p is easy (modular exponentiation, polynomial time), but inverting it is exponentially hard. Diffie–Hellman is key agreement, distinct from encryption (see the RSA Calculator) — it produces a shared secret you'd then feed into a symmetric cipher. Pairs with the Modular Arithmetic, Euler's Totient, and RSA Calculator tools. Everything runs locally — nothing leaves your browser.