To implement Diffie-hallman key exchange algorithm in C. (WITH OUTPUT IMAGE)



#include<stdio.h>
#include<conio.h>
#include<math.h>


int mod(int a,int b,int c)
{
   double x=1,div1;
   int i,mod1,div;

   for(i=1;i<=b;i++)
   {x=x*a;}

// printf("\n Exponent is %lf",x);

 div1=x/c;
 div=x/c;
// printf("\n devided %lf",div1);
 //printf("\n div %d",div);

 div1=div1-div;
 //printf("\n new div %lf",div1);
 mod1=div1*c;
// mod1=ceill(mod1);
 return mod1;
}

void main()
{int q,alpha,k,xa,ya,xb,yb;
clrscr();

printf("Enter prime number q::");
scanf("%d",&q);

printf("\n Enter primitive root (alpha) of q less than q::");
scanf("%d",&alpha);
//printf("%d",alpha);
//public key generation at sender A.
printf("\n Enter sender's privte key Xa::");
scanf("%d",&xa);
//printf("%d",xa);

ya=mod(alpha,xa,q);
printf("\n::::::public key calculated at sender A::::%d",ya);


//public key generation at receiver B.
printf("\n\nEnter reciver's privte key Xb::");
scanf("%d",&xb);
//printf("%d",xb);

yb=mod(alpha,xb,q);
printf("\n::::::public key calculated at receiver B::::%d",yb);

//secret key generation at sender.
k=mod(yb,xa,q);
printf("\n\n::::::secret key K calcualted at sender:::::%d",k);
k=0;
//secret key generation at receiver.
k=mod(ya,xb,q);
printf("\n\n::::::secret key K calcualted at receiver:::::%d",k);

getch();
}

OUTPUT:



Comments