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.
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.
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 = ga mod p (A = 56 mod 23 = 8)
- Bob chooses a secret integer b = 15, then sends Alice B = gb mod p (B = 515 mod 23 = 19)
- Alice computes s = Ba mod p (s = 196 mod 23 = 2)
- Bob computes s = Ab mod p (s = 815 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
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.
// Function to compute a^m mod n
int compute(int a, int m, int n)
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;
// C program to demonstrate Diffie-Hellman algorithm
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);
Alice’s Secret Key is 2
Bob’s Secret Key is 2
Thanks for reading.
Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂