Loading...
Explanation of the MITM (Man In The Middle) with the Diffie-Hellmann key exchange
We need to make some changes: separate the explanation from the code, add a subpart about the MITM and maybe to develop more the instructions
Let's say Alice and Bob want to exchange a secret over an insecure channel. In other words, anyone can read the messages they send, but the goal is to ensure that only Alice and Bob can calculate the secret key.
Diffie-Hellman key exchange provides a solution to this seemingly impossible task. Since code may be easier to understand than a detailed explanation, I'll provide it first:
Here's a brief explanation of the code:
We choose a prime and a generator
Alice picks a private key
Bob picks a private key
Alice's public key is
Bob's public key is
Their shared key is
So anybody observing the messages sent between Alice and Bob would see , but they wouldn't be able to calculate the shared key .
This is because given and , it should be infeasible to calculate . If this sounds familiar, that's because it's the Discrete Log Problem.
The original paper can be found here. It uses the group of integers modulo a prime to perform the key exchange. In practice however, any group with a hard discrete log problem can be used.