The Diffie-Hellman algorithm is being used to establish a shared secret that can be used for secret communications while exchanging data over a public network using the elliptic curve to generate points and get the secret key using the parameters.
#include<stdio.h>
#include<math.h>
long long int power(long long int a, long long int b,
long long int P)
{
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
printf("Enter the value of P: ");
scanf("%lld", &P);
printf("Enter the value of G: ");
scanf("%lld", &G);
printf("Enter the value of private key for A, a: ");
scanf("%lld", &a);
x = power(G, a, P);
printf("Enter the value of private key for B, b: ");
scanf("%lld", &b);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
printf("Secret key for the Alice is : %lld\n", ka);
printf("Secret Key for the Bob is : %lld\n", kb);
return 0;
}