-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1.cpp
More file actions
1068 lines (931 loc) 路 25.8 KB
/
Copy pathLab1.cpp
File metadata and controls
1068 lines (931 loc) 路 25.8 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//LAB1 Coding DES, AES using cryptopp library
#include "cryptopp/osrng.h"
using CryptoPP::AutoSeededRandomPool;
#include <iostream>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
//reading from files
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::wcin;
using std::wcout;
using std::wstring;
#include <limits>
#include <string>
using std::string;
#include <codecvt>
#include <locale>
#include <cstdlib>
using std::exit;
#include "cryptopp/cryptlib.h"
using CryptoPP::AAD_CHANNEL;
using CryptoPP::BufferedTransformation;
using CryptoPP::DEFAULT_CHANNEL;
using CryptoPP::Exception;
#include "cryptopp/hex.h"
using CryptoPP::HexDecoder;
using CryptoPP::HexEncoder;
#include "cryptopp/filters.h"
using CryptoPP::AuthenticatedDecryptionFilter;
using CryptoPP::AuthenticatedEncryptionFilter;
using CryptoPP::Redirector;
using CryptoPP::StreamTransformationFilter;
using CryptoPP::StringSink;
using CryptoPP::StringSource;
#include "cryptopp/des.h"
using CryptoPP::DES;
#include "cryptopp/aes.h"
using CryptoPP::AES;
#include "cryptopp/modes.h"
using CryptoPP::CBC_Mode;
using CryptoPP::CFB_Mode;
using CryptoPP::CTR_Mode;
using CryptoPP::ECB_Mode;
using CryptoPP::OFB_Mode;
#include "cryptopp/xts.h"
using CryptoPP::XTS;
#include "cryptopp/ccm.h"
using CryptoPP::CCM;
#include "cryptopp/gcm.h"
using CryptoPP::GCM;
using CryptoPP::GCM_TablesOption;
#include "cryptopp/secblock.h"
using CryptoPP::SecByteBlock;
#include "cryptopp/files.h"
using CryptoPP::FileSink;
using CryptoPP::FileSource;
#include <assert.h>
#define N_ITER 10000
// Convert string to wstring
wstring s2ws(const std::string &str)
{
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
return converter.from_bytes(str);
}
// Convert wstring to string
string ws2s(const std::wstring &wstr)
{
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
return converter.to_bytes(wstr);
}
// Pretty print SecByteBlock as a hex wstring
void PrettyPrint(SecByteBlock byte_block)
{
// Convert the byte_block to a hex wstring,
// and print to console
string encoded_string;
StringSource(byte_block, byte_block.size(), true,
new HexEncoder(
new StringSink(encoded_string)));
wstring wstr = s2ws(encoded_string);
wcout << wstr << endl;
}
// Pretty print Cryptopp::byte array as a hex wstring
void PrettyPrint(CryptoPP::byte *bytes_array)
{
// Convert the bytes_array to a hex wstring,
// and print to console
string encoded_string;
StringSource(bytes_array, sizeof(bytes_array), true,
new HexEncoder(
new StringSink(encoded_string)));
wstring wstr = s2ws(encoded_string);
wcout << wstr << endl;
}
// Pretty print byte string as a hex wstring
void PrettyPrint(string str)
{
// Convert byte string to a hex wstring,
// and print to console.
string encoded_string;
StringSource(str, true,
new HexEncoder(
new StringSink(encoded_string)));
wstring wstr = s2ws(encoded_string);
wcout << wstr << endl;
}
// a template for encryption of various modes of operation
// Mode is 'm<DES>::Encryption' in which 'm' is the actual mode of DES
template <class Mode>
void Encrypt(const string &plain, Mode &e, string &cipher)
{
cipher.clear();
// StringSource acts as a pipeliner which intakes 'plain' as input,
// uses StreamTransformationFilter to perform transformation on the input `plain`.
// StreamTransformationFilter adds padding and invokes the Encryption object `e`
// to perform encryption on the plaintext 'plain'.
// The result (recovered plaintext) is stored in 'recovered' variable.
try
{
StringSource(plain, true,
new StreamTransformationFilter(e, new StringSink(cipher)));
}
catch (const CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
}
// a template for encryption of various modes of operation
// Mode is 'm<DES>::Decryption' in which 'm' is the actual mode of DES
template <class Mode>
void Decrypt(const string &cipher, Mode &d, string &recovered)
{
recovered.clear();
// StringSource acts as a pipeliner which intakes 'cipher" as input,
// uses StreamTransformationFilter to perform transformation on the `cipher`.
// StreamTransformationFilter removes padding and invokes the Decryption object `d`
// to perform decryption on the ciphertext 'cipher'.
// The result (recovered plaintext) is stored in 'recovered' variable.
try
{
StringSource(cipher, true,
new StreamTransformationFilter(d,
new StringSink(recovered)));
}
catch (const CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
}
// The 'key', 'ciphertext' and 'recovered' will be changed in place.
// This function returns the time (in ms) to perform DES algorithm in 1 time.
template <class Encryption, class Decryption>
double *Encrypt_Decrypt(const SecByteBlock &key, string plaintext, string &ciphertext, string &recovered)
{
// clock() return the current clock tick of the processor
// Get starting clock tick of encryption
int start_e = clock();
// Declare new Encryption object
Encryption e;
// Attach the key to the Encryption object
try
{
e.SetKey(key, key.size());
}
catch (CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
// Perform encryption
Encrypt<Encryption>(plaintext, e, ciphertext);
// Get ending clock tick of encryption
int end_e = clock();
// Get starting clock tick of decryption
int start_d = clock();
// Declare the new Decryption object
Decryption d;
// Attach the key to the Decryption object
try
{
d.SetKey(key, key.size());
}
catch (const CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
// Perform decryption
Decrypt<Decryption>(ciphertext, d, recovered);
// Get ending clock tick of decryption
int end_d = clock();
// Calculate execution time (in ms) of encryption and decryption individually
double *etime = new double[2];
etime[0] = double(end_e - start_e) / CLOCKS_PER_SEC * 1000;
etime[1] = double(end_d - start_d) / CLOCKS_PER_SEC * 1000;
// Return execution time
return etime;
}
// A template to perform encryption and decryption with various modes of operation that use IV
// The 'key', iv, 'ciphertext' and 'recovered' will be changed in place.
// This function return the time (in ms) to perform DES algorithm in 1 time.
template <class Encryption, class Decryption>
double *Encrypt_Decrypt_withIV(const SecByteBlock &key, const SecByteBlock &iv, string plaintext, string &ciphertext, string &recovered)
{
// clock() return the current clock tick of the processor
// Get the starting clock tick of encryption
int start_e = clock();
// Declare new Encryption object
Encryption e;
// Attach the key to the Encryption object
try
{
e.SetKeyWithIV(key, key.size(), iv);
}
catch (const CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
// Perform encryption
Encrypt<Encryption>(plaintext, e, ciphertext);
// Get ending clock tick of encryption
int end_e = clock();
// Get starting clock tick of decryption
int start_d = clock();
// Declare the new Decryption object
Decryption d;
// Attach the key to the decryption object
try
{
d.SetKeyWithIV(key, key.size(), iv);
}
catch (const CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
// Perform decryption
Decrypt<Decryption>(ciphertext, d, recovered);
// Get ending clock tick of decryption
int end_d = clock();
// Calculate execution time (in ms) of encryption and decryption individually
double *etime = new double[2];
etime[0] = double(end_e - start_e) / CLOCKS_PER_SEC * 1000;
etime[1] = double(end_d - start_d) / CLOCKS_PER_SEC * 1000;
return etime;
}
// A template to perform DES with various modes of operation that use IV.
// The 'key', iv, 'ciphertext' and 'recovered' are changed in place;
// therefore, the last value of them can be used to displayed on console as an example.
// The number of iteration is pre-defined as 'N_ITER'.
// This function returns the total execution time (in ms) of N_ITER iterations.
template <class Encryption, class Decryption>
double *Looping_IV(const SecByteBlock &key, const SecByteBlock &iv, string plaintext, string &ciphertext, string &recovered)
{
// first element relates to the encryption time
// second element relates to the decryption time
double *sum = new double[2];
double *etime = NULL;
sum[0] = 0;
sum[1] = 0;
for (int i = 0; i < N_ITER; ++i)
{
etime = Encrypt_Decrypt_withIV<Encryption, Decryption>(key, iv, plaintext, ciphertext, recovered);
sum[0] += etime[0];
sum[1] += etime[1];
}
delete[] etime;
return sum;
}
// A template to perform DES with various modes of operation that don't use IV.
// The 'key', iv, 'ciphertext' and 'recovered' are changed in place;
// therefore, the last value of them can be used to displayed on console as an example.
// The number of iteration is pre-defined as 'N_ITER'.
// This function returns the total execution time (in ms) of N_ITER iterations.
template <class Encryption, class Decryption>
double *Looping_nonIV(const SecByteBlock &key, string plaintext, string &ciphertext, string &recovered)
{
// first element relates to the encryption time
// second element relates to the decryption time
double *sum = new double[2];
double *etime = NULL;
sum[0] = 0;
sum[1] = 0;
for (int i = 0; i < N_ITER; ++i)
{
etime = Encrypt_Decrypt<Encryption, Decryption>(key, plaintext, ciphertext, recovered);
sum[0] += etime[0];
sum[1] += etime[1];
}
delete[] etime;
return sum;
}
template <class Encryption, class Decryption>
double *Encrypt_Decrypt_withAuthentication(const SecByteBlock &key, const SecByteBlock &iv, string plaintext, string auth, string &ciphertext, string &recovered_plaintext, string &recovered_auth)
{
ciphertext.clear();
recovered_plaintext.clear();
// [START ENCRYPTION]
int start_e = clock();
const int TAG_SIZE = 8;
try
{
Encryption enc;
// Attach key and IV
enc.SetKeyWithIV(key, key.size(), iv, iv.size());
// Not required for GCM, but required for CCM
enc.SpecifyDataLengths(auth.size(), plaintext.size(), 0);
AuthenticatedEncryptionFilter ef(enc,
new StringSink(ciphertext), false, TAG_SIZE);
// Put authenticated data to the authenticated channel which only provides authentication
ef.ChannelPut(AAD_CHANNEL, (const CryptoPP::byte *)auth.data(), auth.size());
ef.ChannelMessageEnd(AAD_CHANNEL);
// Put plaintext to the default channel which provides confidentiality and authentiation
ef.ChannelPut(DEFAULT_CHANNEL, (const CryptoPP::byte *)plaintext.data(), plaintext.size());
ef.ChannelMessageEnd(DEFAULT_CHANNEL);
}
catch (CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
int end_e = clock();
// [END ENRYPTION]
// [START DECRYPTION]
int start_d = clock();
try
{
// Split the ciphertext into encrypted data and MAC value
string encrypted_data = ciphertext.substr(0, ciphertext.size() - TAG_SIZE);
string mac = ciphertext.substr(ciphertext.size() - TAG_SIZE);
// Authenticated data is sent via a clear channel
recovered_auth = auth;
Decryption dec;
// Attach the key and IV
dec.SetKeyWithIV(key, key.size(), iv, iv.size());
dec.SpecifyDataLengths(recovered_auth.size(), encrypted_data.size(), 0);
AuthenticatedDecryptionFilter df(dec, NULL,
AuthenticatedDecryptionFilter::MAC_AT_BEGIN |
AuthenticatedDecryptionFilter::THROW_EXCEPTION,
TAG_SIZE);
df.ChannelPut(DEFAULT_CHANNEL, (const CryptoPP::byte *)mac.data(), mac.size());
df.ChannelPut(AAD_CHANNEL, (const CryptoPP::byte *)auth.data(), auth.size());
df.ChannelPut(DEFAULT_CHANNEL, (const CryptoPP::byte *)encrypted_data.data(), encrypted_data.size());
df.ChannelMessageEnd(AAD_CHANNEL);
df.ChannelMessageEnd(DEFAULT_CHANNEL);
// Check data's integrity
bool b = false;
b = df.GetLastResult();
assert(true == b);
// Retrieve confidential data from channel
df.SetRetrievalChannel(DEFAULT_CHANNEL);
size_t n = (size_t)df.MaxRetrievable();
recovered_plaintext.resize(n);
if (n > 0)
{
df.Get((CryptoPP::byte *)recovered_plaintext.data(), n);
}
}
catch (CryptoPP::Exception &ex)
{
wcout << ex.what() << endl;
exit(1);
}
int end_d = clock();
// [END DECRYPTION]
double *etime = new double[2];
etime[0] = double(end_e - start_e) / CLOCKS_PER_SEC * 1000;
etime[1] = double(end_d - start_d) / CLOCKS_PER_SEC * 1000;
return etime;
}
template <class Encryption, class Decryption>
double *Looping_Authentication(const SecByteBlock &key, const SecByteBlock &iv, string plaintext, string auth, string &ciphertext, string &recovered_plaintext, string &recovered_auth)
{
double *sum = new double[2];
double *etime = NULL;
sum[0] = 0;
sum[1] = 0;
for (int i = 0; i < N_ITER; ++i)
{
etime = Encrypt_Decrypt_withAuthentication<Encryption, Decryption>(key, iv, plaintext, auth, ciphertext, recovered_plaintext, recovered_auth);
sum[0] += etime[0];
sum[1] += etime[1];
}
delete[] etime;
return sum;
}
string GraspAuthenticatedData()
{
wstring wadata;
wcout << L"Authenticated data: ";
fflush(stdin);
#ifdef __linux__
getline(wcin, wadata);
getline(wcin, wadata);
#endif
getline(wcin, wadata);
string adata = ws2s(wadata);
return adata;
}
// Setup for Vietnamese support
void SetupVietnameseSupport()
{
#ifdef _WIN32
_setmode(_fileno(stdin), _O_U16TEXT);
_setmode(_fileno(stdout), _O_U16TEXT);
#elif __linux__
setlocale(LC_ALL, "");
#endif
}
// Select mode of operation
int SelectMode(bool is_AES)
{
int mode;
wcout << L"Choose a mode of operation (choose the number):\n";
wcout << L"(1) ECB\n";
wcout << L"(2) CBC\n";
wcout << L"(3) CFB\n";
wcout << L"(4) OFB\n";
wcout << L"(5) CTR\n";
if (is_AES)
{
wcout << L"(6) XTS\n";
wcout << L"(7) GCM\n";
wcout << L"(8) CCM\n";
}
wcout << L"> ";
try
{
wcin >> mode;
// if mode is of type 'int' but not within the valid range
if (mode < 1 || (mode > 8 && is_AES) || (mode > 5 && !is_AES))
{
wcout << L"Invalid mode!" << endl;
exit(1);
}
// otherwise
return mode;
}
catch (...)
{
// If an error occurs
wcout << L"Invalid mode!" << endl;
exit(1);
}
}
// Select DES/AES
int SelectScheme()
{
wcout << L"Please choose the scheme:" << endl;
wcout << L"(1) DES" << endl;
wcout << L"(2) AES" << endl;
wcout << L"> ";
int scheme;
try
{
wcin >> scheme;
// if scheme if of type 'int' but not of valid values
if (scheme != 1 && scheme != 2)
{
wcout << L"Invalid Scheme !" << endl;
exit(1);
}
// otherwise
return scheme;
}
catch (...)
{
// if an error occurs
wcout << L"Invalid Scheme !" << endl;
exit(1);
}
}
// Select AES key size
int SelectKeySize(int mode)
{
const int key_sizes[] = {16, 24, 32, 64};
wcout << L"Please choose the key size of AES:" << endl;
if (mode != 6)
{
wcout << L"(1) 128 bits ~ 16 bytes (default)\n";
wcout << L"(2) 192 bits ~ 24 bytes\n";
wcout << L"(3) 256 bits ~ 32 bytes\n";
}
if (mode == 6)
{
wcout << L"(1) 256 bits ~ 32 bytes\n";
wcout << L"(2) 512 bits ~ 64 bytes\n";
}
wcout << L"> ";
int option;
try
{
wcin >> option;
if (mode != 6 && option >= 1 && option <= 3)
{
return key_sizes[option - 1];
}
else if (mode == 6 && option >= 1 && option <= 2)
{
return key_sizes[option + 1];
}
else
{
wcout << L"Invalid Key size !" << endl;
exit(1);
}
}
catch (...)
{
wcout << L"Invalid Key size !" << endl;
exit(1);
}
}
// Select IV's size in AES
int SelectIVSize(int mode)
{
wcout << L"Please choose the IV size or using default value :" << endl;
wcout << L"(1) Automatic " << endl;
wcout << L"(2) Using default value" << endl;
wcout << L"> ";
int option, sz;
try
{
wcin >> option;
if (option == 1)
{
if (mode == 7)
{
wcout << L"IV size: ";
wcin >> sz;
}
else if (mode == 8)
{
wcout << L"IV size [7, 13]: ";
wcin >> sz;
if (sz < 7 || sz > 13)
{
wcout << L"Invalid IV size !" << endl;
exit(1);
}
}
}
else if (option == 2)
{
if (mode == 7)
{
sz = AES::BLOCKSIZE;
}
else if (mode == 8)
{
sz = 8;
}
}
}
catch (const std::exception &e)
{
wcout << L"Invalid IV size!" << endl;
exit(1);
}
return sz;
}
// Acquire a string from console and convert to SecByteBlock in place.
// Return true if succeed.
void GraspInputFromConsole(SecByteBlock &block, int block_size, wstring which)
{
try
{
// Acquire a string from console
wstring winput;
wcout << L"Input " + which + L": ";
fflush(stdin);
getline(wcin, winput);
string input = ws2s(winput);
// Convert to bytes
StringSource ss(input, false);
CryptoPP::ArraySink bytes_block(block, block_size);
ss.Detach(new Redirector(bytes_block));
ss.Pump(block_size);
}
catch (...)
{
wcout << L"Error !" << endl;
exit(1);
}
}
// Generate the key/IV based on option (manual or random).
// Return true if succeed.
void GenerateSecByteBlock(SecByteBlock &block, int block_size, wstring which, int scheme)
{
wcout << L"Input " + which + L", random or reading " + which + L" from file:\n";
wcout << L"(1) Input " + which << endl;
wcout << L"(2) Random " + which << endl;
wcout << L"(3) Read " + which << L" from file" << endl;
wcout << L"> ";
int option;
try
{
wcin >> option;
if (option == 1)
{
block = SecByteBlock(block_size);
GraspInputFromConsole(block, block_size, which);
}
else if (option == 2)
{
AutoSeededRandomPool prng;
block = SecByteBlock(block_size);
prng.GenerateBlock(block, block_size);
}
else if (option == 3)
{
block = SecByteBlock(block_size);
if (scheme == 1 && which == L"key")
{
#ifdef _WIN32
FileSource fs("des_key.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#elif __linux__
FileSource fs("des_key.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#endif
}
else if (scheme == 1 && which == L"IV")
{
#ifdef _WIN32
FileSource fs("des_iv.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#elif __linux__
FileSource fs("des_iv.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#endif
}
else if (scheme == 2 && which == L"key")
{
#ifdef _WIN32
FileSource fs("aes_key.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#elif __linux__
FileSource fs("aes_key.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#endif
}
else
{
#ifdef _WIN32
FileSource fs("aes_iv.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#elif __linux__
FileSource fs("aes_iv.key", false);
CryptoPP::ArraySink bytes_block(block, block_size);
fs.Detach(new Redirector(bytes_block));
fs.Pump(block_size);
#endif
}
}
else
{
wcout << L"Choosing is invalid!" << endl;
exit(1);
}
}
catch (...)
{
wcout << L"Error for creating block !!" << endl;
exit(1);
}
}
int main(int argc, char *argv[])
{
// Setup for Vietnamese support
SetupVietnameseSupport();
// Declaration
AutoSeededRandomPool prng;
CryptoPP::SecByteBlock key;
CryptoPP::SecByteBlock iv;
// declaration plaintext
wstring wplaintext, wciphertext, wrecoveredtext;
string plaintext, ciphertext, recoveredtext;
// Declaration inputfile
ifstream input_file;
int Choose; // simulation variable
//MENU
while (true)
{
wcout<<"Please choose the size of the input string !!\n";
wcout<<"-------------------***----------------------\n";
wcout<<"1) 1KB\n";
wcout<<"2) 10KB\n";
wcout<<"3) 50KB\n";
wcout<<"4) 100KB\n";
wcout<<"5) 1MB\n";
wcout<<"6) 5MB\n";
wcout<<">>";
wcin>>Choose;
if (Choose < 1 || Choose > 6)
{
wcout<<"Not FOUND, Please Choose again !!\n";
}
else break;
}
//selection code
switch (Choose)
{
case 1 :
{
input_file.open("Input_files/1KB.txt");
break;
}
case 2 :
{
input_file.open("Input_files/10KB.txt");
break;
}
case 3 :
{
input_file.open("Input_files/50KB.txt");
break;
}
case 4 :
{
input_file.open("Input_files/100KB.txt");
break;
}
case 5 :
{
input_file.open("Input_files/1MB.txt");
break;
}
case 6 :
{
input_file.open("Input_files/5MB.txt");
break;
}
default: break;
}
//convert to plaintext
input_file >> plaintext;
cout<<"plaintext : "<<plaintext;
// Convert wstring 'wplaintext' to string 'plaintext' for the algorithm to work properly
//plaintext = ws2s(wplaintext);
// Select scheme
int scheme = SelectScheme();
// Select mode
int mode;
if (scheme == 1)
{
mode = SelectMode(false);
}
else if (scheme == 2)
{
mode = SelectMode(true);
}
double *etime = NULL;
int key_size, iv_size;
string auth, recovered_auth;
// If an authentication mode is selected
if (mode == 7 || mode == 8)
{
auth = GraspAuthenticatedData();
}
// DES
if (scheme == 1)
{
// Generate key by random, from screen, or from file
key_size = DES::DEFAULT_KEYLENGTH;
GenerateSecByteBlock(key, key_size, L"key", scheme);
// Generate IV by random, from screen, or from file
if (mode > 1)
{
iv_size = DES::BLOCKSIZE;
GenerateSecByteBlock(iv, iv_size, L"IV", scheme);
// Write IV to file
#ifdef _WIN32
StringSource ss_iv(iv, iv.size(), true, new FileSink("des_iv_test.key"));
#elif __linux__
StringSource ss_iv(iv, iv.size(), true, new FileSink("des_iv_test.key"));
#endif
}
// Write key to file
#ifdef _WIN32
StringSource ss_key(key, key.size(), true, new FileSink("des_key_test.key"));
#elif __linux__
StringSource ss_key(key, key.size(), true, new FileSink("des_key_test.key"));
#endif
// Decide on the mode
switch (mode)
{
case 1:
etime = Looping_nonIV<ECB_Mode<DES>::Encryption, ECB_Mode<DES>::Decryption>(key, plaintext, ciphertext, recoveredtext);
break;
case 2:
etime = Looping_IV<CBC_Mode<DES>::Encryption, CBC_Mode<DES>::Decryption>(key, iv, plaintext, ciphertext, recoveredtext);
break;
case 3:
etime = Looping_IV<CFB_Mode<DES>::Encryption, CFB_Mode<DES>::Decryption>(key, iv, plaintext, ciphertext, recoveredtext);
break;
case 4:
etime = Looping_IV<OFB_Mode<DES>::Encryption, OFB_Mode<DES>::Decryption>(key, iv, plaintext, ciphertext, recoveredtext);
break;
case 5:
etime = Looping_IV<CTR_Mode<DES>::Encryption, CTR_Mode<DES>::Decryption>(key, iv, plaintext, ciphertext, recoveredtext);
break;
}
}
// AES
else if (scheme == 2)
{
// Select key size from screen
key_size = SelectKeySize(mode);
// Validate key's size
if (key_size == 64 && mode != 6)
{
wcout << L"Invalid Key size !" << endl;
exit(1);
}
// Generate key by random, from screen, or from file
GenerateSecByteBlock(key, key_size, L"key", scheme);
// Generate IV
if (mode > 1)
{
// Select IV's size
if (mode == 7 || mode == 8)
{
iv_size = SelectIVSize(mode);
}
else
{
iv_size = AES::BLOCKSIZE;
}
// Generate IV by random, from screen, or from file
GenerateSecByteBlock(iv, iv_size, L"IV", scheme);
// Write IV to file
#ifdef _WIN32
StringSource ss_iv(iv, iv.size(), true, new FileSink("aes_iv_test.key"));
#elif __linux__
StringSource ss_iv(iv, iv.size(), true, new FileSink("aes_iv_test.key"));
#endif
}
// Write key to file
#ifdef _WIN32
StringSource ss_key(key, key.size(), true, new FileSink("aes_key_test.key"));
#elif __linux__
StringSource ss_key(key, key.size(), true, new FileSink("aes_key_test.key"));
#endif
// Decide on the mode
switch (mode)