Saturday, August 8, 2020

Simple Diffie–Hellman Key Exchange Example in C.

Diffie-Hellman Key Exchange Method


DiffieHellman key exchange method allows two parties that have no prior knowledge of each other to jointly establish a shared secret key over an insecure channel. This key can then be used to encrypt subsequent communications using a symmetric key cipher.



EXAMPLE :-

#include<stdio.h>
long int power(int p,int q,int mod)
{
long long int s;
if(q==1)
return p;
s=power(p
,q/2,mod);
if(q%2==0)
return (s*s)%mod;
else
return (((s*s)%mod)*p)%mod;
}
long long
int calculateKey(int p,int x,int n)
{
return power(p,x,n);
}
int main()
{
int n,g,x,p,y,q;
printf("Enter the value of n and g : ");
scanf("%d%d",&n,&g);
printf("Enter the value of x for the first person : ");
scanf("%d",&x); p=power(g,x,n);
printf("Enter the value of y for the second person : ");
scanf("%d",&y); q=power(g,y,n);
printf("key for the first person is : %lld\n",power(q,x,n));
printf("key for the second person is : %lld\n",power(p,y,n));
return 0;
}


OUTPUT

Enter the value of n and g : 5
23
Enter the value of x for the first person : 3
Enter the value of y for the second person : 5
key for the first person is : 2
key for the second person is : 2

No comments:

Post a Comment

Please do not any spam link in Comment Box