-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffie_hellman.cpp
68 lines (57 loc) · 2.21 KB
/
diffie_hellman.cpp
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
#include<iostream>
#include<cstdio>
class DiffieHellman
{
public:
uint64_t common_prime_number, primitive_root,
alice_secret_number, alice_private_key,
bob_secret_number, bob_private_key,
alice_public_key, bob_public_key;
DiffieHellman(uint64_t common_prime, uint64_t primitive_root, uint64_t secret_num_1, uint64_t secret_num_2)
{
alice_private_key = power(primitive_root, secret_num_1, common_prime);
bob_private_key = power(primitive_root, secret_num_2, common_prime);
alice_public_key = power(bob_private_key, secret_num_1, common_prime);
bob_public_key = power(alice_private_key, secret_num_2, common_prime);
}
uint64_t power(uint64_t base, uint64_t exponent, uint64_t modulo)
{
if (exponent == 1)
{
return base;
}
uint64_t s = power(base, exponent / 2, modulo);
if (exponent % 2 == 0)
{
return (s * s) % modulo;
}
else
{
return (((s * s) % modulo) * base) % modulo;
}
}
};
int
main()
{
uint64_t common_prime_number = 0 , primitive_root = 0,
alice_secret_number = 0, alice_private_key = 0,
bob_secret_number = 0, bob_private_key = 0,
alice_public_key = 0, bob_public_key = 0;
std::cout << "Enter the common prime number and primitive root that Alice And Bob agreed on: ";
std::cin >> common_prime_number >> primitive_root;
std::cout << "\n";
std::cout << "Enter the secret number Alice chose: ";
std::cin >> alice_secret_number;
std::cout << "\n";
std::cout << "Enter the secret number Bob chose: ";
std::cin >> bob_secret_number;
DiffieHellman diffie_hellmann(common_prime_number, primitive_root, alice_secret_number, bob_secret_number);
std::cout << "\n";
std::cout << "Alice's private key: " << diffie_hellmann.alice_private_key << "\n";
std::cout << "Bob's private key: " << diffie_hellmann.bob_private_key << "\n";
std::cout << "\n";
std::cout << "Alice's public key: " << diffie_hellmann.alice_public_key << "\n";
std::cout << "Bob's public key: " << diffie_hellmann.bob_public_key << "\n";
return 0;
}