-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffie_hellman_alice_and_bob.c
30 lines (27 loc) · 1.12 KB
/
diffie_hellman_alice_and_bob.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
#include<stdio.h>
#include<math.h>
int main(){
int p = 23; //public known (prime number)
int g =5; //public known (primitive root)
int x =4; //only Alice know
int y =3; //only Bob know
double aliceSend = fmod(pow(g,x), p);
double bobComputer = fmod(pow(aliceSend, y), p);
double bobSend = fmod(pow(g,y), p);
double aliceComputer = fmod(pow(bobSend,x), p);
double sharedSecret = fmod(pow(g, x*y), p);
printf("Simulation of Diffie- Hellman key exchange algorithm \n");
printf("-----------------------------------------------------\n");
printf("Alice sends: %.0f\n", aliceSend);
printf("Bob computes: %.0f\n", bobComputer);
printf("Bob sends: %.0f\n", bobSend);
printf("Alice computes: %.0f\n", aliceComputer);
printf("Shared secret: %.0f\n", sharedSecret);
//shared secret should match and equality is transitive
if(aliceComputer == sharedSecret && aliceComputer == bobComputer){
printf("Success: Shared secret matches %.0f\n", sharedSecret);
}else {
printf("Error: Shared secret does not match\n");
}
return 0;
}