27, నవంబర్ 2012, మంగళవారం

Diffie-Hellman Example

#include <stdio.h>
#include <math.h>
void main()
{
int q,alpha,xa,xb,ya,yb,ka,kb, x,y,z,count,ai[20][20];
printf("Enter a Prime Number \"q\":");
scanf("%d",&q);
printf("Enter a No \"xa\" which is lessthan value of q:");
scanf("%d",&xa);
printf("Enter a No \"xb\" which is lessthan value of q:");
scanf("%d",&xb);
for(x=0;x<q-1;x++) //Primitive Root Calculation
for(y=0;y<q-1;y++)
ai[x][y] = ((int)pow(x+1,y+1))%q;
for(x=0;x<q-1;x++)
{
count = 0;
for(y=0;y<q-2;y++)
{
for(z=y+1;z<q-1;z++)
if(ai[x][y] == ai[x][z])
{
count = 1;
break;
}
if(count == 1)
break;
}
if (count == 0 )
{
alpha = x+1;
break;
}
}
printf("alpha = %d\n",alpha);
ya = ((int)pow(alpha,xa))%q; yb = ((int)pow(alpha,xb))%q;
ka = ((int)pow(yb,xa))%q; kb = ((int)pow(yb,xb))%q;
printf("ya = %d\nyb = %d\nka = %d\nkb = %d\n",ya,yb,ka,kb);
 
 
if(ka == kb)
 printf("The keys exchanged are same");
else 
printf("The keys exchanged are not same");
}


  • Here is an example of Duffie-Hellman using small numbers.
    • The eavesdropper monitors what is sent between the sender and the recipient, but does not alter the contents of their communications.
      a = sender's private key  a = 3
      b = recipient's private key  b = 4
      g = public base  g = 1
      p = public (prime) number  p = 5
      s = shared secret key  s = 1
Sender
knows does not know
p = 5 b = 4
base g = 2
a = 3
23 mod 5 = 3
2b mod 5 = 1
13 mod 5 = 1
3b mod 5 = 1
13 mod 5 = 3b mod 5
s = 1
Recipient
knows does not know
p = 5 a = 3
base g = 2
b = 4
24 mod 5 = 1
2a mod 5 = 3
34 mod 5 = 1
1a mod 5 = 1
34 mod 5 = 1a mod 5
s = 1
Eavesdropper
knows does not know
p = 5 a = 3
base g = 2 b = 4

s = 1
2a mod 5 = 3
2b mod 5 = 1
1a mod 5 = s
3b mod 5 = s
1a mod 5 = 3b mod 5

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి