Diffie-Hellman Key Exchange Simulator
Perform a secure DH key exchange online. Use our simulator to compute shared secrets locally in your browser. Perfect for understanding dh key exchange mechanics.
Related Utilities
The Mathematical Foundation of the DH Key Exchange Online
The Diffie-Hellman protocol is one of the most elegant breakthroughs in current cryptography, enabling two parties to establish a shared secret over an unsecure channel. When you perform a DH key exchange online, you are essentially navigating the discrete logarithm problem. The security of this exchange relies on the fact that while it is computationally trivial to perform modular exponentiation, it is incredibly difficult to reverse the process to find the private exponent. By using a prime modulus ($p$) and a generator ($g$), both parties can derive an identical result without ever transmitting their private keys.
Configuring Your DH Key Exchange Parameters
Before performing any calculations, you must establish the domain parameters. The "Prime Modulus ($p$)" input defines the field size; in production, this is typically a massive prime number to thwart brute-force attempts. The "Generator ($g$)" is the primitive root modulo $p$, which acts as the base for our exponentiation. You can modify these values to observe how different prime modulus choices impact the resulting public keys in your DH key exchange simulation.
Verifying Public Keys with the DH Key Exchange Converter
Once you have set your domain parameters, the next phase involves generating private secrets for Alice and Bob. These secrets are represented by ($a$) and ($b$), which remain local to each participant. Using the "Compute Public Keys" action, the system calculates the public values ($A$) and ($B$) using the formula:
$$ A = g^a \pmod{p} $$
$$ B = g^b \pmod{p} $$
These public values are intended to be shared openly. Even if an eavesdropper intercepts $A$ and $B$, they cannot feasibly calculate the private secrets $a$ or $b$ due to the complexity of the discrete logarithm problem.
Deriving the Shared Secret Key in Your Browser
The final step of the dh key exchange is the derivation of the shared secret. Alice uses Bob's public key ($B$) and her private secret ($a$), while Bob uses Alice's public key ($A$) and his private secret ($b$). The beauty of this math is that both arrive at the exact same value ($s$):
$$ s = B^a \pmod{p} = (g^b)^a \pmod{p} $$
$$ s = A^b \pmod{p} = (g^a)^b \pmod{p} $$
This result can then be used as a symmetric key for further encrypted communication.
Define Domain Parameters
Input your desired prime modulus ($p$) and generator ($g$) in the top configuration card to define the cryptographic field.
Generate Private Secrets
Click "Generate Random Secrets" to assign random integers to both Alice and Bob, ensuring a fresh start for your exchange.
Compute Public Keys
Click "Compute Public Keys" to see the resulting $A$ and $B$ values, which are the products of your generator raised to your secret powers mod $p$.
Derive Shared Secret
Use the "Derive Shared Secret Key" button to complete the math, resulting in an identical output for both Alice and Bob.
Why Prime Modulus Choice Matters for DH Key Exchange Online
Choosing the right $p$ and $g$ is the most critical aspect of any dh key exchange online simulation. If $p$ is too small, the discrete logarithm problem becomes trivial for current hardware to solve via trial and error. In this simulator, we use small numbers for educational clarity, but real-world implementations require primes that are thousands of bits long. Always ensure $p$ is a prime number to maintain the algebraic properties required for a secure field.
Practical Walkthrough: Calculating a Sample Secret
Let's assume a prime modulus $p = 23$ and a generator $g = 5$. Alice chooses a private secret $a = 6$. Her public key becomes $5^6 \pmod{23} = 8$. Bob chooses a private secret $b = 15$. His public key becomes $5^{15} \pmod{23} = 19$. When Alice receives Bob's $19$, she calculates $19^6 \pmod{23} = 2$. Bob, receiving Alice's $8$, calculates $8^{15} \pmod{23} = 2$. They have successfully established a shared secret of $2$ without sharing their private exponents $6$ or $15$.
Comparing DH Key Exchange Output Settings
| Input Field | Purpose | Sensitivity |
|---|---|---|
| Prime Modulus ($p$) | Defines the field size | High - Must be prime |
| Generator ($g$) | The base for exponentiation | Medium - Must be a primitive root |
| Private Secrets ($a, b$) | The hidden exponents | Critical - Never share these |
| Shared Secret ($s$) | The final derivation | Critical - Used as a symmetric key |