# Why does encrypting the Diffie-Hellman value with the other side’s public key prevent man-in-the middle?

As far as I understood about Diffie-Hellman, it is easy for the man in the middle to easily decrypt and read the message.

• Diffie-Hellman is a key exchange protocol, not an encryption scheme. Do you perhaps mean ElGamal? In any case, please describe how a man in the middle would decrypt the message, and someone will be able to tell you whether your method is valid or not. – bkjvbx Oct 2 '16 at 9:02
• I wrote an extensive answer, but the problem remains that 1) "the Diffie-Hellman value" is not well defined and 2) no values within DH are actually encrypted; raising something to a power modulo N is not the same as encryption. So the question itself contains incorrect assumptions, making it impossible to explain the problem without explaining DH in its entirety. – Maarten Bodewes Oct 2 '16 at 10:12
• In man-in-the-middle scenario, the other party key is MITM key, so preventing is still hard. Should one get the right key of the other party, no MITM anymore. – Vadym Fedyukovych Oct 3 '16 at 10:03

It's because, Eve (attacker) can't break the encrypted data since Eve doesn't have other side's private key. Only by using other side's private key, the encrypted data can be decrypted.

And yes, this is general technique for preventing Man-In-the-Middle attack using asymmetric keys. Diffie-Hellman is Symmetric Key Exchange Protocol, not encryption mechanism.

First two numbers are generated g and p where p is prime.

One side:

$R_1 = g^x \bmod p$ where $x$ is randomly chosen

Send $R_1$ to other side, Receive $R_2$

Calculate key as $K = {R_2}^x \bmod p$

Other side:

$R_2 = g^y \bmod p$ where $y$ is randomly chosen

Send $R_2$ to other side, Receive $R_1$

Calculate key as $K = {R_1}^y \bmod p$

Also, Diffie-Hellman doesn't prevent Man-In-The-Middle; it's one of Diffie-Hellman's weakness and it's vulnerable to it.

• Thanks @Raoul722 for the edit..I couldn't format it well! :-) – kiner_shah Oct 3 '16 at 14:06