Sitemap

Diffie–Hellman Key Exchange: How Two Strangers Can Share a Secret 🔐

6 min readSep 5, 2026

--

Press enter or click to view image in full size

Imagine Alice and Bob want to communicate securely over the internet. There is one problem:

They have never communicated before, and they need to agree on a secret encryption key.

They could simply send the key to each other, but anyone listening to the network could steal it. So, how can two people who have never met establish a shared secret over a completely public channel?

This is where Diffie–Hellman (DH) key exchange comes in.

Diffie–Hellman doesn’t encrypt your messages. Instead, it allows two parties to securely establish a shared secret that can later be used for encryption.

🔑 What Is Diffie–Hellman?

Diffie–Hellman is a key agreement protocol introduced by Whitfield Diffie and Martin Hellman in 1976. Its fundamental idea is surprisingly elegant:

Alice and Bob exchange information publicly, but still end up with the same secret value, without ever sending that secret across the network.

The security comes from a mathematical problem that is easy to perform in one direction but extremely difficult to reverse. For the traditional DH algorithm, this is based on the difficulty of the discrete logarithm problem.

🧮 The Mathematics Behind DH

Let’s start with two public numbers:

  • A large prime number: p
  • A generator: g

These values don’t need to be secret. For a simple example, let’s use:

p = 23
g = 5

In a real cryptographic system, these numbers are much larger, like 10–12 bits.

Step 1: Alice Chooses a Private Key

Alice secretly chooses a number:

a = 6

She then calculates her public value:

A = gᵃ mod p

Therefore:

A = 5⁶ mod 23
A = 15625 mod 23
A = 8

Alice sends 8 to Bob.

Notice that Alice does not send her private key 6.

Step 2: Bob Does the Same

Bob chooses his own private key:

b = 15

He calculates:

B = gᵇ mod p

So:

B = 5¹⁵ mod 23
B = 19

Bob sends 19 to Alice.

Now an attacker observing the network sees:

p = 23
g = 5
Alice's public value = 8
Bob's public value = 19

But they don’t know:

Alice's private key = 6
Bob's private key = 15

🤝 Step 3: Creating the Shared Secret

Now comes the interesting part.

Alice takes Bob’s public value and combines it with her private key.

S = Bᵃ mod p

Therefore:

S = 19⁶ mod 23
S = 2

Alice gets: Shared Secret = 2

Bob performs the opposite calculation:

S = Aᵇ mod p
S = 8¹⁵ mod 23
S = 2

Bob also gets: Shared Secret = 2

They now share the same secret. And the most important part?

The shared secret was never transmitted over the network.

🧠 But How Does That Work?

The magic is actually simple mathematics.

Alice calculates: S = (gᵇ)ᵃ mod p

Which becomes: S = gᵃᵇ mod p

Bob calculates: S = (gᵃ)ᵇ mod p

Which becomes: S = gᵃᵇ mod p

Therefore: (gᵇ)ᵃ = (gᵃ)ᵇ

Both arrive at the same result.

👀 What Does an Attacker See?

Suppose Eve is listening to the communication.

She can see:

p
g
A = gᵃ mod p
B = gᵇ mod p

But she wants to calculate: gᵃᵇ mod p

To do that, she would need to discover either a or b. Given: A = gᵃ mod p

Get Chulanka Lakmana Thabrew’s stories in your inbox

Join Medium for free to get updates from this writer.

Finding a from A, g, and p is known as the discrete logarithm problem. With properly chosen cryptographic parameters, solving this problem is computationally infeasible. That’s the security foundation of traditional Diffie–Hellman.

⚠️ DH Has an Important Weakness

Press enter or click to view image in full size
Photo by David Pupăză on Unsplash

At this point, you might think:

“So DH completely prevents attackers from intercepting the communication?”

Not exactly. DH by itself is vulnerable to a Man-in-the-Middle (MITM) attack.

Imagine Eve sits between Alice and Bob. Alice thinks she’s communicating with Bob. Bob thinks he’s communicating with Alice. But Eve intercepts their public values.

Instead of allowing Alice’s public key to reach Bob, Eve sends her own value. The result?

Alice ↔ Eve
Eve ↔ Bob

Alice establishes a secret with Eve. Bob establishes another secret with Eve. Eve can then potentially decrypt, read, and re-encrypt messages passing between them. This is why DH needs authentication.

🛡️ How Is DH Used Securely?

Modern protocols combine key exchange with authentication. For example, TLS, which protects HTTPS connections, uses authenticated key exchange mechanisms. Instead of simply saying:

“Here is my public value.”

The system also needs to establish:

“Here is my public value, and I can prove that I am actually the party you intended to communicate with.”

Digital certificates and digital signatures are commonly used for this purpose.

Press enter or click to view image in full size
Photo by Sasun Bughdaryan on Unsplash

🔄 What About ECDH?

Traditional Diffie–Hellman uses modular arithmetic over finite fields. Modern cryptographic systems often use Elliptic Curve Diffie–Hellman (ECDH). The fundamental concept remains the same:

Private Key
↓
Mathematical Operation
↓
Public Key
↓
Exchange Public Keys
↓
Combine Own Private Key + Other's Public Key
↓
Shared Secret

The mathematical foundation is different, but the goal is identical. Two parties independently derive the same secret without transmitting it directly.

⚡ Why Elliptic Curves?

One major advantage of elliptic-curve cryptography is that it can provide strong security with relatively small key sizes. This makes elliptic-curve approaches attractive for:

  • HTTPS
  • Secure messaging
  • VPNs
  • Mobile applications
  • Embedded devices
  • Modern authentication systems

A commonly encountered variant is ECDHE: Elliptic Curve Diffie–Hellman Ephemeral. The word ephemeral is important. It means temporary keys are generated for individual sessions.

🔥 Forward Secrecy

One of the biggest benefits of ephemeral Diffie–Hellman is Forward Secrecy.

Imagine an attacker records encrypted traffic today. Years later, they somehow obtain the server’s long-term private key. With an appropriately designed protocol using ephemeral key exchange, the attacker still shouldn’t be able to decrypt those previously recorded sessions.

Why? Because the session keys were generated using temporary private keys that are no longer available. So even if a long-term key is compromised later:

Past Sessions
↓
Remain Protected

This is one reason ephemeral Diffie–Hellman is so important in modern secure communications.

🌐 Where Do We See DH in the Real World?

You may encounter Diffie–Hellman without realizing it. It is part of the broader cryptographic machinery behind technologies such as:

  • HTTPS / TLS
  • VPN protocols
  • SSH
  • Secure messaging systems
  • Other authenticated key-exchange protocols

The user doesn’t normally see DH happening. You simply open a website. But behind the scenes, cryptographic protocols negotiate keys that allow your browser and the server to communicate securely.

💡 The Key Idea

The most beautiful part of Diffie–Hellman isn’t the calculation. It’s the concept. Alice and Bob don’t need to agree on a secret before communicating. They can communicate publicly.

Private: a                 Private: b   │                          │
│──── Public Key A ───────>│
│ │
│<──── Public Key B ───────│
│ │
▼ ▼
Shared Secret = gᵃᵇ mod p

Both independently calculate the same secret. An observer can see the public values but, under the underlying hardness assumptions and appropriate parameters, cannot feasibly recover the shared secret.

🚨 DH Is Not Encryption

This distinction is extremely important.

Diffie–Hellman is a key agreement mechanism, not an encryption algorithm.

Its job is:

Establish a shared secret.

That secret can then be processed into cryptographic keys used by symmetric encryption and authentication algorithms. For example:

Diffie–Hellman
↓
Shared Secret
↓
Key Derivation
↓
Session Keys
↓
Symmetric Encryption
↓
Secure Communication

So when you hear about DH in cybersecurity, think, “How can two parties agree on a secret without sending the secret?”

🎯 Final Takeaway

Diffie–Hellman is one of the foundational ideas in modern cryptography. It solves a seemingly impossible problem:

How can two strangers establish a shared secret over an insecure network?

The answer is elegant mathematics. Alice and Bob exchange public information, keep their private values secret, and independently calculate the same shared secret.

The core idea can be summarized in one equation: gᵃᵇ mod p

Both sides can calculate it. An attacker watching the communication cannot feasibly calculate it without solving a difficult mathematical problem. And that simple idea, introduced nearly five decades ago, remains at the heart of modern secure communication.

Cryptography is often about turning mathematical problems into practical security, and Diffie–Hellman is one of the most beautiful examples. 🔐

--

--