-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
13114 lines (10736 loc) · 587 KB
/
Program.cs
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
/*
MIT License
Copyright (c) 2018 José A. Rojo L.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* REFS:
* http://blogs.msdn.com/b/shawnfa/archive/2006/10/09/the-differences-between-rijndael-and-aes.aspx
* http://stackoverflow.com/questions/17171893/algorithm-is-the-rijndaelmanaged-class-in-c-sharp-equivalent-to-aes-encryption
* https://cketkar.wordpress.com/2013/05/13/fips-compliance-aes-and-net-crypto/
* http://blogs.msdn.com/b/shawnfa/archive/2004/04/14/113514.aspx
* http://security.stackexchange.com/questions/52665/which-is-the-best-cipher-mode-and-padding-mode-for-aes-encryption
* http://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode.aspx
* http://en.wikipedia.org/wiki/Padding_(cryptography)
* https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.legalkeysizes.aspx
* https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.iv(v=vs.110).aspx
* https://msdn.microsoft.com/es-es/library/windows/desktop/aa386986(v=vs.85).aspx
* http://stackoverflow.com/questions/7444586/how-can-i-sign-a-file-using-rsa-and-sha256-with-net
* http://blog.aggregatedintelligence.com/2010/02/encryptingdecrypting-using.html
* https://support.microsoft.com/es-es/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard-.net-application
* http://stackoverflow.com/questions/13231858/private-key-of-certificate-in-certificate-store-not-readable
* https://msdn.microsoft.com/es-es/library/system.security.cryptography.x509certificates.x509certificate2(v=vs.110).aspx
* http://www.bouncycastle.org/csharp/
* https://code.msdn.microsoft.com/Pretty-Good-Privacy-using-4f473c67
* http://bouncy-castle.1462172.n4.nabble.com/attachment/1466960/0/EncryptDecryptLargeFiles.java
* https://crypto.stackexchange.com/questions/15449/rsa-key-generation-parameters-public-exponent-certainty-string-to-key-count/15450#15450
* http://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test
* https://sites.google.com/site/lcastelli/cryptolib
* https://hashlib.codeplex.com/
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Anssi;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.EC;
using Org.BouncyCastle.Crypto.Agreement;
using Org.BouncyCastle.Crypto.Agreement.Kdf;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Math.EC.Custom.Sec;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Utilities;
using CryptoLib.SymmetricAlgorithms; // MARS
using HashLib; // HASHES
using HashLib.Crypto;
using HashLib.Crypto.SHA3;
using HashLib.Hash32;
using HashLib.Hash64;
using HashLib.Hash128;
using HashLib.Checksum;
using Jarol.Console;
using Jarol.IO;
namespace crypto
{
public class Program
{
private enum CryptoJob
{
OTHER
, ENCRYPT
, DECRYPT
};
private enum CryptoFormat
{
BASE64
, XML
, ARMORED
, RAW
};
private enum CryptoPadding
{
PKCS7 = 2
, Zeros = 3
, X923 = 4
, ISO10126 = 5
, ISO7816D4 = 6
, TBC = 7
, OAEP = 8
, PKCS1 = 9
, ISO9796D1 = 10
};
//----------------------------------------------------------------------------------
private struct SymmetricKey
{
public byte[] key;
public byte[] iv;
}
//----------------------------------------------------------------------------------
public struct AbstractCertificate
{
public string target;
public bool store;
public byte type;
public static AbstractCertificate Create (string t, bool b, byte k)
{
return new AbstractCertificate() { target = t, store = b, type = k };
}
}
//----------------------------------------------------------------------------------
private const string MSG_PROCESSING = "Processing. Please wait...";
private const string MSG_INVALID_OUTPUT = "Invalid output file or directory!";
private const string MSG_INVALID_KEY_SIZE = "Invalid key size!";
private const string MSG_INVALID_BLOCK_SIZE = "Invalid block size!";
private const string MSG_INVALID_FEEDBACK_SIZE = "Invalid feedback size!";
private const string MSG_INVALID_PUBLIC_KEY = "Invalid public key!";
private const string MSG_INVALID_PRIVATE_KEY = "Invalid private key!";
private const string MSG_INVALID_KEY_PAIR = "Invalid key pair!";
private const string MSG_INVALID_CIPHER_MODE = "Invalid cipher mode!";
private const string MSG_INVALID_BUFFER_SIZE = "Invalid buffer size!";
private const string MSG_INVALID_PADDING_MODE = "Invalid padding mode!";
private const string MSG_INVALID_HASH = "Invalid hash!";
private const string MSG_INVALID_IV = "The initial vector needs to be {0} bytes length!";
private const string MSG_INVALID_KEY = "The key needs to be {0} bytes length!";
private const string MSG_INVALID_RADIX = "Radix must be between 2 and 64!";
private const string MSG_INVALID_CODE = "Invalid length or repeated characters in the base code string!";
private const string MSG_INVALID_BASE_SEQ = "Invalid Base{0} sequence";
private const string MSG_INVALID_IES_CIPHER = "Invalid Ies cipher!";
private const string MSG_INVALID_PGP_ALGORITHM = "Invalid Pgp algorithm!";
private const string MSG_INVALID_CURVE_STORE = "Invalid curve store!";
private const string MSG_INVALID_FORMAT = "Invalid format!";
private const string MSG_INVALID_HASH_KEY_SIZE = "Can't use the {0} hash, The key has a size less than {1}!";
private const string MSG_INVALID_RSA_KEY = "The key type is not Rsa!";
private const string MSG_INVALID_ELGAMAL_KEY = "The key type is not ElGamal!";
private const string MSG_INVALID_ECDH_KEY = "The key type is not ECDH!";
private const string MSG_INVALID_EXPORT_PARAMS = "Invalid export parameters!";
private const string MSG_NON_RECIPROCAL_KEYS = "Non-reciprocal Pgp keys!";
private const string MSG_IV_DOES_NOT_ALLOW = "The current mode or operation does not allow initial vector and will not be processed!";
private const string MSG_CER_ALG_INCOMPATIBLE = "The use of certificates is only compatible with the RSA algorithm, other cases are ignored!";
private const string MSG_GEN_WITH_CER_DECRYPT = "The key pair generation can not be used simultaneously with certificates or decryption processes!";
private const string MSG_GEN_WITH_ENCRYPT = "The key pair generation can not be used simultaneously with the encryption indicator for this mode!";
private const string MSG_CROSS_INCOMPATIBLE = "The crossbreeding modifier can not be used simultaneously with key pair generation!";
private const string MSG_CROSS_RSA_PUB_KEY = "The public key file is not necessary, it will only be processed from the private key!";
private const string MSG_FILE_WAS_NOT_FOUND = "The file(s) was not found: \"{0}\"";
private const string MSG_PROCESS_CANCELLED = "Process cancelled by the user!\n";
private const string MSG_DONE = "Done!\n";
private const string MSG_PASSWORD = "You must enter at least 1 character.\n\n> Password: ";
private const string MSG_CONFIRM_PWD = "Confirm the password. Enter the password again.\n\n> Password: ";
private const string MSG_SALT = "You must enter at least 8 characters or nothing at all.\n\n> Salt: ";
private const string MSG_PRIVATE_KEY_PWD = "Private key password: ";
private const string MSG_NO_PGP_KEY_FOUND = "No Pgp {0} key found!";
private const string MSG_WRONG_LINE_WRAP = "Wrong line wrap for Base{0} encode!";
private const string MSG_CONTINUE_QUESTION = "\n\n> Do you want to continue?";
private const string MSG_EXPORT_PWD_QUESTION = "Do you want to use the certificate password for Pgp private key?";
private const string MSG_EXCEPTION_LOOPING = "The same exception has produced more than {0} consecutive times!";
private const string MSG_WRONG_PASSWORD = "Wrong password! ";
private const string MSG_WRONG_PWD_SALT = "Wrong password or salt";
private const string MSG_PLEASE_TRY_AGAIN = "Please try again.";
private const string MSG_KEYPAIR_INSECURE = "The resulting key pair might not be secure!";
private const string MSG_LARGE_KEYSIZE = "The key size is considerably large, It will take a long time!";
private const string MSG_MALFORMED_CMD_LINE = "Malformed command line!";
private const string MSG_PUBLIC_KEY_ONLY = "There is only one public key!";
private const string MSG_EXPORT_USE = "The export modifier can not be used simultaneously with encryption, decryption, or key pair generation!";
private const string MSG_PGP_SIGN_USE = "The signature parameter is not necessary for decryption, exportation, or key pair generation and will not be processed!";
private const string MSG_GENERIC_USE = "The {0} parameter is not necessary or supported for current mode or operation and will not be processed!";
private const string MSG_UNICODE_QUESTION = "The string contains Unicode characters!\n\n> Do you want to keep them?";
private const string MSG_UNICODE_CHANGE = "The character encoding was changed to Unicode!";
private const string MSG_INVALID_ALGO_SIGN = "The specified algorithm key cannot be used to sign with Pgp. An DSA, RSA, or ECDSA master key is needed! You can try export keys to Pgp.";
private const string MSG_INNER_EXCEPTION_CTRL = "Inner exception!";
private const string MSG_UNSUPPORTED_KEY_PROVIDER = "Key exchange provider not supported!";
private const string FILES = " files.";
private const string HASH_BLAKE224 = "BLAKE224";
private const string HASH_BLAKE256 = "BLAKE256";
private const string HASH_BLAKE384 = "BLAKE384";
private const string HASH_BLAKE512 = "BLAKE512";
private const string HASH_BMW224 = "BMW224";
private const string HASH_BMW256 = "BMW256";
private const string HASH_BMW384 = "BMW384";
private const string HASH_BMW512 = "BMW512";
private const string HASH_CUBE224 = "CUBE224";
private const string HASH_CUBE256 = "CUBE256";
private const string HASH_CUBE384 = "CUBE384";
private const string HASH_CUBE512 = "CUBE512";
private const string HASH_ECHO224 = "ECHO224";
private const string HASH_ECHO256 = "ECHO256";
private const string HASH_ECHO384 = "ECHO384";
private const string HASH_ECHO512 = "ECHO512";
private const string HASH_FUGUE224 = "FUGUE224";
private const string HASH_FUGUE256 = "FUGUE256";
private const string HASH_FUGUE384 = "FUGUE384";
private const string HASH_FUGUE512 = "FUGUE512";
private const string HASH_GROESTL224 = "GROESTL224";
private const string HASH_GROESTL256 = "GROESTL256";
private const string HASH_GROESTL384 = "GROESTL384";
private const string HASH_GROESTL512 = "GROESTL512";
private const string HASH_HAMSI224 = "HAMSI224";
private const string HASH_HAMSI256 = "HAMSI256";
private const string HASH_HAMSI384 = "HAMSI384";
private const string HASH_HAMSI512 = "HAMSI512";
private const string HASH_JH224 = "JH224";
private const string HASH_JH256 = "JH256";
private const string HASH_JH384 = "JH384";
private const string HASH_JH512 = "JH512";
private const string HASH_KECCAK224 = "KECCAK224";
private const string HASH_KECCAK256 = "KECCAK256";
private const string HASH_KECCAK384 = "KECCAK384";
private const string HASH_KECCAK512 = "KECCAK512";
private const string HASH_LUFFA224 = "LUFFA224";
private const string HASH_LUFFA256 = "LUFFA256";
private const string HASH_LUFFA384 = "LUFFA384";
private const string HASH_LUFFA512 = "LUFFA512";
private const string HASH_SHABAL224 = "SHABAL224";
private const string HASH_SHABAL256 = "SHABAL256";
private const string HASH_SHABAL384 = "SHABAL384";
private const string HASH_SHABAL512 = "SHABAL512";
private const string HASH_SHAVITE_224 = "SHAVITE224";
private const string HASH_SHAVITE_256 = "SHAVITE256";
private const string HASH_SHAVITE_384 = "SHAVITE384";
private const string HASH_SHAVITE_512 = "SHAVITE512";
private const string HASH_SIMD224 = "SIMD224";
private const string HASH_SIMD256 = "SIMD256";
private const string HASH_SIMD384 = "SIMD384";
private const string HASH_SIMD512 = "SIMD512";
private const string HASH_SKEIN224 = "SKEIN224";
private const string HASH_SKEIN256 = "SKEIN256";
private const string HASH_SKEIN384 = "SKEIN384";
private const string HASH_SKEIN512 = "SKEIN512";
private const string HASH_RIPEMD = "RIPEMD";
private const string HASH_RIPEMD128 = "RIPEMD128";
private const string HASH_RIPEMD160 = "RIPEMD160";
private const string HASH_RIPEMD256 = "RIPEMD256";
private const string HASH_RIPEMD320 = "RIPEMD320";
private const string HASH_SHA224 = "SHA224";
private const string HASH_SHA256 = "SHA256";
private const string HASH_SHA384 = "SHA384";
private const string HASH_SHA512 = "SHA512";
private const string HASH_SHA1 = "SHA1";
private const string HASH_SHA0 = "SHA0";
private const string HASH_MD4 = "MD4";
private const string HASH_MD2 = "MD2";
private const string HASH_MD5 = "MD5";
private const string HASH_GRINDAHL256 = "GRINDAHL256";
private const string HASH_GRINDAHL512 = "GRINDAHL512";
private const string HASH_HAS160 = "HAS160";
private const string HASH_HAVAL3_128 = "HAVAL3-128";
private const string HASH_HAVAL3_160 = "HAVAL3-160";
private const string HASH_HAVAL3_192 = "HAVAL3-192";
private const string HASH_HAVAL3_224 = "HAVAL3-224";
private const string HASH_HAVAL3_256 = "HAVAL3-256";
private const string HASH_HAVAL4_128 = "HAVAL4-128";
private const string HASH_HAVAL4_160 = "HAVAL4-160";
private const string HASH_HAVAL4_192 = "HAVAL4-192";
private const string HASH_HAVAL4_224 = "HAVAL4-224";
private const string HASH_HAVAL4_256 = "HAVAL4-256";
private const string HASH_HAVAL5_128 = "HAVAL5-128";
private const string HASH_HAVAL5_160 = "HAVAL5-160";
private const string HASH_HAVAL5_192 = "HAVAL5-192";
private const string HASH_HAVAL5_224 = "HAVAL5-224";
private const string HASH_HAVAL5_256 = "HAVAL5-256";
private const string HASH_PANAMA = "PANAMA";
private const string HASH_RG32 = "RG32";
private const string HASH_RG64 = "RG64";
private const string HASH_SNEFRU4_128 = "SNEFRU4-128";
private const string HASH_SNEFRU4_256 = "SNEFRU4-256";
private const string HASH_SNEFRU8_128 = "SNEFRU8-128";
private const string HASH_SNEFRU8_256 = "SNEFRU8-256";
private const string HASH_TIGER2 = "TIGER2";
private const string HASH_TIGER3_192 = "TIGER3-192";
private const string HASH_TIGER4_192 = "TIGER4-192";
private const string HASH_WHIRLPOOL = "WHIRLPOOL";
private const string HASH_AP = "AP";
private const string HASH_BERNSTEIN = "BERNSTEIN";
private const string HASH_BERNSTEIN1 = "BERNSTEIN1";
private const string HASH_BKDR = "BKDR";
private const string HASH_DEK = "DEK";
private const string HASH_DJB = "DJB";
private const string HASH_DOTNET = "DOTNET";
private const string HASH_ELF = "ELF";
private const string HASH_FNV = "FNV";
private const string HASH_FNV1A = "FNV1A";
private const string HASH_FNV64 = "FNV64";
private const string HASH_FNV1A64 = "FNV1A64";
private const string HASH_JENKINS3 = "JENKINS3";
private const string HASH_JS = "JS";
private const string HASH_MURMUR2 = "MURMUR2";
private const string HASH_MURMUR2_64 = "MURMUR2-64";
private const string HASH_MURMUR3 = "MURMUR3";
private const string HASH_MURMUR3_128 = "MURMUR3-128";
private const string HASH_ONEATTIME = "ONEATTIME";
private const string HASH_PJW = "PJW";
private const string HASH_ROTATING = "ROTATING";
private const string HASH_RS = "RS";
private const string HASH_SDBM = "SDBM";
private const string HASH_SHIFTANDXOR = "SNX";
private const string HASH_SUPERFAST = "SUPERFAST";
private const string HASH_SIPHASH = "SIPHASH";
private const string HASH_ADLER32 = "ADLER32";
private const string HASH_CRC32_IEEE = "CRC32-IEEE";
private const string HASH_CRC32_CASTAGNOLI = "CRC32-CASTAGNOLI";
private const string HASH_CRC32_KOOPMAN = "CRC32-KOOPMAN";
private const string HASH_CRC32_Q = "CRC32-Q";
private const string HASH_CRC64_ISO = "CRC64-ISO";
private const string HASH_CRC64_ECMA = "CRC64-ECMA";
private const string MOD_SHORT_MODE = "-m";
private const string MOD_LONG_MODE = "--mode";
private const string MOD_SHORT_HASH = "-h";
private const string MOD_LONG_HASH = "--hash";
private const string MOD_SHORT_KEY_SIZE = "-y";
private const string MOD_LONG_KEY_SIZE = "--key-size";
private const string MOD_SHORT_IO_OPTIONS = "-7";
private const string MOD_LONG_IO_OPTIONS = "--io-options";
private const string MOD_LONG_EXPORT = "--export";
private const string MOD_LONG_HELP = "--help";
private const string DIGEST = "DIGEST";
private const string CHECKSUM = "CHECKSUM";
private const string B2 = "B2";
private const string B3 = "B3";
private const string B4 = "B4";
private const string B5 = "B5";
private const string B6 = "B6";
private const string B7 = "B7";
private const string B8 = "B8";
private const string B9 = "B9";
private const string B10 = "B10";
private const string B11 = "B11";
private const string B12 = "B12";
private const string B13 = "B13";
private const string B14 = "B14";
private const string B15 = "B15";
private const string B16 = "B16";
private const string B17 = "B17";
private const string B18 = "B18";
private const string B19 = "B19";
private const string B20 = "B20";
private const string B21 = "B21";
private const string B22 = "B22";
private const string B23 = "B23";
private const string B24 = "B24";
private const string B25 = "B25";
private const string B26 = "B26";
private const string B27 = "B27";
private const string B28 = "B28";
private const string B29 = "B29";
private const string B30 = "B30";
private const string B31 = "B31";
private const string B32 = "B32";
private const string B33 = "B33";
private const string B34 = "B34";
private const string B35 = "B35";
private const string B36 = "B36";
private const string B37 = "B37";
private const string B38 = "B38";
private const string B39 = "B39";
private const string B40 = "B40";
private const string B41 = "B41";
private const string B42 = "B42";
private const string B43 = "B43";
private const string B44 = "B44";
private const string B45 = "B45";
private const string B46 = "B46";
private const string B47 = "B47";
private const string B48 = "B48";
private const string B49 = "B49";
private const string B50 = "B50";
private const string B51 = "B51";
private const string B52 = "B52";
private const string B53 = "B53";
private const string B54 = "B54";
private const string B55 = "B55";
private const string B56 = "B56";
private const string B57 = "B57";
private const string B58 = "B58";
private const string B59 = "B59";
private const string B60 = "B60";
private const string B61 = "B61";
private const string B62 = "B62";
private const string B63 = "B63";
private const string B64 = "B64";
private const string BIN = "BIN";
private const string HEX = "HEX";
private const string OCTAL = "OCTAL";
private const string DECIMAL = "DECIMAL";
private const string AES = "AES";
private const string RIJNDAEL = "RIJNDAEL";
private const string TDES = "3DES";
private const string DES = "DES";
private const string RC2 = "RC2";
private const string MARS = "MARS";
private const string SALSA20 = "SALSA20";
private const string XSALSA20 = "XSALSA20";
private const string CHACHA = "CHACHA";
private const string VMPC = "VMPC";
private const string CAMELLIA = "CAMELLIA";
private const string BLOWFISH = "BLOWFISH";
private const string TWOFISH = "2FISH";
private const string THREEFISH = "3FISH";
private const string SERPENT = "SERPENT";
private const string TNEPRES = "TNEPRES";
private const string CAST5 = "CAST5";
private const string CAST6 = "CAST6";
private const string IDEA = "IDEA";
private const string NOEKEON = "NOEKEON";
private const string TEA = "TEA";
private const string XTEA = "XTEA";
private const string GOST = "GOST";
private const string SEED = "SEED";
private const string SKIPJACK = "SKIPJACK";
private const string RC6 = "RC6";
private const string RC5 = "RC5";
private const string RC4 = "RC4";
private const string HC = "HC";
private const string ISAAC = "ISAAC";
private const string RSA = "RSA";
private const string PGP = "PGP";
private const string ELGAMAL = "ELGAMAL";
private const string NACCACHE = "NACCACHE";
private const string ECIES = "ECIES";
private const string DLIES = "DLIES";
private const string CUSTOM = "CUSTOM";
private const string ANSSI = "ANSSI";
private const string TELETRUST = "TELETRUST";
private const string NIST = "NIST";
private const string X962 = "X962";
private const string SEC = "SEC";
private const string DSA = "DSA";
private const string ECDSA = "ECDSA";
private const string ECDH = "ECDH";
private const string IV = "IV";
private const string ISO9796D2 = "ISO9796D2";
private const string PSS = "PSS";
private const string CODE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
private const string BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
private const string BASE32HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
private const string OUT = "output";
private const string RNDGEN = "random-gen";
private const string RSA_BC = "rsa-bouncy-castle";
private const string CURVE_STORE = "--curve-store";
private const string BATCH = "batch:";
//----------------------------------------------------------------------------------
private static readonly char[] _io_options_separator = new char[] { ',' };
private static readonly char[] _path_delimiter = new char[] { ';' };
//----------------------------------------------------------------------------------
private static bool _banner = true;
private static byte _percent = 100;
private static int _buffersize = 1024;
private static string _password = string.Empty;
private static bool _haspwd = false;
private static string _salt = string.Empty;
private static bool _saltleaking = false;
private static string _hash = HASH_SHA512;
private static string _key = string.Empty;
private static string _iv = string.Empty;
private static bool _without_iv = false;
private static bool _rsa_bc = false;
private static string _sign = string.Empty;
private static string _rsa_sign = RSA;
private static short _keysize = -1;
private static short _blocksize = -1;
private static short _feedbacksize = -1;
private static int _iterations = 1000;
private static int _rounds = 20;
private static CipherMode _ciphermode = CipherMode.CBC;
private static CryptoPadding _padding = CryptoPadding.PKCS7;
private static CryptoFormat _format = CryptoFormat.RAW;
private static CryptoJob _job = CryptoJob.OTHER;
private static Finder _finder = null;
private static Finder.Mode _findermode = Finder.Mode.Basic;
private static bool _ignorecase = true;
private static bool _recursively = false;
private static bool _reverse = false;
private static bool _raise = true;
private static bool _raisepwd = false;
private static bool _b32hex = false;
private static bool _rfc4648 = true;
private static bool _ksa3 = false;
private static bool _rc5b64 = false;
private static bool _unesc = false;
private static bool _generator = false;
private static bool _overwrite = false;
private static bool _crossbreeding = false;
private static bool _random = false;
private static bool _sha1 = false;
private static string _public_key = string.Empty;
private static string _private_key = string.Empty;
private static bool _export = false;
private static string _export_pbk = string.Empty;
private static string _export_pvk = string.Empty;
private static string _export_pwd = string.Empty;
private static string _code = CODE;
private static short _charsperline = 0;
private static string _pgp_algorithm = RSA;
private static string _pgp_id = string.Empty;
private static string _pgp_master = RSA;
private static bool _pgp_sign = false;
private static PgpPrivateKey _pgp_pvk = null;
private static PgpPublicKey _pgp_pbk = null;
private static string _ies_cipher = AES;
private static SymmetricKey _sk = new SymmetricKey();
private static bool _tellapart = false;
private static byte _certainty = 0;
private static long _public_exponent = 0;
private static byte _e_cnt = 0;
private static int _e_num = 0;
private static byte _e_max = 2;
private static bool _pathdelimiter = true;
private static bool _keyexchange = false;
private static string _sbox = string.Empty;
private static Encoding _encoding = Encoding.ASCII;
private static bool _codechanged = false;
private static string _mode = string.Empty;
private static string _curvestore = string.Empty;
private static string _curve = string.Empty;
private static int _small_primes = 30;
private static List<AbstractCertificate> _cer = new List<AbstractCertificate>();
private static SymmetricKeyAlgorithmTag _ska = SymmetricKeyAlgorithmTag.Aes256;
private static CompressionAlgorithmTag _cat = CompressionAlgorithmTag.Zip;
//----------------------------------------------------------------------------------
private static string Prompt
(
string msg
, bool hidden
, short minlen = 1
, bool empty = false
){
StringBuilder sb = new StringBuilder();
Messenger.Print(Messenger.Icon.WARNING, msg);
do
{
ConsoleKeyInfo ki = Console.ReadKey(true);
byte bk = (byte)ki.KeyChar;
if (ki.Key == ConsoleKey.Enter && ((empty && sb.Length == 0) || sb.Length >= minlen))
break;
else if (ki.Key == ConsoleKey.Backspace)
{
if (sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(' ');
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
}
else if ((bk > 31 && bk < 127) || bk > 127)
{
sb.Append(ki.KeyChar);
Console.Write(hidden ? '*' : ki.KeyChar);
}
}
while (true);
if (msg.IndexOf('\n') != -1)
Console.WriteLine();
return sb.ToString();
}
//--------------------------------------------------------------------------------
private static void Delay (long milliseconds)
{
Stopwatch w = Stopwatch.StartNew();
while (w.ElapsedMilliseconds < milliseconds);
w.Stop();
}
//--------------------------------------------------------------------------------
private static void Progress (double current, double total, int block)
{
if (current == block)
Console.Write('\n');
_percent = (byte)Math.Floor(current / total * 100);
block = _percent / 10;
StringBuilder s = new StringBuilder();
s.Append("[");
for (short i = 0; i < 11; ++i)
s.Append(i <= block ? '#' : ' ');
s.Append("]: ");
s.Append(_percent);
s.Append('%');
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(s);
if (_percent == 100)
{
Program.Delay(500);
Console.SetCursorPosition(0, Console.CursorTop);
for (byte i = 0; i < 20; ++i)
Console.Write(' ');
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
}
//--------------------------------------------------------------------------------
private static void LineWrapper (Stream dest, long current, ref long total)
{
if (_charsperline > 0 && total > 0 && current > total)
{
dest.WriteByte(10);
total += _charsperline;
}
}
//--------------------------------------------------------------------------------
private static void Write
(
ICryptoTransform ct
, Stream src
, Stream dest
, bool progressbar
){
if (_charsperline > 0 && (_charsperline < 4 || _charsperline % 4 != 0))
throw new Exception(string.Format(MSG_WRONG_LINE_WRAP, 64));
byte[] o = new byte[ct.OutputBlockSize];
byte[] i = new byte[ct.InputBlockSize];
long w = _charsperline - 1;
long l = src.Length;
long p = l - ct.InputBlockSize;
long c = 0;
long t = 0;
int n = 0;
int k = 0;
while (c < p)
{
if ((n = src.Read(i, 0, i.Length)) > 0)
{
Program.LineWrapper(dest, t, ref w);
dest.Write(o, 0, k = ct.TransformBlock(i, 0, n, o, 0));
t += k;
c += n;
if (progressbar)
Program.Progress(c, l, n);
}
}
if (c > 0 || (l > 0 && p < 1))
{
Program.LineWrapper(dest, t, ref w);
n = src.Read(i, 0, i.Length);
Array.Clear(o, 0, o.Length);
o = ct.TransformFinalBlock(i, 0, n);
dest.Write(o, 0, o.Length);
Array.Clear(o, 0, o.Length);
Array.Clear(i, 0, i.Length);
if (progressbar)
Program.Progress(c + n, l, n);
}
}
//--------------------------------------------------------------------------------
private static void Base64Encode (Stream src, Stream dest, bool progressbar = true)
{
using (ToBase64Transform t = new ToBase64Transform())
Program.Write(t, src, dest, progressbar);
}
//--------------------------------------------------------------------------------
private static byte[] Base64Encode (byte[] data, bool progressbar = false)
{
using (MemoryStream dt = new MemoryStream(data))
{
using (MemoryStream bf = new MemoryStream())
{
Program.Base64Encode(dt, bf, progressbar);
return bf.ToArray();
}
}
}
//--------------------------------------------------------------------------------
private static byte[] Base64Encode (string data, bool progressbar = false)
{
byte[] d = _encoding.GetBytes(data);
byte[] b = Program.Base64Encode(d, progressbar);
Array.Clear(d, 0, d.Length);
return b;
}
//--------------------------------------------------------------------------------
private static void Base64Decode (Stream src, Stream dest, bool progressbar = true)
{
using (FromBase64Transform t = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces))
Program.Write(t, src, dest, progressbar);
}
//--------------------------------------------------------------------------------
private static byte[] Base64Decode (byte[] data, bool progressbar = false)
{
using (MemoryStream dt = new MemoryStream(data))
{
using (MemoryStream bf = new MemoryStream())
{
Program.Base64Decode(dt, bf, progressbar);
return bf.ToArray();
}
}
}
//--------------------------------------------------------------------------------
private static byte[] Base64Decode (string data, bool progressbar = false)
{
byte[] d = Encoding.UTF8.GetBytes(data);
byte[] b = Program.Base64Decode(d, progressbar);
Array.Clear(d, 0, d.Length);
return b;
}
//--------------------------------------------------------------------------------
private static bool TryBase64Decode (ref byte[] data, bool clean = true)
{
if (data == null || data.Length < 4)
return false;
try
{
byte[] b = Program.Base64Decode(data);
if (clean)
Array.Clear(data, 0, data.Length);
data = b;
}
catch (FormatException)
{
return false;
}
return true;
}
//--------------------------------------------------------------------------------
private static bool IsBase64 (byte[] data)
{
return Program.TryBase64Decode(ref data, false);
}
//--------------------------------------------------------------------------------
private static bool IsBase64 (string data)
{
Regex r = new Regex
(
"([a-z0-9+/]{4})*([a-z0-9+/]{4}|[a-z0-9+/]{3}=|[a-z0-9+/]{2}==)$"
, RegexOptions.IgnoreCase | RegexOptions.Multiline
);
return r.Matches(data.Trim()).Count > 0;
}
//--------------------------------------------------------------------------------
public static void Base32Encode (Stream src, Stream dest)
{
if (_charsperline > 0 && _charsperline < 2)
throw new Exception(string.Format(MSG_WRONG_LINE_WRAP, 32));
string s = _b32hex ? BASE32HEX : BASE32;
long l = src.Length;
long n = (long)Math.Ceiling(l / 5f) * 8;
long w = _charsperline - 1;
short r = 5;
long c = 0;
long p = 0;
byte t = 0;
int b;
while ((b = src.ReadByte()) > -1)
{
Program.LineWrapper(dest, c++, ref w);
dest.WriteByte((byte)s[t | (b >> 8 - r)]);
if (r <= 3)
{
Program.LineWrapper(dest, c++, ref w);
dest.WriteByte((byte)s[(b >> 3 - r) & 31]);
r += 5;
}
t = (byte)((b << (r -= 3)) & 31);
Program.Progress(++p, l, 1);
}
if (c != n)
{
Program.LineWrapper(dest, c++, ref w);
dest.WriteByte((byte)s[t]);
}
while (c < n)
{
Program.LineWrapper(dest, c++, ref w);
dest.WriteByte(61);
}
}
//--------------------------------------------------------------------------------
public static void Base32Decode (Stream src, Stream dest)
{
string e = string.Format(MSG_INVALID_BASE_SEQ + '!', 32);
string s = _b32hex ? BASE32HEX : BASE32;
long l = src.Length;
long c = 0;
int r = 8;
int t = 0;
byte p = 0;
int n, b;
while ((b = src.ReadByte()) > -1)
{
Program.Progress(++c, l, 1);
if ((b > 6 && b < 14) || b == 32)
continue;
else if (b == 61)
{
if (++p > 6)
throw new Exception(e);
}
else if (p > 0)
throw new Exception(e);
else
{
if ((n = s.IndexOf((char)b)) < 0)
throw new Exception(e);
else if (r > 5)
t = t | (n << (r -= 5));
else
{
dest.WriteByte((byte)(t | (n >> 5 - r)));
t = n << (r += 3);
}
}
}
}
//--------------------------------------------------------------------------------
private static bool HasRepeatedChars (string src)
{
for (int i = 0, l = src.Length; i < l; ++i)
for (int j = i + 1; j < l; ++j)
if (src[i] == src[j])
return true;
return false;
}
//--------------------------------------------------------------------------------
private static byte GetBlockSize (byte radix)
{
if (radix == 2) radix = 8;
else if (radix == 3) radix = 6;
else if (radix > 3 && radix < 7) radix = 4;
else if (radix > 6 && radix < 16) radix = 3;
else radix = 2;
return radix;
}
//--------------------------------------------------------------------------------
private static string ByteToString (byte b, byte radix)
{
string s = string.Empty;
do
{
s = _code[b % radix] + s;
}
while ((b = (byte)(b / radix)) > 0);
radix = Program.GetBlockSize(radix);
b = (byte)s.Length;
while (b++ < radix)
s = _code[0] + s;
return s;
}
//--------------------------------------------------------------------------------
private static byte ByteFromBlock (byte[] b, byte radix)
{
byte n = 0;
for (int c, i = 0, l = b.Length; i < l; ++i)
{
if ((c = _code.IndexOf((char)b[i])) < 0) throw new Exception
(
string.Format
(
MSG_INVALID_BASE_SEQ + " or Wrong base-code!"
, radix
)
);
n = (byte)(n * radix + c);