No history yet

Diffie-Hellman Overview

The Key Exchange Problem

Imagine you and a friend want to share a secret message. To keep it safe, you plan to lock it in a box. But how do you get the key to your friend without someone intercepting it? If you send the key by itself, an eavesdropper could copy it. You can't just mail a key in an unlocked box.

This is a classic problem in cryptography. How can two people, who have never met and can only communicate over a public channel (like the internet), agree on a secret key that only they know? This is where the Diffie-Hellman key exchange comes in. It’s a clever method that allows two parties, let's call them Alice and Bob, to create a shared secret while snoops listen in.

The Diffie-Hellman key exchange is a cryptographic protocol that allows parties to establish a shared secret over an insecure channel.

The magic of Diffie-Hellman doesn't come from hiding information. Instead, it relies on performing mathematical operations that are easy to do in one direction but incredibly difficult to reverse. Think of mixing paint. It's simple to mix yellow and blue to get green. But it's nearly impossible to take that green paint and perfectly separate it back into pure yellow and blue.

Modular Arithmetic

The mathematical foundation of Diffie-Hellman is modular arithmetic, which is sometimes called "clock arithmetic." When we use a 12-hour clock, the hours wrap around. If it's 8 o'clock and you add 5 hours, it becomes 1 o'clock, not 13 o'clock. In mathematical terms, we would say (8+5)(mod12)=1(8 + 5) \pmod{12} = 1.

The modulus is the number we divide by to find the remainder. In the clock example, the modulus is 12. Diffie-Hellman uses this concept with very large numbers, specifically with prime numbers.

ab(modn)a \equiv b \pmod{n}

The process works like this:

  1. Alice and Bob publicly agree on two numbers: a prime number, pp, and a base, gg. These aren't secret.
  2. Alice chooses a secret private number, aa. Bob chooses his own secret private number, bb. They never share these.
  3. Alice calculates A=ga(modp)A = g^a \pmod{p} and sends the result, AA, to Bob.
  4. Bob calculates B=gb(modp)B = g^b \pmod{p} and sends the result, BB, to Alice.
  5. Alice takes Bob's public number, BB, and calculates the shared secret: s=Ba(modp)s = B^a \pmod{p}.
  6. Bob takes Alice's public number, AA, and calculates the shared secret: s=Ab(modp)s = A^b \pmod{p}.

Even though they started with different secrets, they both arrive at the exact same number.

Why does this work? It's because of the properties of exponents. Alice's calculation is s=(gb)a(modp)s = (g^b)^a \pmod{p}, which simplifies to gba(modp)g^{ba} \pmod{p}. Bob's calculation is s=(ga)b(modp)s = (g^a)^b \pmod{p}, which simplifies to gab(modp)g^{ab} \pmod{p}. Since ab=baab = ba, their results are identical.

The Discrete Logarithm Problem

An eavesdropper, let's call her Eve, knows pp, gg, AA, and BB. To find the shared secret, she needs to find either Alice's secret aa or Bob's secret bb. To get aa, she would need to solve the equation A=ga(modp)A = g^a \pmod{p}. Finding the exponent aa in this equation is known as the discrete logarithm problem.

For small numbers, this is easy. If we know 8=2x(mod17)8 = 2^x \pmod{17}, we can try a few values and find that x=3x=3. But when pp is a prime number hundreds of digits long, finding this exponent becomes computationally infeasible. There is no known efficient algorithm to solve it. This difficulty is what keeps the shared secret safe.

discrete logarithm

noun

The exponent that a base must be raised to in modular arithmetic to yield a certain number. Finding it is a computationally difficult problem for large numbers.

Let's walk through a simple example.

StepAliceBobEve (Eavesdropper)
1. Public NumbersAgree on p=23p=23 and g=5g=5.Agree on p=23p=23 and g=5g=5.Sees p=23,g=5p=23, g=5.
2. Private KeysChooses secret a=4a=4.Chooses secret b=3b=3.Knows nothing.
3. Public KeysCalculates A=54(mod23)=625(mod23)=4A = 5^4 \pmod{23} = 625 \pmod{23} = 4. Sends 4 to Bob.Calculates B=53(mod23)=125(mod23)=10B = 5^3 \pmod{23} = 125 \pmod{23} = 10. Sends 10 to Alice.Sees A=4A=4 and B=10B=10.
4. Shared SecretCalculates s=Ba(modp)=104(mod23)=10000(mod23)=18s = B^a \pmod{p} = 10^4 \pmod{23} = 10000 \pmod{23} = 18.Calculates s=Ab(modp)=43(mod23)=64(mod23)=18s = A^b \pmod{p} = 4^3 \pmod{23} = 64 \pmod{23} = 18.Is stuck. To find aa, she must solve 4=5a(mod23)4=5^a \pmod{23}.

Both Alice and Bob have independently calculated the shared secret, 18. They can now use this number as a key to encrypt their future communications, and Eve is left out in the cold. With large enough numbers, this method provides a secure foundation for encrypted channels.

Ready to check your understanding?

Quiz Questions 1/5

What is the primary problem that the Diffie-Hellman key exchange is designed to solve?

Quiz Questions 2/5

In the Diffie-Hellman process, which of the following pieces of information are considered public and can be seen by an eavesdropper?