Program:
/* This program calculates the Key for two personsusing the Diffie-Hellman Key exchange algorithm */#include <stdio.h>#include <math.h>// Power function to return value of a ^ b mod Plong long int power(long long int a, long long int b,long long int P){if (b == 1)return a;elsereturn (((long long int)pow(a, b)) % P);}// Driver programint main(){long long int P, G, x, a, y, b, ka, kb;// Both the persons will be agreed upon the// public keys G and PP = 23; // A prime number P is takenprintf("The value of P : %lld\n", P);G = 9; // A primitive root for P, G is takenprintf("The value of G : %lld\n\n", G);// Alice will choose the private key aa = 4; // a is the chosen private keyprintf("The private key a for Alice : %lld\n", a);x = power(G, a, P); // gets the generated key// Bob will choose the private key bb = 3; // b is the chosen private keyprintf("The private key b for Bob : %lld\n\n", b);y = power(G, b, P); // gets the generated key// Generating the secret key after the exchange// of keyska = power(y, a, P); // Secret key for Alicekb = power(x, b, P); // Secret key for Bobprintf("Secret key for the Alice is : %lld\n", ka);printf("Secret Key for the Bob is : %lld\n", kb);return 0;}
Output:
The value of P : 23
The value of G : 9
The private key a for Alice : 4
The private key b for Bob : 3
Secret key for the Alice is : 9
Secret Key for the Bob is : 9
0 Comments