In: Computer Science
Encode Diffie Hellamn exchange in software by writing a small program that accepts the values of p and g, and randomly generates the secret numbers SA and SB, and derives the Diffie Hellman secret.
Test it on the following examples:
p = 11, g = 13
p = 7, g = 17
p = 17, g = 13
please find the code in c
#include<stdio.h>
long int findKey(int a,int b,int mod)
{
long long int t;
if(b==1)
return a;
t=findKey(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return (((t*t)%mod)*a)%mod;
}
long long int calculateKey(int a,int x,int n)
{
return findKey(a,x,n);
}
int main()
{
int n,g,x,a,y,b;
printf("Enter the value of p and g : ");
scanf("%d%d",&n,&g);
printf("Enter the value for x for first person");
scanf("%d",&x);
a=findKey(g,x,n);
printf("Enter value for y for second person");
scanf("%d",&y);
b=findKey(g,y,n);
printf("key for the first person is :
%lld\n",findKey(b,x,n));
printf("key for the second person is :
%lld\n",findKey(a,y,n));
return 0;
}
