Course contentsShow
← Security
Lesson 153 of 3,0524. Asymmetric Cryptography and PKIFree lesson

Diffie-Hellman Key Exchange Fundamentals

How two parties establish a shared secret over an insecure channel using modular exponentiation and public values.

Diffie-Hellman Key Exchange Fundamentals

What you'll learn: How two parties can agree on a shared secret key over a public, insecure channel without ever directly transmitting that secret.

The Problem It Solves

Imagine Alice and Bob want to communicate securely using symmetric encryption (like AES), but they've never met in person to exchange a key. How can they agree on a shared secret when anyone might be listening to their conversation? Diffie-Hellman solves this seemingly impossible problem.

How It Works (The Paint Mixing Analogy)

Think of mixing paint colors:

  1. Public Agreement: Alice and Bob publicly agree on a common "base color" (yellow) that everyone can see.
  2. Private Secrets: Each picks their own secret color. Alice chooses red (kept private), Bob chooses blue (kept private).
  3. Public Mix: Alice mixes yellow + red = orange, and sends orange publicly. Bob mixes yellow + blue = green, and sends green publicly.
  4. Final Mix: Alice adds her secret red to Bob's green = brown. Bob adds his secret blue to Alice's orange = brown.
  5. Shared Secret: They both arrive at the same brown color without ever sharing their individual secret colors!

The Math Behind It

Diffie-Hellman uses modular exponentiation instead of paint:

  • Public values: a large prime p and base g
  • Alice picks secret a, computes A = g^a mod p, sends A
  • Bob picks secret b, computes B = g^b mod p, sends B
  • Alice computes B^a mod p, Bob computes A^b mod p
  • Both arrive at the same value: g^(ab) mod p

The magic: computing g^(ab) from g^a and g^b is mathematically hard for eavesdroppers (the discrete logarithm problem).

Key Takeaway: Diffie-Hellman enables two parties to establish a shared secret over an insecure channel using public exchanges and private calculations, forming the foundation for secure key agreement in modern cryptography.