-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cipher_Algorithms.cpp
269 lines (235 loc) · 5.86 KB
/
Cipher_Algorithms.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* This file contains the implementation of the algorithms used to create ciphers.
*
* That includes:
* -------------------------------
* caesar cipher
* RSA encryp and decrypt
* base64 encrypt and ecrypt
* -------------------------------
*
*/
#include <bits/stdc++.h>
using namespace std;
const string menu = "Enter 1 to print menu\n"
"Enter 2 to rotate every character in the string\n"
"Enter 3 to encode it to base64\n"
"Enter 4 to decode it from base64\n"
"Enter 5 to RSA encrypt a number\n"
"Enter 6 to RSA decrypt a number\n"
"Enter 7 to end program\n"
"-->";
const string charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
* The function binpow() is used for RSA encryption and ecryption.
* It uses the concept of binary exponentiation under modulus.
* It takes three parameters , the number to be encrypted/decrypted (a), the encryption key(b), the modulo number(m)
*/
long long binpow(long long a, long long b, long long m)
{
a %= m;
long long res = 1;
while (b > 0)
{
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
/*
* The function rotateCharacters is used to rotate every character by some factor.
*/
void rotateCharacters(string s, int val)
{
val %= 26;
for (auto &i : s)
{
if (i >= 'A' and i <= 'Z')
i = (i - 'A' + val) % 26 + 'A';
}
cout << s << endl;
return;
}
/*
* The function encodeToBase64 is used to encode the givent text into base64.
*/
void encodeToBase64(string str)
{
string result = "";
int i;
int j;
int temp;
int index;
int len = str.length();
int numberOfBits = 0;
int padding = 0;
int value = 0;
int count = 0;
for (i = 0; i < len; i += 3)
{
value = 0;
count = 0;
numberOfBits = 0;
for (j = i; j < len && j <= i + 2; j++)
{
value = value << 8;
value = value | str[j];
count += 1;
}
numberOfBits = 8 * count;
padding = numberOfBits % 3;
while (numberOfBits != 0)
{
if (numberOfBits >= 6)
{
temp = numberOfBits - 6;
index = (value >> temp) & 63;
numberOfBits -= 6;
}
else
{
temp = 6 - numberOfBits;
index = (value << temp) & 63;
numberOfBits = 0;
}
result.push_back(charSet[index]);
}
}
for (i = 1; i <= padding; i++)
{
result.push_back('=');
}
cout << result << endl;
return;
}
int findIndexInCharSet(char c)
{
for (int i = 0; i < 64; i++)
{
if (c == charSet[i])
{
return i;
}
}
return -1;
}
/*
* The function decodeToBase64 is used to decode the given base64 into raw data text.
*/
void decodeFromBase64(string str)
{
string result = "";
int i;
int j;
int value = 0;
int numberOfBits = 0;
int len = str.length();
for (i = 0; i < len; i += 4)
{
value = 0;
numberOfBits = 0;
for (j = i; j < len && j <= i + 3; j++)
{
if (str[j] != '=')
{
value = value << 6;
numberOfBits += 6;
}
int idx = findIndexInCharSet(str[j]);
if (idx != -1)
{
value = value | idx;
}
else
{
value = value >> 2;
numberOfBits -= 2;
}
}
while (numberOfBits != 0)
{
numberOfBits -= 8;
result.push_back((value >> numberOfBits) & 255);
}
}
cout << result << endl;
return;
}
int main()
{
while (1)
{
string s;
int val;
cout << menu;
int option;
cin >> option;
switch (option)
{
case 1:
cout<<menu;
break;
case 2:
cout << "Enter the input string: ";
cin >> s;
cout << "Enter the number of character places to rotate:";
cin >> val;
cout << "\nOutput string: " << endl;
rotateCharacters(s, val);
cout << endl;
break;
case 3:
cout << "Enter the input string: ";
cin >> s;
cout << "\nOutput string: " << endl;
encodeToBase64(s);
cout << endl;
break;
case 4:
cout << "Enter the input string: ";
cin >> s;
cout << "\nOutput string: " << endl;
decodeFromBase64(s);
cout << endl;
break;
case 5:
{
cout << "Enter the value to be encrypted: ";
long long num;
cin >> num;
cout << "Enter value of e: ";
long long e;
cin >> e;
cout << "Enter value of N: ";
long long N;
cin >> N;
long long ans = binpow(num, e, N);
cout << "Cipher is : " << ans << endl<<endl;
break;
}
case 6:
{
cout << "Enter the value to be decrypted: ";
long long num;
cin >> num;
cout << "Enter value of d: ";
long long d;
cin >> d;
cout << "Enter value of N: ";
long long N;
cin >> N;
long long ans = binpow(num, d, N);
cout << "Cipher is : " << ans << endl<<endl;
break;
}
case 7:
return 0;
default:
cout << "Enter a valid option" << endl;
break;
}
}
return 0;
}