Diffie Hellman Key Exchange Algorithm
Published: 2020-10-15 16:21:22

_**Introduction:**_ Diffie Hellman key exchange algorithm is a method for securely or secretly exchanging cryptographic keys or a key use in encryption or decryption over a public communications channel or away. Keys are not eventually exchanged, they are joint and derived. It is named after their inventors who invented this algorithm i.e. Whitfield Diffie and Martin Hellman.

_**Working Principle:**_ The Diffie-Hellman key exchange works like mixing colors by exchanging key colors. 
Let’s say we have two users, Alice and Bob.
* They both agree to use random color which is known(public) to everyone (i.e. yellow).
* They both have selected a private color for themselves (red and sea-green).
* Before sharing a color, both mix their private color with public color.
* Both exchange the received output colors with each other.
* For identity verification, both mix their private color in the new color which they received from each other.
* After mixing private color both will have identical color as final color, which identifies the exchange of color is successful i.e. they have securely exchanged the keys.
* If the attacker has intercepted the color exchange, it would be difficult to determine the secret color.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7x001gvjhyepghoe6ovw.PNG)

_**Actual Working:**_
Diffie Hellman uses two numbers:
1) A large Prime number (**p**)
2) The primitive root of the prime number (**q**)

 The prime number (**p**) and the primitive root (**q**) are known to everyone i.e. they are public keys.
 Now the sender (Alice) and the receiver (Bob) have to select a private key for transmission.
 Let Alice select private key as '**r**' and Bob select its private key as '**s**'.
 Similar to color mixing, Alice and Bob use the below equation to mix their secret keys into public keys:
                       _**X = q^r mod p**_ 
                       _**Y = q^s mod p**_
 Now, values of X and Y are exchanged between them.
 The sender (Alice) computes _**A = Y^r mod p**_ and the receiver (Bob) computes _**B = X^s mod p**_.

>Let's take an example for a better understanding.
Let p = 23 and q = 5 (primitive root of 23)
Alice chooses r = 2 and Bob selects s = 3
Therefore,
 X = 5^2 mod 23
 X = 25 mod 23
 X = 2

 >Y = 5^3 mod 23
 Y = 125 mod 23
 Y = 10

 >Now Alice computes: 
 A = 10^2 mod 23 ;
 A = 8.
 And Bob calculates:
 B = 2^3 mod 23 ;
 B = 8.
 From the above computation, we get A = B, which shows that they have securely exchanged the keys.

_**Limitations:**_ The most serious limitation of Diffie-Hellman in its basic or "pure" form is the absence of authentication. Communications using Diffie-Hellman all by itself are vulnerable to man in the middle attacks used in any cipher. Ideally, Diffie-Hellman should be used or need in conjunction with an admit authentication method such as digital signatures to verify the identities of the person using this signature in a bank or somewhere over the public communications medium. Diffie-Hellman is well suited for use in data communication but is less often used for data stored or archived over long periods in this kind of cipher.

 _**References:**_  
1. [Wikipedia] (https://en.wikipedia.org/wiki/Diffie–Hellman_key_exchange)
2. [Dzone] (https://dzone.com/articles/diffie-hellman-key-exchange-2)