The Magic of Modulo: How the Diffie-Hellman Key Exchange Actually Works
11 mar 2026
Welcome to the incredible math behind the paint-mixing story: the Diffie-Hellman Key Exchange algorithm!
Behind those buckets of paint lies a mathematical concept called Modular Arithmetic, which is the absolute backbone of cybersecurity. You can think of modular arithmetic simply as "clock math" or "finding the remainder." For example, if it is currently 10:00 and you want to know what time it will be 5 hours from now, the answer isn't 15:00; it's 3:00. Because once you pass 12, the clock wraps around back to the beginning (15 \pmod{12} = 3).
Diffie-Hellman uses this exact "wrapping around" and "finding the remainder" logic to perform that magical "color mixing" with numbers. Let's look at how we solve this step-by-step using real numbers.
1. The Public Numbers (The Shared Color)
First, we establish two numbers out loud so that everyone in the classroom (or on the internet) can hear them. These represent our "Public Green" color:
p (Prime Number - The Modulus): A large prime number that will act as the limit for our math. (Think of it like the 12 on a clock).
g (The Generator - The Base): A number smaller than p.
Example: We shout to the classroom: "Hey everyone, we are using p = 23 and g = 5!"
2. Our Secret Numbers (The Secret Colors)
Now, we both pick a secret number for ourselves that we will never tell anyone.
My secret number (Pink): a = 4
Your secret number (Navy): b = 3
3. Preparing the Mixture (The Math)
Next, we take the public number g, raise it to the power of our secret number, and find the remainder when divided by p (this is the modular arithmetic part).
My Mixture (A):
(When we divide 625 by 23, the remainder is 4)
A = 4 (This is my public mixture that I will send to you)
Your Mixture (B):
(When we divide 125 by 23, the remainder is 10)
B = 10 (This is your public mixture that you will send to me)
4. The Swap
I shout across the room and send you the number 4. You shout back and send me the number 10. Everyone in the classroom hears 4 and 10, but they have absolutely no idea that our secret numbers are 3 and 4.
5. The Final Step: Finding the Secret Key (The Final Color)
Now, we take the "public mixtures" we received from each other and apply our own "secret numbers" to them, using the exact same formula.
My Calculation (I use the number B you sent me):
(When we divide 10000 by 23, the remainder is 18)
Result = 18
Your Calculation (You use the number A I sent you):
(When we divide 64 by 23, the remainder is 18)
Result = 18
And BOOM!
We both ended up with the exact same number: 18! Now we can safely use the number 18 as a password to encrypt all our future messages.
But Why Can't the Eavesdroppers Solve It?
Everyone in the room knows that p = 23, g = 5, my result is 4, and your result is 10. To find my secret number (a), they would have to solve this equation:
In our tiny example, they could easily guess the answer by trying a few numbers. But in the real world of cybersecurity, the prime number p is hundreds of digits long. Even the most powerful supercomputers in the world would need more time than the age of the universe to solve this log problem (in math, this is called the Discrete Logarithm Problem)!
And that is exactly how two computers, even if they have never met before, can establish an unbreakable shared password right across the open internet!