-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.cpp
More file actions
166 lines (119 loc) · 4.07 KB
/
encryption.cpp
File metadata and controls
166 lines (119 loc) · 4.07 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
#include <Security/Security.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
unsigned long long 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.");
}
unsigned long long number = 0;
for (int i = 0; i<length; i++ ) {
number |= (static_cast<unsigned long long>(buffer[i]) << (i * 8));
// std::cout << std::hex << (int)buffer[i] << " ";
}
return (int)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;
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
if (a == 0 || b == 0) {
return a;
}
if (a == b)
return a;
return gcd(b, a % b);
}
bool areCoprime(unsigned long long a, unsigned long long b) {
return gcd(a, b) == 1;
}
unsigned long long find_d(unsigned long long A, unsigned long long M) {
for (unsigned long long d = 1; d < M; ++d) {
if ((A * d) % M == 1) {
return d;
}
}
return 0; // Return 0 if no modular inverse is found (when A and M are not coprime)
}
long long mod_exp(unsigned long long base, unsigned long long exp, unsigned long long mod){
unsigned long long result =1 ;
unsigned long long 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<unsigned long long> encrypt(std::string str,unsigned long long e, unsigned long long n){
std::vector<unsigned long long > c;
for(auto i = str.begin(); i != str.end(); i++){
c.push_back(mod_exp(*i, e, n));
}
return c;
}
std::vector<unsigned long long> decrypt(std::vector<unsigned long long> crypted,unsigned long long d, unsigned long long n){
std::vector<unsigned long long > c;
for(auto i = 0; i < crypted.size(); i++){
c.push_back(mod_exp(crypted[i], d, n));
}
return c;
}
void generating_primes(unsigned long long& p, unsigned long long&q, size_t length){
p = generate_secure_random_bytes(length);
while(!isPrime(p)){
p = generate_secure_random_bytes(length);
}
// std::cout <<p << " ";
q = generate_secure_random_bytes(length);
while(!isPrime(q) || p==q){
q = generate_secure_random_bytes(length);
}
}
void find_e(unsigned long long& e, const unsigned long long phi, size_t length){
e = 3;
while(!areCoprime(phi,e) || e > phi ){
e = generate_secure_random_bytes(length);
}
}
int main(int argc, char* argv[]) {
size_t length = 2;
unsigned long long p;
unsigned long long q;
generating_primes(p, q, length);
unsigned long long n = (p*q);
unsigned long long phi = (p-1)*(q-1);
std::cout << "phi is " << phi << "\n";
unsigned long long e;
find_e(e, phi, length);
unsigned long long d = find_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 << " ";
}
std::cout << "\nM size is " << m.size() << "\n";
for(auto l: m){
std::cout <<(char)l << " ";
}
c.clear();
std::cout << "\n";
}
std::cout << std::dec << std::endl;
return 0;
}