Diffie–Hellman Key Exchange, With Numbers You Can Follow
In a Diffie–Hellman key exchange, the shared secret is calculated twice: once on each participant's computer. The information crossing the network is public, but each calculation also uses a private value that stays at its own end.
This is key agreement, a way for two participants to establish shared secret information. Encryption software can use keys derived from that information to scramble messages so they can't readily be read.
An encryption key is a data value the software uses to scramble or recover information. Diffie–Hellman establishes the starting secret; it doesn't itself encrypt the messages or verify who's taking part.
The Number Trick Behind a Shared Secret
A small example you can check
We'll call the participants Alice and Bob and use tiny numbers to make the arithmetic visible. These teaching numbers are far too small to provide real security.
Alice and Bob publicly agree to use 2 and 23. Here, 23 is a prime number: it's divisible only by 1 and itself.
The notation mod 23 means “take the remainder after dividing by 23.” A raised number, called an exponent, means repeated multiplication, so 2³ is 2 × 2 × 2, or 8.
| Step | Alice | Bob |
|---|---|---|
| Keep a private number | 3 | 7 |
| Calculate a public result | 2³ mod 23 = 8 | 2⁷ mod 23 = 13 |
| Send that public result | Sends 8 to Bob | Sends 13 to Alice |
| Combine the received result with the private number | 13³ mod 23 = 12 | 8⁷ mod 23 = 12 |
Both finish with 12, without sending 12 to each other. Alice used the public 13 and her private 3; Bob used the public 8 and his private 7.
- Make a stolen password useless without the key in your hand.
- Plug into a USB-C laptop or tap it against your phone.
- Use one key across Google, Microsoft, Apple and many more accounts.
The answers match because both calculations ultimately correspond to 2 raised to the power 21, followed by taking the remainder. The private exponents give the same product: 3 × 7 = 7 × 3.
The Diffie–Hellman key-agreement specification gives the general calculation. Our table is a small illustration of it, not a set of settings for a real connection.
What the listener is missing
A listener can see 2, 23, 8, and 13. They haven't received the private 3 or 7, although these tiny choices would be easy to discover by trying possibilities.
Real implementations use suitable mathematical parameters and unpredictable private values that make recovering the shared secret from the public exchange computationally difficult. The settings, random-number generation, and software implementation all affect that protection.
The shared secret also needs further processing. A key-derivation function is a calculation that turns secret material and other connection information into the keys the protocol needs.
A protocol is the set of rules the communicating computers follow. The TLS 1.3 specification separates establishing keys from using them to protect subsequent traffic.
TLS means Transport Layer Security, a protocol widely used to protect web connections.
How Do You Send a Secret Without Sharing a Secret First?
A matching secret doesn't establish an identity
An attacker who can replace the public values may arrange two separate exchanges: one with Alice and one with Bob. Each participant then shares a secret with the attacker instead of with the intended partner.
This is a man-in-the-middle attack, where someone intervenes between the participants. The TLS specification's discussion of anonymous Diffie–Hellman explains why the arithmetic can succeed while the identity assumption fails.
A complete connection protocol needs authentication, checks that establish the other participant's identity and bind that identity to the exchange. TLS's security analysis treats those checks as part of authenticated key exchange.
- Turn sketchy hotel and airport Wi-Fi into your own private network in minutes.
- Put every device you own behind your VPN at once, with nothing to install on each one.
- Block ads and trackers for everything connected with the flick of a switch.
The modern names use the same basic idea
Elliptic-curve Diffie–Hellman, or ECDH, uses a different mathematical structure to establish a shared secret. The X25519 specification describes one version, where each side again combines its private value with the other's public result.
An extra E in ECDHE means ephemeral, or temporary: the exchange uses short-lived keys.
Fresh exchange keys can support forward secrecy: later theft of the key used to prove a system's identity doesn't, by itself, expose earlier conversations. That protection depends on the temporary secrets having been properly erased.
That protection doesn't cover an attacker reading messages directly on a compromised computer. Real applications need a maintained, authenticated protocol implementation that handles these details, rather than just the small calculation shown above.
Asymmetric Cryptography: Two Keys, Different Jobs

Man-in-the-Middle Attacks: An Encrypted Login Can Reach the Wrong Site

How Does Diffie-Hellman Work?

QUOTE:
"Amateurs hack systems, professionals hack people."

