-
Notifications
You must be signed in to change notification settings - Fork 2
/
fescpp.cpp
289 lines (245 loc) · 7.17 KB
/
fescpp.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <files.h>
#include <modes.h>
#include <osrng.h>
#include "cryptlib.h"
#include "rijndael.h"
#include "hex.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <default.h>
#include <math.h>
#include <string>
#include <rsa.h>
#include <cassert>
using namespace std;
using namespace CryptoPP;
//Functions for basic encryption start from here
int encryp() {
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the File name: ";
cin >> gets_s(fileName); //Getting the file to encrypt
fps.open(fileName, fstream::in);
if (!fps)//If the file is not present then...
{
cout << "\nError!, Opening the Source File (to Read)!";
return 0;
}
fpt.open("tmp.txt", fstream::out);//We create a temporary file to store cipher text
if (!fpt)
{
cout << "\nError!, Opening/Creating the tmp File!";
return 0;
}
while (fps >> noskipws >> ch) // adding 100 to the characters without skipping white spaces
{
ch = ch + 100;
fpt << ch;
}
fps.close();
fpt.close();
fps.open(fileName, fstream::out);
if (!fps)
{
cout << "\nError!, Opening the Source File (to write)!";
return 0;
}
fpt.open("tmp.txt", fstream::in);//The cipher text is written to the original file.
if (!fpt)
{
cout << "\nError!, Opening the tmp File!";
return 0;
}
while (fpt >> noskipws >> ch)
fps << ch;
fps.close();
fpt.close();
cout << "\nFile '" << fileName << "' File Encrypted Successfully!";
cout << endl;
};
int decryp() {
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the Name of File: ";
cin >> gets_s(fileName);
fps.open(fileName, fstream::out);
if (!fps)
{
cout << "\nError Occurred while Opening the Source File!";
return 0;
}
fpt.open("tmp.txt", fstream::in);
if (!fpt)
{
cout << "\nError Occurred while Opening/Creating tmp File!";
return 0;
}
while (fpt >> noskipws >> ch)
{
ch = ch - 100;
fps << ch;
}
fps.close();
fpt.close();
cout << "\nFile '" << fileName << "' Decrypted Successfully!";
cout << endl;
};
//Functions for basic encryption end over here
//Functions for aes encryption start from here
int aesencrypt() {
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the File name: ";
cin >> gets_s(fileName); //Getting the file to encrypt
fps.open(fileName, fstream::in);
if (!fps)//If the file is not present then...
{
cout << "\nError!, Opening the Source File (to Read)!";
return 0;
}
string password;
string encrypted;
password = "SecureComms123";
cout << "Password (authentication tag): " << password << endl;
FileSource fs1(fileName, true,
new DefaultEncryptorWithMAC(
(byte*)&password[0], password.size(),
new HexEncoder(
new StringSink(encrypted)
)
)
);
cout << "Encrypted: \n\n" << encrypted << endl;
};
int aesdecrypt() {
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the File name: ";
cin >> gets_s(fileName); //Getting the file to encrypt
fps.open(fileName, fstream::in);
if (!fps)//If the file is not present then...
{
cout << "\nError!, Opening the Source File (to Read)!";
return 0;
}
string password = "SecureComms123";
cout << "Password (authentication tag): " << password << endl;
string encrypted, decrypted;
FileSource fs1(fileName, true,
new DefaultEncryptorWithMAC(
(byte*)&password[0], password.size(),
new HexEncoder(
new StringSink(encrypted)
)
)
);
StringSource ss1(encrypted, true,
new HexDecoder(
new DefaultDecryptorWithMAC(
(byte*)&password[0], password.size(),
new StringSink(decrypted)
)
)
);
cout << "Decrypted: \n\n" << decrypted << endl;
};
//Functions for aes encryption end over here
//Functions for rsa encryption start over here
int rsaencrypt() {
// Generate keys
AutoSeededRandomPool rng;
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 1536);
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
string cipher, recovered;
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the File name: ";
cin >> gets_s(fileName); //Getting the file to encrypt
fps.open(fileName, fstream::in);
if (!fps)//If the file is not present then...
{
cout << "\nError!, Opening the Source File (to Read)!";
return 0;
}
RSAES_OAEP_SHA_Encryptor e(publicKey);
FileSource f1(fileName, true,
new PK_EncryptorFilter(rng, e,
new StringSink(cipher)
) // PK_EncryptorFilter
);
cout << "Encrypted File Contents:\n\n" << cipher;
}
int rsadecrypt() {
AutoSeededRandomPool rng;
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 1536);
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
char fileName[30], ch;
string cipher, recovered;
fstream fps, fpt;
cout << "Enter the File name: ";
cin >> gets_s(fileName); //Getting the file to encrypt
fps.open(fileName, fstream::in);
if (!fps)//If the file is not present then...
{
cout << "\nError!, Opening the Source File (to Read)!";
return 0;
}
RSAES_OAEP_SHA_Encryptor e(publicKey);
FileSource f1(fileName, true,
new PK_EncryptorFilter(rng, e,
new StringSink(cipher)
) // PK_EncryptorFilter
);
RSAES_OAEP_SHA_Decryptor d(privateKey);
StringSource ss2(cipher, true,
new PK_DecryptorFilter(rng, d,
new StringSink(recovered)
) // PK_DecryptorFilter
); // StringSource
cout << "Decrypted File Contents:\n\n" << recovered;
assert(fileName == recovered);
}
//Functions for rsa encryption end over here
//Main function
//Switch case used for a simple GUI
int main()
{
int ch;
while (1)
{
cout << "\n\n===FILE ENCRYPTION SYSTEM===\n";
cout << "\n[1] Basic Encryption\n[2] AES encryption\n[3] RSA encryption\n[4] Basic decryption\n[5] AES decryption\n[6] RSA decryption\n[7] Exit\n\n";
cout << "Enter choice:\t";
cin >> ch;
switch(ch){
case 1:
encryp();
break;
case 2:
aesencrypt();
break;
case 3:
rsaencrypt();
break;
case 4:
decryp();
break;
case 5:
aesdecrypt();
break;
case 6:
rsadecrypt();
break;
case 7:
exit(1);
break;
default:"Invalid Choice";
}
}
return 0;
}