-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.cpp
145 lines (108 loc) · 3.03 KB
/
rsa.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
template<typename T>
bool isPrime(T n) {
if (n <= 1) return false;
for (T i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
uint32_t gcd(uint32_t a, uint32_t b) {
if (b > a) swap(a, b);
if (a % b == 0) return b;
return gcd(b, a % b);
}
// 유클리드 호제법 확장
// a*x + b*y = gcd(a, b), a < b 일 때 정수 x, y를 반환
std::pair<int64_t, int64_t> xgcd(int64_t a, int64_t b) {
int64_t x[3] = {0, 1, 1}, y[3] = {1, 0, 0};
while (b % a) {
int64_t q = b / a;
x[2] = x[0] - q*x[1];
y[2] = y[0] - q*y[1];
x[0] = x[1]; x[1] = x[2];
y[0] = y[1]; y[1] = y[2];
int64_t rest = b % a;
b = a;
a = rest;
}
return std::make_pair(x[2], y[2]);
}
// 재귀적으로 구하는 함수
std::pair<int64_t, int64_t> xgcd2(int64_t a, int64_t b) {
if (a == 0)
return std::make_pair(0, 1);
auto next = xgcd2(b % a, a);
int64_t x = next.first;
int64_t y = next.second;
return std::make_pair(y - (b/a)*x, x);
}
uint32_t privateKey(uint32_t e, uint32_t En) {
// int64_t ret = xgcd2(e, En).first;
int64_t ret = xgcd(e, En).first;
if (ret < 0)
ret += En;
if (ret > En)
ret %= En;
return ret;
}
uint32_t coprimeKey(uint32_t n) {
uint32_t ret = 1;
int count = 10;
for (uint32_t i = n - 1; i > 1; i--) {
if (gcd(n, i) == 1) {
ret = i;
if (--count == 0)
break;
}
}
return ret;
}
uint64_t fastPow(uint64_t base, uint64_t exp, uint32_t mod) {
uint64_t result = 1;
while (exp > 0) {
if (exp % 2 == 1) {
result = (result*base) % mod;
}
base = (base * base) % mod;
exp = exp / 2;
}
return result;
}
uint16_t p = 47111;
uint16_t q = 64237;
int main() {
uint32_t n, En, e, d;
cout << "p: " << p << " q: " << q << endl;
if (not isPrime(p) || not isPrime(q)) {
cout << "p와 q중에 소수가 아닌 수가 있습니다" << endl;
return 0;
}
n = (uint32_t)(p * q);
En = ((uint32_t)(p - 1) * (uint32_t)(q - 1));
e = coprimeKey(En);
d = privateKey(e, En);
cout << "Pi(pq): " << En << " e: " << e << " d: " << d << endl;
// private key valied check
uint64_t isValid = ((uint64_t)e*(uint64_t)d) % (uint64_t)En;
if (isValid != 1) {
cout << "public key, private key 쌍이 유효하지 않습니다" << endl;
return 0;
}
while (1) {
uint32_t data, encoded, decoded;
cout << "암호화 할 자연수를 입력하세요. ("<< n << " 미만)\n";
cout << "data: "; cin >> data;
if (data >= n) break;
encoded = fastPow(data, e, n);
decoded = fastPow(encoded, d, n);
cout << "encoded data: " << encoded << std::endl;
cout << "decoded data: " << decoded << std::endl;
}
return 0;
}