Diffie-Hellman algorithm is used to establish a shared secret between two parties which can be used for secret communication for exchanging data over a public network.

##### Pictorial Explanation:

The process begins by having the two parties, Alice and Bob, agree on an arbitrary starting color that does not need to be kept secret (but should be different every time); in this example the color is yellow. Each of them selects a secret color–red and aqua respectively–that they keep to themselves. The crucial part of the process is that Alice and Bob now mix their secret color together with their mutually shared color, resulting in orange and blue mixtures respectively, then publicly exchange the two mixed colors. Finally, each of the two mix together the color they received from the partner with their own private color. The result is a final color mixture (brown) that is identical to the partner’s color mixture.

It would be impossible for eavesdropper Eve to determine the common secret color.

##### Cryptographic Explanation:

The algorithm in itself is very simple. Let’s assume that Alice wants to establish a shared secret with Bob. Here is an example of the protocol with secret values in red.

- Alice and Bob agree to use a prime number
*p*= 23 and base*g*= 5. (These two values are chosen in this way to ensure that the resulting shared secret can take on any value from 1 to*p*–1).

- Alice chooses a secret integer
*a*= 6, then sends Bob*A*=*g*mod^{a}*p*(*A*= 5^{6}mod 23 = 8)

- Bob chooses a secret integer
*b*= 15, then sends Alice*B*=*g*mod^{b}*p*(*B*= 5^{15}mod 23 = 19)

- Alice computes
*s*=*B*mod^{a}*p*(*s*= 19^{6}mod 23 = 2)

- Bob computes
*s*=*A*mod^{b}*p*(*s*= 8^{15}mod 23 = 2)

- Alice and Bob now share a secret (the number 2).

The number Alice get at step 4 is same as Bob got at step 5 as

Bob computes

^{b}mod p = (g

^{a}mod p)

^{b}mod p = g

^{ab}mod p

Alice computes

^{a}mod p = (g

^{b}mod p)

^{a}mod p = g

^{ba}mod p

Diffie-Hellman algorithm is primarily used as a method of exchanging cryptography keys for use in symmetric encryption algorithms like AES. Please note that information is not shared during the key exchange. Here the two parties are creating a key together.

## C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <stdio.h> // Function to compute a^m mod n int compute(int a, int m, int n) { int r; int y = 1; while (m > 0) { r = m % 2; // fast exponention if (r == 1) y = (y*a) % n; a = a*a % n; m = m / 2; } return y; } // C program to demonstrate Diffie-Hellman algorithm int main() { int p = 23; // modulus int g = 5; // base int a, b; // a - Alice's Secret Key, b - Bob's Secret Key. int A, B; // A - Alice's Public Key, B - Bob's Public Key // choose secret integer for Alice's Pivate Key (only known to Alice) a = 6; // or use rand() // Calculate Alice's Public Key (Alice will send A to Bob) A = compute(g, a, p); // choose secret integer for Bob's Pivate Key (only known to Bob) b = 15; // or use rand() // Calculate Bob's Public Key (Bob will send B to Alice) B = compute(g, b, p); // Alice and Bob Exchanges their Public Key A & B with each other // Find Secret key int keyA = compute(B, a, p); int keyB = compute(A, b, p); printf("Alice's Secret Key is %d\nBob's Secret Key is %d", keyA, keyB); return 0; } |

**Output: **

Alice’s Secret Key is 2

Bob’s Secret Key is 2

**References:** https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂