This repository was archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
69 lines (53 loc) · 1.77 KB
/
main.cpp
File metadata and controls
69 lines (53 loc) · 1.77 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cmath>
#include <iostream>
#include <ctime>
using namespace std;
#define FIELD_MODULE 65521
#define PRIMITIVE_NUMBER 17
//return g ^ n mod P
long long int power(long long int g, long long int n, long long int P)
{
long long int res = 1;
for(int i = 0; i < n; i++) res = (res * g) % P;
return res;
}
int main()
{
string alice = "Alice";
string bob = "Bob";
srand(time(0));
long long int alice_private_key;
long long int alice_public_key;
long long int bob_private_key;
long long int bob_public_key;
long long int bob_session_key;
long long int alice_session_key;
cout << "Press: \n1 --- input user key\n2 --- input random key" << endl;
int flag;
cin >> flag;
switch (flag)
{
case 1:
cout << "Alice private key ---> ";
cin >> alice_private_key;
cout << "Bob public key --->";
cin >> bob_private_key;
break;
case 2:
alice_private_key = rand() % FIELD_MODULE;
bob_private_key = rand() % FIELD_MODULE;
break;
default:
break;
}
alice_public_key = power(PRIMITIVE_NUMBER, alice_private_key, FIELD_MODULE);
bob_public_key = power(PRIMITIVE_NUMBER, bob_private_key, FIELD_MODULE);
cout << "Alice sent her public key(" << alice_public_key << ") to Bob" << endl;
cout << "Bob sent her public key(" << bob_public_key << ") to Alice" << endl;
cout << "\n--------------------------------------------\n" << endl;
bob_session_key = power(alice_public_key, bob_private_key, FIELD_MODULE);
cout << "Bob session key = " << bob_session_key << endl;
alice_session_key = power(bob_public_key, alice_private_key, FIELD_MODULE);
cout << "Alice session key = " << bob_session_key << endl;
return 0;
}