-
Notifications
You must be signed in to change notification settings - Fork 1
/
myDES.cpp
517 lines (467 loc) · 11.7 KB
/
myDES.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "myDES.h"
#include "utility.h"
int main(int argc, char *argv[])
{
if (argc != 8)
{
cout << "Incorrect number of commandline arguments, expecting 6" << '\n';
cout << "Expected Syntax:" << '\n';
cout << "./a.out text.txt key.txt iv.txt output.txt -{cryptoFlag} -{keyFlag} -{ivFlag}" << '\n';
}
// Load flags
string cryptoFlag = argv[5];
string keyFlag = argv[6];
string ivFlag = argv[7];
// Initialize iv and key variables
string iv;
string key;
// Load or Generate key
if (keyFlag[1] == 'y')
key = generateRandomKey(argv[2]);
else if (keyFlag[1] == 'n')
key = loadFileToString(argv[2]);
else
{
cout << "Unrecognized key flag: " << keyFlag << '\n';
cout << "Quitting program." << '\n';
return 0;
}
// Load or Generate iv
if (ivFlag[1] == 'y')
iv = generateRandomIV(argv[3]);
else if (ivFlag[1] == 'n')
iv = loadFileToString(argv[3]);
else
{
cout << "Unrecognized IV flag: " << ivFlag << '\n';
cout << "Quitting program." << '\n';
return 0;
}
// Load user defined parameters into variables
string text = loadFileToString(argv[1]);
string fileName = argv[4];
// Perform proper crypto operation
switch(cryptoFlag[1])
{
case 'e':
{
encrypt(text, key, iv, fileName);
break;
}
case 'd':
{
decrypt(text, key, iv, fileName);
break;
}
default:
{
cout << "Error in parsing your flag" << '\n';
cout << "Accepted Flags:" << '\n';
cout << "Encrypt Decrypt" << '\n';
cout << "-e -d " << '\n';
break;
}
}
return 0;
}
string loadFileToString(string fileName)
{
ifstream fileStream (fileName, ifstream::in);
if(fileStream.fail())
cout << "error loading file: " << fileName << '\n';
string fileString;
char temp;
while(fileStream.good())
{
fileStream.get(temp);
// if (temp.empty())
// temp = '\n';
fileString+=temp;
}
// string fileString ((std::istreambuf_iterator<char>(fileStream)),
// std::istreambuf_iterator<char>());
return fileString;
}
void encrypt(std::string text, std::string key, std::string iv, std::string fileName)
{
string outputBinary = "";
string chainingBlock = hexToBinary(iv);
vector <string> subKeys = generateSubKeys(key);
vector <string> textBlocks = generateTextBlocks(text, true);
for (auto block : textBlocks)
{
string chainedBlock = xorString(block,chainingBlock);
string permutedBlock = permuteTextBlock(chainedBlock, true);
string feistelBlock = feistel(permutedBlock, subKeys, false);
string repermutedBlock = permuteTextBlock(feistelBlock, false);
chainingBlock = repermutedBlock;
outputBinary+=repermutedBlock;
}
string cryptedText = binaryToAscii(outputBinary);
writeToFile(fileName, cryptedText);
}
void decrypt(std::string text, std::string key, std::string iv, std::string fileName)
{
string outputBinary = "";
string chainingBlock = hexToBinary(iv);
vector <string> subKeys = generateSubKeys(key);
vector <string> textBlocks = generateTextBlocks(text, false);
for (int i = 0; i < textBlocks.size(); i++)
{
string block = textBlocks[i];
string permutedBlock = permuteTextBlock(block, true);
string feistelBlock = feistel(permutedBlock, subKeys, true);
string repermutedBlock = permuteTextBlock(feistelBlock,false);
string dechainedBlock = xorString(repermutedBlock, chainingBlock);
chainingBlock = block;
if (i == (textBlocks.size()-1))
{
string unpaddedBlock = unpadBlock(dechainedBlock);
outputBinary+=unpaddedBlock;
}
else
{
outputBinary+=dechainedBlock;
}
}
string cryptedText = binaryToAscii(outputBinary);
writeToFile(fileName, cryptedText);
}
string generateRandomKey(std::string keyFileName)
{
string key = "";
while (key.length() < 64)
{
auto time_now = std::chrono::high_resolution_clock::now();
std::chrono::nanoseconds nanos = time_now.time_since_epoch();
if (nanos.count()%2 == 0)
key+='0';
else
key+='1';
}
string hexKey = binToHex(key);
cout << "Random Key Generated: " << hexKey << '\n';
writeToFile(keyFileName, hexKey);
return hexKey;
}
string generateRandomIV(std::string ivFileName)
{
string iv = "";
while (iv.length() < 64)
{
auto time_now = std::chrono::high_resolution_clock::now();
std::chrono::nanoseconds nanos = time_now.time_since_epoch();
if (nanos.count()%2 == 0)
iv+='0';
else
iv+='1';
}
string hexIV = binToHex(iv);
cout << "Random IV Generated: " << hexIV << '\n';
writeToFile(ivFileName, hexIV);
return hexIV;
}
vector<string> generateSubKeys(std::string key)
{
vector<string> subKeys;
string binKey = hexToBinary(key);
string permutedKey = permuteKey(binKey, 1);
vector<string> subBlocks = generateSubBlocks(permutedKey);
for (auto elem : subBlocks)
subKeys.push_back(permuteKey(elem, 2));
return subKeys;
}
vector<string> generateTextBlocks(std::string text, bool isEncrypt)
{
vector<string> textBlocks;
int numOfBlocks = text.length() / 8;
int unfinishedBlock = text.length() % 8;
for (int i = 0; i < numOfBlocks; i++)
{
string textBinaryBlock = asciiToBinary(text.substr(i*8,8));
textBlocks.push_back(textBinaryBlock);
}
if(isEncrypt)
{
if (unfinishedBlock > 0)
{
string finalTextBlock = asciiToBinary(text.substr(numOfBlocks*8));
string finalHexBlock = binToHex(finalTextBlock);
string paddedBlock = padBlock(finalHexBlock);
string binPadBlock = hexToBinary(paddedBlock);
textBlocks.push_back(binPadBlock);
}
else
{
string fullPadBlock = "08080808";
textBlocks.push_back(fullPadBlock);
}
}
return textBlocks;
}
string padBlock(std::string text)
{
int blockSize = 16;
int padAmount = (blockSize - text.length())/2;
string binPad = decimalToBin(padAmount);
string hexPad = binToHex(binPad);
for (int i = 0; i < padAmount; i++)
text += ('0' + hexPad);
return text;
}
string unpadBlock(std::string text)
{
string hexText = binToHex(text);
char lastChar = hexText.back();
int padSize = lastChar - '0';
string trueString = hexText.substr(0,16-(padSize*2));
string trueBinString = hexToBinary(trueString);
return trueBinString;
}
string permuteTextBlock(std::string textBlock, bool initialPerm)
{
string permutedBlock;
if(initialPerm)
for (auto elem : IP)
permutedBlock+=textBlock[elem-1];
else
for (auto elem : FP)
permutedBlock+=textBlock[elem-1];
return permutedBlock;
}
string feistel(std::string permutedBlock, std::vector<std::string> subKeys, bool isDecryption)
{
string feistelBlock;
if(isDecryption)
reverse(subKeys.begin(), subKeys.end());
string left = permutedBlock.substr(0,32);
string right = permutedBlock.substr(32,32);
for(auto key : subKeys)
{
string tempLeft = left;
string tempRight = right;
left = tempRight;
right = feistelRight(tempLeft, tempRight, key);
}
feistelBlock = right + left;
return feistelBlock;
}
string feistelRight(std::string left, std::string right, std::string key)
{
string roundFunctionResult = roundFunction(right, key);
return xorString(left, roundFunctionResult);
}
string roundFunction(std::string right, std::string key)
{
string expanded = expand(right);
string keyed = xorString(expanded, key);
string subBoxed = subBox(keyed);
string permBoxed = permBox(subBoxed);
return permBoxed;
}
string expand(std::string text)
{
string expanded;
for (auto elem : E)
expanded.push_back(text[elem-1]);
return expanded;
}
string subBox(std::string keyed)
{
string subBoxed;
for(int i = 0; i < 8; i++)
{
string subBlock = keyed.substr(i*6,6);
string rowBin;
rowBin+=subBlock[0];
rowBin+=subBlock[5];
int row = binaryToDec(rowBin);
int column = binaryToDec(subBlock.substr(1,4));
int index = (row*16)+column;
int intSubbedBlock = SBOXMAP[i][index];
string subbedBlock = decimalToBin(intSubbedBlock);
subBoxed+=subbedBlock;
}
return subBoxed;
}
string permBox(std::string subBoxed)
{
string permBoxed;
for (auto elem : P)
permBoxed.push_back(subBoxed[elem-1]);
return permBoxed;
}
string permuteKey(std::string binKey, int PCBox)
{
string permutedKey;
switch(PCBox)
{
case 1:
{
for (auto elem : PC1)
permutedKey+=binKey[elem-1];
break;
}
case 2:
{
for (auto elem : PC2)
permutedKey+=binKey[elem-1];
break;
}
default:
{
cout << "this shouldn't be called" << '\n';
for (auto elem : PC2)
permutedKey+=binKey[elem-1];
break;
}
}
return permutedKey;
}
vector<string> generateSubBlocks(std::string permutedKey)
{
// number of times you shift based on current round of iteration
int leftshift[16] =
{ 1, 1, 2, 2,
2, 2, 2, 2,
1, 2, 2, 2,
2, 2, 2, 1 };
vector<string> subBlocks;
// Split the permuted key into two halves and shift them to generate intermediate values referred to as "blocks".
// These "blocks" are used to generate the sub keys.
string left = permutedKey.substr(0,28);
string right = permutedKey.substr(28,28);
// Perform a shift each round before rejoining the left and right sides into a new subBlock
for (int i = 0; i < 16; i++)
{
left = left.substr(leftshift[i]) + left.substr(0,leftshift[i]);
right = right.substr(leftshift[i]) + right.substr(0,leftshift[i]);
subBlocks.push_back(left + right);
}
return subBlocks;
}
string hexToBinary(std::string hex)
{
string bin;
for (auto elem : hex)
{
switch(toupper(elem))
{
case '0': bin+="0000"; break;
case '1': bin+="0001"; break;
case '2': bin+="0010"; break;
case '3': bin+="0011"; break;
case '4': bin+="0100"; break;
case '5': bin+="0101"; break;
case '6': bin+="0110"; break;
case '7': bin+="0111"; break;
case '8': bin+="1000"; break;
case '9': bin+="1001"; break;
case 'A': bin+="1010"; break;
case 'B': bin+="1011"; break;
case 'C': bin+="1100"; break;
case 'D': bin+="1101"; break;
case 'E': bin+="1110"; break;
case 'F': bin+="1111"; break;
default:
{
std::cout << "Unrecognized Hex Value: " << (int) elem << '\n';
break;
}
}
}
return bin;
}
string binToHex(std::string bin)
{
string hex = "";
for (int i = 0; i < bin.size(); i+=4)
{
string binChunk = bin.substr(i,4);
if (binChunk.compare("0000") == 0)
hex += '0';
else if (binChunk.compare("0001") == 0)
hex += '1';
else if (binChunk.compare("0010") == 0)
hex += '2';
else if (binChunk.compare("0011") == 0)
hex += '3';
else if (binChunk.compare("0100") == 0)
hex += '4';
else if (binChunk.compare("0101") == 0)
hex += '5';
else if (binChunk.compare("0110") == 0)
hex += '6';
else if (binChunk.compare("0111") == 0)
hex += '7';
else if (binChunk.compare("1000") == 0)
hex += '8';
else if (binChunk.compare("1001") == 0)
hex += '9';
else if (binChunk.compare("1010") == 0)
hex += 'A';
else if (binChunk.compare("1011") == 0)
hex += 'B';
else if (binChunk.compare("1100") == 0)
hex += 'C';
else if (binChunk.compare("1101") == 0)
hex += 'D';
else if (binChunk.compare("1110") == 0)
hex += 'E';
else if (binChunk.compare("1111") == 0)
hex += 'F';
}
return hex;
}
string asciiToBinary(std::string text)
{
string textBinary;
for (auto elem : text)
{
bitset<8> temp(elem);
textBinary+=temp.to_string();
}
return textBinary;
}
string binaryToAscii(std::string text)
{
string textAscii;
stringstream sstream(text);
while(sstream.good())
{
bitset<8> bits;
sstream >> bits;
char c = char(bits.to_ulong());
if ((int) c != 0)
textAscii += c;
}
return textAscii;
}
int binaryToDec(std::string val)
{
unsigned long ul = bitset<8>(val).to_ulong();
return (int) ul;
}
string decimalToBin(int val)
{
return bitset<4>(val).to_string();
}
string xorString(std::string val1, std::string val2)
{
string xored;
for (int i = 0; i < val1.length(); i++)
{
if (val1[i] == val2[i])
xored+='0';
else
xored+='1';
}
return xored;
}
void writeToFile(std::string fileName, std::string val)
{
ofstream myFile;
myFile.open(fileName);
myFile << val;
myFile.close();
}