Friday, 31 August 2018

Implement Diffie-Hellmen Key exchange Method in c.


Introduction:
The first published public-key algorithm appeared in the seminal paper by Diffie and Hillman that defined public-key cryptography and is generally referred to as Diffie-Hillman key exchange.
The purpose of the algorithm is to enable two users to securely exchange a key that can then be used for subsequent symmetric encryption of messages. Its purpose is limited to exchange of secret value only.
The Diffie-Hillman algorithm depends for its effectiveness on the difficulty of computing discrete logarithms.
There are two publicly known numbers: a prime number q and an integer α that is primitive root of q.
Suppose the users A and B wish to exchange a key.
User A selects a random integer XA < q and computes YA = αXA mod q.
Similarly User B selects indepently a random integer XB< q and computes YB = αXB mod q.
Each side keeps X value Private and makes the Y value publicly available to other side.
User A computes the key as K=YBXA mod q and user B computes K=YAXB mod q.
We need to prove that K calculated on both side are equal.

K=YBXA mod q
   = (αXA mod q) XA mod q
=(αXB )XA  mod q
   =αXBXA  mod q
=(αXA )XB  mod q
=(αXA mod q)XB  mod q
   = YAXB mod q
 


C code:
// Diffie-Hillman
#include<stdio.h>
#include<conio.h>
#include<math.h>

long int power(long int a, long int j, long int c)
{
            long int f,i;
            f=1;
            for(i=1;i<=j;i++)
            {
                        f=(f*a)%c;
            }
            f=f%c;
            return f;
}

void main()
{
            long int q,alpha,xa,xb,ya,yb,key1,key2,ya1,yb1;
            clrscr();
            {
                        printf("enter the value of q:");
                        scanf("%ld",&q);
                        printf("enter the value of alpha:");
                        scanf("%ld",&alpha);
                        if(alpha<q)
                        {
                                    printf("enter the value of xa:");
                                    scanf("%ld",&xa);
                                    printf("enter the value of xb:");
                                    scanf("%ld",&xb);
                                    ya=(power(alpha,xa,q));
                                    printf("\nvalue of ya is:%ld",ya);
                                    yb=power(alpha,xb,q);
                                    printf("\nvalue of yb is:%ld",yb);
                                    key1=power(yb,xa,q);
                                    printf("\nvalue of key is:%ld",key1);
                                    key2=power(ya,xb,q);
                                    printf("\nvalue of key is:%ld",key2);
                        }

                        else
                        {
                                    printf("\n no result");
                        }
                        getch();
                        }
}
 


Output:
 

THANK YOU!!!!

No comments:

Post a Comment