-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDH.c
45 lines (32 loc) · 760 Bytes
/
DH.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<math.h>
int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if (y % 2 == 1)
res = (res*x) % p;
y = y/2;
x = (x*x) % p;
}
return res;
}
// C program to demonstrate Diffie-Hellman algorithm
int main()
{
int p = 23;
int g = 5;
int pvtA, pvtB;
int pubA, pubB;
pvtA = rand();
pubA = power(g, pvtA, p);
pvtB = rand();
pubB = power(g, pvtB, p);
int keyA = power(pubB, pvtA, p);
int keyB = power(pubA, pvtB, p);
printf("Alice's Secret Key is %d\nBob's Secret Key is %d", keyA, keyB);
return 0;
}