-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa_serial.cpp
More file actions
58 lines (51 loc) · 1.48 KB
/
rsa_serial.cpp
File metadata and controls
58 lines (51 loc) · 1.48 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
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include "CycleTimer.h"
using namespace std;
typedef boost::multiprecision::cpp_int bigint;
pair<bigint, bigint> prime_factors(bigint n) {
if (!(n & 1)) { // n % 2 == 0
return {2, n>>1};
}
for (bigint i = 3; i * i <= n; i += 2) {
if (n % i == 0){
return {i, n/i};
}
}
return {0, 0};
}
bigint mod_inverse(bigint a, bigint m) {
bigint m0 = m;
bigint y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
bigint q = a / m; // q is quotient
bigint t = m;
m = a % m, a = t; // m is remainder
t = y;
y = x - q * y; // update y and x
x = t;
}
if (x < 0) x += m0; // make x positive
return x;
}
int main() {
bigint n("14282098901120359061");
bigint e = 65537;
double start = CycleTimer::currentSeconds();
pair<bigint, bigint> pq_pairs = prime_factors(n);
bigint p = pq_pairs.first, q = pq_pairs.second;
bigint m = (p - 1) * (q - 1);
bigint d = mod_inverse(e, m);
double end = CycleTimer::currentSeconds();
cout << "Public key:" << endl;
cout << "n = " << n << endl;
cout << "e = " << e << endl;
cout << "\nCrack result (private key):" << endl;
cout << "p = " << p << endl;
cout << "q = " << q << endl;
cout << "(p - 1) * (q - 1) = " << m << endl;
cout << "d = " << d << endl;
cout << "Cracked in " << end - start << " seconds." << endl;
return 0;
}