-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkey_pair_generator.cpp
More file actions
37 lines (26 loc) · 1.01 KB
/
Copy pathkey_pair_generator.cpp
File metadata and controls
37 lines (26 loc) · 1.01 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
#include <iostream>
#include "key_pair_generator.h"
using namespace std;
void KeyPairGenerator::generate() {
Prime prime;
Numbers numbers;
cout << "Generating p..." << endl;
bignum p( prime.generate_prime(length) );
cout << "Generating q..." << endl;
bignum q( prime.generate_prime(length) );
cout << "Calculating n..." << endl;
bignum n( p * q );
bignum one(1);
cout << "Computing fi(n)..." << endl;
bignum fi_n( n - p - q + one ); // fi(n) = (p - 1)(q - 1) if p,q are prime numbers.
cout << "Choosing e..." << endl;
bignum e( numbers.coprime_for(fi_n) ); // 1 < e < fi(n) and gcd(e, fi(n)) = 1
cout << "Computing d..." << endl;
bignum d( modinv(e, fi_n) );
cout << "Saving private key..." << endl;
IO::write_key( private_key_path, n, d ); // private
cout << "Saving public key..." << endl;
IO::write_key( public_key_path, n, e );
cout << "Successfully done!" << endl;
cout << "You can find your private key in file: `" << private_key_path << "` and public: `" << public_key_path << "`." << endl;
}