Diffie-Hellman Key Exchange.
Imagine Bob and Alice want to send secret messages to each other and Trudy is trying to read these messages. How can Bob and Alice keep Trudy from eavesdropping on their conversation? The answer is they use an encryption algorithm that has a key. You can picture this as the put the message in a box and use a lock (an encryption algorithm) that has a key. The Diffie-Hellman key exchange algorithm is a set of steps that allows Bob and Alice to both get a copy of the key without the risk of Trudy getting a copy.
The Diffie-Hellman key exchange algorithm is a very clever way to exchange an encryption key between 2 machines without the risk of revealing the key to a 3rd party that may be trying to eavesdrop. It uses mathematical properties that allow both Bob and Alice to generate the same key without ever sending the key, so even if Trudy got all the messages deciding what key to use, she would not be able to generate the key.
For some context as to why this is important, say Bob wants to encrypt his messages but needs Alice to be able to decrypt them. So he picks the key 27 and sends it to Alice so she can decrypt his messages. If Trudy is listening to for messages between Bob and Alice when bob sends the key, then Trudy can decrypt everything Bob sends and even pretend to be bob by using his key and sending messages to Alice. So imagine this same situation if you were communication with a bank or other service. Trudy could not only see everything you are doing, she could take your password (that you encrypted to prove to the bank your identity) and rob you blind! With Diffie-Hellman the key is never sent between Bob and Alice, it is generated separately by both Bob and Alice in a way that Trudy can’t replicate even if she is eavesdropping.
I’ll walk you through the steps with Bob and Alice communicating while Trudy tries to eavesdrop.
Bob and Alice start by agreeing upon 2 numbers P, a prime number, and G, the generator number.
In this example Bob and Alice decide P = 23, and G = 5.
Bob’s Info Alice’s Info Trudy’s Info P = 23
G = 5 P = 23
G = 5 P = 23
G = 5
Next Bob picks a random secret number Sbob, and Alice picks a random secret number Salice.
Bob picks Sbob = 15, and Alice picks Salice = 6.
Bob’s Info Alice’s Info Trudy’s Info P = 23
G = 5 P = 23
G = 5 P = 23
G = 5 Sbob = 15 Salice = 6 Nothing
Bob and Alice both use their secret along with the function T = GS mod P then send them to each other. Bob generates Tbob = 515 mod 23 and finds TBob = 19, Bob then sends 19 to Alice. Alice does the same after finding Talice = 56 mod 23 = 2.
Bob’s Info Alice’s Info Trudy’s Info P = 23
G = 5 P = 23
G = 5 P = 23
G = 5 Sbob = 15 Salice = 6 Nothing Tbob = 19
Talice = 2 Talice = 2
Tbob = 19 Tbob = 19
Talice = 2
Bob and Alice now have all the information they need to generate the key they will use for encryption. They both use the function key = TS mod p. to get the key. Bob uses keybob = TaliceSbob mod P and gets keybob = 2. Alice uses keyalice = TbobSalice mod P and gets keyalice = 2.
Bob’s Info Alice’s Info Trudy’s Info P = 23
G = 5 P = 23
G = 5 P = 23
G = 5 Sbob = 15 Salice = 6 Nothing Tbob = 19
Talice = 2 Talice = 2
Tbob = 19 Tbob = 19
Talice = 2 Keybob = 2 Keyalice = 2 Nothing
Bob and Alice can now communicate using the key they generated and Trudy cannot generate the key without one of the secret integers that Bob or Alice picked, But since the secret numbers are never transmitted Trudy has no idea what they are. The only option would be for her to guess the key until she finds the right one, for this example this would happen very quickly but in practice this algorithm uses huge numbers. Numbers that would be very difficult to show an example with.
This works because of how modulus and exponents work. Modulus makes sure you can’t simply find the secret value Bob or Alice used by working backwards. Use of exponents makes sure that both parties arrive at the same key.
Modulus if you don’t know is a mathematical operation that finds the remainder of a division. So, 5 mod 3 = 2 because the remainder of 5/3 is 2, 3 only goes into 5 once and the remaining value is 2. So in Diffie-Hellman if we had a P = 5 and calculate a T = 1 the secret could be 2 or 4 but there is now way to tell which. So if Trudy tried to generate the key she would not know what power to use on the T to get the key.
The main property of exponents used here is that (TSbob mod P)Salice is the same as (TSalice mod P)Sbob. Bob and Alice are essentially combining both their secrets numbers together without actually exchanging them.
The key remains a secret from Trudy because she needs either Bob’s or Alice’s secret number to generate the key. Trudy is unable to reverse engineer the secret number, and Bob and Alice apply their key to G before sending it to the other person to add their secret to G as well.
Diffie-Hellman Key Exchange. was originally published on Cole Talks