-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffie.cpp
More file actions
52 lines (41 loc) · 957 Bytes
/
Copy pathdiffie.cpp
File metadata and controls
52 lines (41 loc) · 957 Bytes
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
/*
SHIV's code for Diffie Hellman key exchange
*/
#include <bits/stdc++.h>
int ModPow(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) == 1){
res = (res*x) % p;
std::cout << res << " = " <<(res*x) << " mod(" << p << ")\n";
}
y = y>>1;
x = (x*x) % p;
}
return res;
}
int main()
{
long long int P, pr, Xa, Xb, Ya, Yb, K1, K2;
std::cout << "Enter a prime number P: ";
std::cin >> P;
std::cout << "Enter primitive root for P: ";
std::cin >> pr;
std::cout << "Enter Xa for Shiv: ";
std::cin >> Xa;
Ya = ModPow(pr, Xa, P);
std::cout << "Ya is: " << Ya << "\n";
std::cout << "Enter Xb for Batman: ";
std::cin >> Xb;
Yb = ModPow(pr, Xb, P);
std::cout << "Yb is: " << Yb << "\n";
std::cout << "\nValidating key exchange:\n";
K1 = ModPow(Yb, Xa, P);
std::cout << "Key on Shiv's side: " << K1 << "\n\n";
K2 = ModPow(Ya, Xb, P);
std::cout << "Key on Batman's side: " << K2 << "\n";
return 0;
}