-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.cpp
114 lines (100 loc) · 2.78 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
#include <iostream>
#include <cmath>
#include <cstring>
#include "keygen.cpp"
using namespace std;
unsigned long long int ipow(unsigned long long int base, unsigned long long int exp)
{
unsigned long long int result = 1ULL;
while(exp)
{
if (exp & 1)
{
result *= (unsigned long long int)base;
}
exp >>= 1;
base *= base;
}
return result;
}
bool isPrime(unsigned long long prime)
{
unsigned long long i, j;
j = (unsigned long long)sqrt((long double)prime);
for (i = 2; i <= j; i++)
{
if (prime % i == 0)
{
return false;
}
}
return true;
}
unsigned long long calculate_d(unsigned long long e, unsigned long long t) {
unsigned long long d = 2;
while (true) {
if ((d * e) % t == 1) {
break;
}
++d;
}
return d;
}
unsigned long long calculate_e(unsigned long long t) {
unsigned long long e = 2;
while (true) {
if (t % e != 0) {
if (isPrime(e)) {
break;
}
}
++e;
}
return e;
}
string encrypt(const string &unencrypted, unsigned long long e, unsigned long long n) {
string result;
for (auto i : unencrypted) {
unsigned long long truncate = ipow(i - '0', e);
result.append(to_string(truncate % n));
result.append(":");
}
return result;
}
string decrypt(const string &encrypted, unsigned long long d, unsigned long long n) {
string result;
string buffer;
for (int i = 0; i < encrypted.size(); i++) {
if (encrypted[i] != ':') {
buffer.append(to_string(encrypted[i] - '0'));
}
else {
result.append(to_string((unsigned long long) ipow(stoi(buffer), d) % n));
buffer = "";
}
}
return result;
}
int main() {
unsigned long long p = 2;
unsigned long long q = 11;
unsigned long long n = p * q;
unsigned long long t = (p - 1) * (q - 1);
unsigned long long e = calculate_e(t);
unsigned long long d = calculate_d(e, t);
string unencrypted = "34222912143330352214312512212210";
//string encrypted = encrypt(unencrypted, e, n);
string encrypted;
cout << "Enter encrypted string -> ";
cin >> encrypted;
string decrypted = decrypt(encrypted, d, n);
char *decrypted_num = new char[decrypted.length() + 1];
strcpy(decrypted_num, decrypted.c_str());
decrypted = ntt(decrypted_num);
cout << "Text key -> " << decrypted << "\n";
///cout << "RSA Public Key -> " << "e:" << e << " n:" << n << "\n";
//cout << "RSA Private Key -> " << "d:" << d << " n:" << n << "\n";
cout << "Encrypted number -> " << encrypted << "\n";
cout << "Decrypted number -> " << decrypted << "\n";
return 0;
}