-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption_optimized.cpp
More file actions
187 lines (134 loc) · 4.36 KB
/
encryption_optimized.cpp
File metadata and controls
187 lines (134 loc) · 4.36 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <Security/Security.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <gmpxx.h>
#include <string>
#include <sstream>
#include <stdexcept>
#include <iomanip>
std::string generate_secure_random_bytes(size_t length = 8) {
std::vector<unsigned char> buffer(length);
if (SecRandomCopyBytes(kSecRandomDefault, length, buffer.data()) != errSecSuccess) {
throw std::runtime_error("Failed to generate secure random bytes.");
}
// Convert each byte in buffer to a hexadecimal string
std::ostringstream oss;
for (size_t i = 0; i < length; i++) {
// Convert each byte to hexadecimal and add to the output stream
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(buffer[i]);
}
std::string number = oss.str();
return number;
}
bool isPrime(unsigned long long num) {
if (num <= 1) return false;
if (num == 2 || num == 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
unsigned long long limit = static_cast<unsigned long long>(std::sqrt(num));
for (unsigned long long i = 5; i <= limit; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
mpz_class gcd(mpz_class a, mpz_class b) {
if (a == 0 || b == 0) {
return a;
}
if (a == b)
return a;
return gcd(b, a % b);
}
bool areCoprime(mpz_class a, mpz_class b) {
return gcd(a, b) == 1;
}
mpz_class mod_exp(mpz_class base, mpz_class exp, mpz_class mod){
mpz_class result =1 ;
mpz_class prev = base % mod;
do{
if( (exp&1) == 1){
result = (result * prev) % mod;
}
exp= exp >> 1;
prev = (prev*prev) % mod;
}while(exp > 0);
return (result%mod);
}
std::vector<mpz_class> encrypt(std::string str,mpz_class e, mpz_class n){
std::vector<mpz_class > c;
for(auto i = str.begin(); i != str.end(); i++){
c.push_back(mod_exp(*i, e, n));
}
return c;
}
std::vector<mpz_class> decrypt(std::vector<mpz_class> crypted,mpz_class d, mpz_class n){
std::vector<mpz_class > c;
for(auto i = 0; i < crypted.size(); i++){
c.push_back(mod_exp(crypted[i], d, n));
}
return c;
}
void find_e(mpz_class& e, mpz_class phi, size_t length){
e = 65537;
while(!areCoprime(phi,e) || e > phi ){
e.set_str(generate_secure_random_bytes(length/2),16);
}
}
void find_d(mpz_class& d, mpz_class A, mpz_class M) {
mpz_class m0 = M;
mpz_class y = 0, x = 1;
if (M == 1) {
return; // No modular inverse exists if M == 1
}
while (A > 1) {
// q is quotient
mpz_class q = A / M;
mpz_class t = M;
// Apply Euclid's algorithm
M = A % M;
A = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive if necessary
if (x < 0) {
x += m0;
}
d = x;
}
int main(int argc, char* argv[]) {
size_t length = 64; //512 bit security
// std::string s = generate_secure_random_bytes(length);
mpz_class p(generate_secure_random_bytes(length),16); // Generate a random 512-bit number
mpz_nextprime(p.get_mpz_t(), p.get_mpz_t()); // Set p to the next prime after the random number
mpz_class q(generate_secure_random_bytes(length),16); // Generate a random 512-bit number
mpz_nextprime(q.get_mpz_t(), q.get_mpz_t()); // Set p to the next prime after the random number
mpz_class n = p*q;
mpz_class phi = (p-1)*(q-1);
// std::cout << n << "\n";
std::cout << "phi is " << phi << "\n";
mpz_class e;
mpz_class d;
find_e(e, phi, length);
find_d(d, e,phi);
std::cout << "Public key in format(e, n) is (" << e << ", " << n << ")\n";
std::cout << "secret key in format(d, n) is (" << d << ", " << n << ")\n";
for(int i =1; i< argc; i++){
auto c = encrypt(argv[i], e, n);
auto m = decrypt(c, d, n);
std::cout << "Encryption of \"" << argv[i] << "\" is: ";
for(auto l: c){
std::cout << l << "\n";
}
std::cout << "\nM size is " << m.size() << "\n";
for(auto l: m){
std::cout << static_cast<char>(l.get_ui()) << " ";
}
c.clear();
std::cout << "\n";
}
// std::cout << std::dec << std::endl;
return 0;
}