-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
1488 lines (1334 loc) · 73.2 KB
/
MainWindow.xaml.cs
File metadata and controls
1488 lines (1334 loc) · 73.2 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
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.Generic;
namespace Tab
{
abstract class Ciphers
{
private static readonly string errorString = "Проверьте правильность введенных данных";
private static readonly string symbolsErrorInputString = "Проверьте наличие внеалфавитных символов во входной строке";
private static readonly string parityErrorInputString = "Количество символов во входной строке должно быть четным";
private static readonly List<string> alphabetsList = new List<string>() { "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ",
"абвгдеёжзийклмнопрстуфхцчшщъыьэюя",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz" };
private static int getNumber(char letter, List<string> alphabets)
{
foreach (var alphabet in alphabets)
if (alphabet.Contains(letter))
return alphabet.IndexOf(letter);
return -1;
}
private static char getLetter(int offset, string alphabet)
{
int index = offset % alphabet.Length;
index = (index < 0) ? index + alphabet.Length : index;
return alphabet.ElementAt(index);
}
private static List<string> GenerateSloganAlphabet(string slogan)
{
List<string> sloganAlphabetsList = new List<string>() { "", "", "", "" };
string formalizedSlogan = String.Empty;
foreach (var letter in slogan)
{
if (alphabetsList.ElementAt(0).Contains(letter) || alphabetsList.ElementAt(1).Contains(letter))
{
sloganAlphabetsList[0] += (!sloganAlphabetsList[0].Contains(Char.ToUpper(letter))) ? Char.ToUpper(letter).ToString() : String.Empty;
sloganAlphabetsList[1] += (!sloganAlphabetsList[1].Contains(Char.ToLower(letter))) ? Char.ToLower(letter).ToString() : String.Empty;
} else if (alphabetsList.ElementAt(2).Contains(letter) || alphabetsList.ElementAt(3).Contains(letter)) {
sloganAlphabetsList[2] += (!sloganAlphabetsList[2].Contains(Char.ToUpper(letter))) ? Char.ToUpper(letter).ToString() : String.Empty;
sloganAlphabetsList[3] += (!sloganAlphabetsList[3].Contains(Char.ToLower(letter))) ? Char.ToLower(letter).ToString() : String.Empty;
}
}
for (int i = 0; i < alphabetsList.Count; i++)
foreach (var letter in alphabetsList.ElementAt(i)) sloganAlphabetsList[i] += (!sloganAlphabetsList[i].Contains(letter)) ? letter.ToString() : String.Empty;
return sloganAlphabetsList;
}
public abstract class Caesar
{
public static string About()
{
return "Каждая буква входного слова смещается на указанное в ключе число позиций" + Environment.NewLine +
"Неалфавитные символы в слове не преобразуются" + Environment.NewLine +
"Неалфавитные символы в ключе игнорируются";
}
public static string Encode(string inputWord, int key)
{
string outputWord = String.Empty;
foreach (var letter in inputWord)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(letter)))
outputWord += letter;
else foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(letter))
{
outputWord += getLetter(getNumber(letter, alphabetsList) + key, alphabet);
continue;
}
}
}
return outputWord;
}
public static string Decode(string inputWord, int key)
{
string outputWord = String.Empty;
foreach (var letter in inputWord)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(letter)))
outputWord += letter;
else foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(letter))
{
outputWord += getLetter(getNumber(letter, alphabetsList) - key, alphabet);
continue;
}
}
}
return outputWord;
}
}
public abstract class Atbash
{
public static string About()
{
return "Каждая буква входного слова заменяется на букву обратного алфавита в той же позиции" + Environment.NewLine +
"Неалфавитные символы в слове не преобразуются" + Environment.NewLine +
"Ключ не используется";
}
public static string Encode(string inputWord)
{
string outputWord = String.Empty;
foreach (var letter in inputWord)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(letter)))
outputWord += letter;
else foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(letter))
{
outputWord += getLetter(alphabet.Length - getNumber(letter, alphabetsList) - 1, alphabet);
continue;
}
}
}
return outputWord;
}
public static string Decode(string word)
{
return Encode(word);
}
}
public abstract class Motto
{
public static string About()
{
return "В соответствие каждой букве алфавита ставится буква измененного в соответствии с лозунгом нового алфавита" + Environment.NewLine +
"Например, для слова \"информатика\" алфавит будет \"информаткбвгдеёжзл...эюя\"" + Environment.NewLine +
"Неалфавитные символы выдают ошибку";
}
private static List<string> sloganAlphabets;
public static string Encode(string inputWord, string slogan)
{
string outputWord = String.Empty;
sloganAlphabets = GenerateSloganAlphabet(slogan);
foreach (var letter in inputWord)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(letter)))
outputWord += letter;
else
foreach (var alphabet in alphabetsList)
if (alphabet.Contains(letter))
outputWord += sloganAlphabets.ElementAt(alphabetsList.IndexOf(alphabet)).ElementAt(alphabet.IndexOf(letter));
}
return outputWord;
}
public static string Decode(string inputWord, string slogan)
{
string outputWord = String.Empty;
sloganAlphabets = GenerateSloganAlphabet(slogan);
foreach (var letter in inputWord)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(letter)))
outputWord += letter;
else
foreach (var sloganAlphabet in sloganAlphabets)
if (sloganAlphabet.Contains(letter))
outputWord += alphabetsList.ElementAt(sloganAlphabets.IndexOf(sloganAlphabet)).ElementAt(sloganAlphabet.IndexOf(letter));
}
return outputWord;
}
}
public abstract class TrisemusSystem
{
public static string About()
{
return "Алфавит записывается в квадрат и каждая буква входного слова заменяется на букву того же столбца, но на один ряд ниже" + Environment.NewLine +
"Если ниже буквы отсутствуют, берется буква из первого ряда" + Environment.NewLine +
"Неалфавитные символы выдают ошибку";
}
private static List<string> sloganAlphabets;
public static string Encode(string inputWord, string slogan)
{
const int N = 6;
string outputWord = String.Empty;
sloganAlphabets = GenerateSloganAlphabet(slogan);
foreach (var letter in inputWord)
{
if (!sloganAlphabets.Any(sloganAlphabet => sloganAlphabet.Contains(letter)))
outputWord += letter;
else {
foreach (var sloganAlphabet in sloganAlphabets)
{
if (sloganAlphabet.Contains(letter))
{
if (getNumber(letter, sloganAlphabets) < sloganAlphabet.Length - N)
outputWord += getLetter(getNumber(letter, sloganAlphabets) + N, sloganAlphabet);
else
outputWord += getLetter(getNumber(letter, sloganAlphabets) % N, sloganAlphabet);
}
}
}
}
return outputWord;
}
public static string Decode(string inputWord, string slogan)
{
const int N = 6;
string outputWord = String.Empty;
sloganAlphabets = GenerateSloganAlphabet(slogan);
foreach (var letter in inputWord)
{
if (!sloganAlphabets.Any(sloganAlphabet => sloganAlphabet.Contains(letter)))
outputWord += letter;
else
{
foreach (var sloganAlphabet in sloganAlphabets)
{
if (sloganAlphabet.Contains(letter))
{
if (getNumber(letter, sloganAlphabets) < N)
{
if (getNumber(letter, sloganAlphabets) < sloganAlphabet.Length % N)
outputWord += getLetter(sloganAlphabet.Length - sloganAlphabet.Length % N + getNumber(letter, sloganAlphabets), sloganAlphabet);
else
outputWord += getLetter(sloganAlphabet.Length - sloganAlphabet.Length % N - N + getNumber(letter, sloganAlphabets), sloganAlphabet);
}
else
outputWord += getLetter(getNumber(letter, sloganAlphabets) - N, sloganAlphabet);
}
}
}
}
return outputWord;
}
}
public abstract class TrisemusTable
{
public static string About()
{
return "Каждая буква алфавита смещается на количество позиций, равное ее индексу во входном слове" + Environment.NewLine +
"Неалфавитные символы в слове не преобразуются";
}
public static string Encode(string inputWord)
{
string outputWord = String.Empty;
for (int i = 0; i < inputWord.Length; i++)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(inputWord.ElementAt(i))))
outputWord += inputWord.ElementAt(i);
foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(inputWord.ElementAt(i)))
outputWord += getLetter(getNumber(inputWord.ElementAt(i), alphabetsList) + i, alphabet);
}
}
return outputWord;
}
public static string Decode(string inputWord)
{
string outputWord = String.Empty;
for (int i = 0; i < inputWord.Length; i++)
{
if (!alphabetsList.Any(alphabet => alphabet.Contains(inputWord.ElementAt(i))))
outputWord += inputWord.ElementAt(i);
foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(inputWord.ElementAt(i)))
outputWord += getLetter(getNumber(inputWord.ElementAt(i), alphabetsList) - i, alphabet);
}
}
return outputWord;
}
}
public abstract class PolybianSquare
{
public static string About()
{
return "Алфавит записывается в квадрат и каждая буква входного слова заменяется на два символа - координаты буквы в квадрате" + Environment.NewLine +
"Нецифровые символы выдают ошибку" + Environment.NewLine +
"Нечетное количество цифр выдает ошибку";
}
public static string Encode(string inputWord, bool language)
{
string outputWord = String.Empty;
foreach (var letter in inputWord)
{
if (language == false)
{
if (alphabetsList[0].Contains(letter) || alphabetsList[1].Contains(letter))
outputWord += (alphabetsList[1].IndexOf(Char.ToLower(letter)) / 6).ToString() + (alphabetsList[1].IndexOf(Char.ToLower(letter)) % 6).ToString();
else
outputWord += letter;
} else {
if (alphabetsList[2].Contains(letter) || alphabetsList[3].Contains(letter))
outputWord += (alphabetsList[3].IndexOf(Char.ToLower(letter)) / 6).ToString() + (alphabetsList[3].IndexOf(Char.ToLower(letter)) % 6).ToString();
else
outputWord += letter;
}
}
return outputWord;
}
public static string Decode(string inputWord, bool language)
{
string outputWord = String.Empty;
if (inputWord.Count(ch => Char.IsDigit(ch)) % 2 == 1)
return errorString;
for (int i = 0; i < inputWord.Length; i++)
{
if (Char.IsDigit(inputWord[i]))
{
if (Char.IsDigit(inputWord[i + 1]))
{
if (language == false)
{
if ((int)Char.GetNumericValue(inputWord[i]) * 6 + (int)Char.GetNumericValue(inputWord[i + 1]) >= alphabetsList[1].Length)
return errorString;
else
outputWord += alphabetsList[1].ElementAt((int)Char.GetNumericValue(inputWord[i]) * 6 + (int)Char.GetNumericValue(inputWord[i + 1]));
} else {
if ((int)Char.GetNumericValue(inputWord[i]) * 6 + (int)Char.GetNumericValue(inputWord[i + 1]) >= alphabetsList[3].Length)
return errorString;
else
outputWord += alphabetsList[3].ElementAt((int)Char.GetNumericValue(inputWord[i]) * 6 + (int)Char.GetNumericValue(inputWord[i + 1]));
}
i++;
}
else
return errorString;
} else {
outputWord += inputWord[i];
}
}
return outputWord;
}
}
public abstract class Vigenere
{
public static string About()
{
return "Каждая буква входного слова смещается на количество позиций, равное индексу ключа" + Environment.NewLine +
"Если ниже буквы отсутствуют, берется буква из первого ряда" + Environment.NewLine +
"Недостаточная длина ключа восполняется повтором оного" + Environment.NewLine +
"Излишняя длина ключа игнорируется" + Environment.NewLine +
"Неалфавитные символы выдают ошибку";
}
public static string Encode(string inputWord, string key)
{
string outputWord = String.Empty;
string newKey = String.Empty;
while (newKey.Length < inputWord.Length) newKey += key;
newKey = newKey.Substring(0, inputWord.Length);
for (int i = 0; i < inputWord.Length; i++)
{
if (!alphabetsList.Any(str => str.Contains(inputWord[i])))
outputWord += inputWord[i].ToString();
else
{
foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(inputWord[i]))
{
if (alphabet.Contains(Char.ToLower(newKey[i])))
{
outputWord += getLetter(alphabet.IndexOf(inputWord[i]) + 1 + alphabet.IndexOf((Char.ToLower(newKey[i]))), alphabet);
}
else if (alphabet.Contains(Char.ToUpper(newKey[i])))
{
outputWord += getLetter(alphabet.IndexOf(inputWord[i]) + 1 + alphabet.IndexOf((Char.ToUpper(newKey[i]))), alphabet);
}
else
return errorString;
}
}
}
}
return outputWord;
}
public static string Decode(string inputWord, string key)
{
string outputWord = String.Empty;
string newKey = String.Empty;
while (newKey.Length < inputWord.Length) newKey += key;
newKey = newKey.Substring(0, inputWord.Length);
for (int i = 0; i < inputWord.Length; i++)
{
if (!alphabetsList.Any(str => str.Contains(inputWord[i])))
outputWord += inputWord[i].ToString();
else
{
foreach (var alphabet in alphabetsList)
{
if (alphabet.Contains(inputWord[i]))
{
if (alphabet.Contains(Char.ToLower(newKey[i])))
{
outputWord += getLetter(alphabet.IndexOf(inputWord[i]) - alphabet.IndexOf((Char.ToLower(newKey[i]))) - 1, alphabet);
}
else if (alphabet.Contains(Char.ToUpper(newKey[i])))
{
outputWord += getLetter(alphabet.IndexOf(inputWord[i]) - alphabet.IndexOf((Char.ToUpper(newKey[i]))) - 1, alphabet);
}
else
return errorString;
}
}
}
}
return outputWord;
}
}
public abstract class Playfair
{
public static string About()
{
return "Входное слово разделяется на биграммы, которые шифруются в алфавитной матрице по правилам:" + Environment.NewLine +
"1. Буквы в одном ряду заменяются на буквы справа от них" + Environment.NewLine +
"2. Буквы в одном столбце заменяются на буквы снизу от них" + Environment.NewLine +
"3. Буквы в разных рядах и столбцах заменяются на буквы в своем ряду, но в столбце второй буквы" + Environment.NewLine +
"Неалфавитные символы и нечетность расшифровываемого сообщения выдают ошибку";
}
private static int rows;
private static int columns;
private static int getRow(char letter, string matrix) { return matrix.IndexOf(letter) / columns; }
private static int getCol(char letter, string matrix) { return matrix.IndexOf(letter) % columns; }
private static char getLetter(int row, int col, string matrix) { return matrix[row * columns + col]; }
public static string Encode(string inputWord, string key, bool language)
{
string outputWord = String.Empty;
string formattedWord = inputWord.ToUpper();
List<string> matrix = GenerateSloganAlphabet(key);
matrix.RemoveAt(3);
matrix.RemoveAt(1);
matrix[0] = matrix[0].Remove(matrix[0].IndexOf("Ё"), 1);
matrix[1] = matrix[1].Remove(matrix[1].IndexOf("I"), 1);
int lang = (language == false) ? 0 : 1;
rows = (lang == 0) ? 4 : 5;
columns = (lang == 0) ? 8 : 5;
char separator = (lang == 0) ? 'Я' : 'Z';
if (!inputWord.All(ch => matrix[lang].Contains(Char.ToUpper(ch)))) return symbolsErrorInputString;
for (int i = 0; i < formattedWord.Length; i += 2)
if (i == formattedWord.Length - 1)
formattedWord += separator;
else
if (formattedWord[i] == formattedWord[i + 1])
formattedWord = formattedWord.Insert(i + 1, separator.ToString());
for (int i = 0; i < formattedWord.Length; i += 2) {
if (getRow(formattedWord[i], matrix[lang]) == getRow(formattedWord[i + 1], matrix[lang])) {
outputWord += (getCol(formattedWord[i], matrix[lang]) + 1 == columns) ?
getLetter(getRow(formattedWord[i], matrix[lang]), 0, matrix[lang]) :
getLetter(getRow(formattedWord[i], matrix[lang]), getCol(formattedWord[i], matrix[lang]) + 1, matrix[lang]);
outputWord += (getCol(formattedWord[i + 1], matrix[lang]) + 1 == columns) ?
getLetter(getRow(formattedWord[i + 1], matrix[lang]), 0, matrix[lang]) :
getLetter(getRow(formattedWord[i + 1], matrix[lang]), getCol(formattedWord[i + 1], matrix[lang]) + 1, matrix[lang]);
} else if (getCol(formattedWord[i], matrix[lang]) == getCol(formattedWord[i + 1], matrix[lang])) {
outputWord += (getRow(formattedWord[i], matrix[lang]) + 1 == rows) ?
getLetter(0, getCol(formattedWord[i], matrix[lang]) + 1, matrix[lang]) :
getLetter(getRow(formattedWord[i], matrix[lang]) + 1, getCol(formattedWord[i], matrix[lang]), matrix[lang]);
outputWord += (getRow(formattedWord[i + 1], matrix[lang]) + 1 == rows) ?
getLetter(0, getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]) :
getLetter(getRow(formattedWord[i + 1], matrix[lang]) + 1, getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]);
} else {
outputWord += getLetter(getRow(formattedWord[i], matrix[lang]), getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]);
outputWord += getLetter(getRow(formattedWord[i + 1], matrix[lang]), getCol(formattedWord[i], matrix[lang]), matrix[lang]);
}
}
return outputWord;
}
public static string Decode(string inputWord, string key, bool language)
{
string outputWord = String.Empty;
string formattedWord = inputWord.ToUpper();
List<string> matrix = GenerateSloganAlphabet(key);
matrix.RemoveAt(3);
matrix.RemoveAt(1);
matrix[0] = matrix[0].Remove(matrix[0].IndexOf("Ё"), 1);
matrix[1] = matrix[1].Remove(matrix[1].IndexOf("I"), 1);
int lang = (language == false) ? 0 : 1;
rows = (lang == 0) ? 4 : 5;
columns = (lang == 0) ? 8 : 5;
char separator = (lang == 0) ? 'Я' : 'Z';
if (!inputWord.All(ch => matrix[lang].Contains(Char.ToUpper(ch)))) return symbolsErrorInputString;
if (inputWord.Length % 2 == 1) return parityErrorInputString;
for (int i = 0; i < formattedWord.Length; i += 2)
{
if (getRow(formattedWord[i], matrix[lang]) == getRow(formattedWord[i + 1], matrix[lang]))
{
outputWord += (getCol(formattedWord[i], matrix[lang]) - 1 < 0) ?
getLetter(getRow(formattedWord[i], matrix[lang]), columns - 1, matrix[lang]) :
getLetter(getRow(formattedWord[i], matrix[lang]), getCol(formattedWord[i], matrix[lang]) - 1, matrix[lang]);
outputWord += (getCol(formattedWord[i + 1], matrix[lang]) - 1 < 0) ?
getLetter(getRow(formattedWord[i + 1], matrix[lang]), columns - 1, matrix[lang]) :
getLetter(getRow(formattedWord[i + 1], matrix[lang]), getCol(formattedWord[i + 1], matrix[lang]) - 1, matrix[lang]);
}
else if (getCol(formattedWord[i], matrix[lang]) == getCol(formattedWord[i + 1], matrix[lang]))
{
outputWord += (getRow(formattedWord[i], matrix[lang]) - 1 < 0) ?
getLetter(rows - 1, getCol(formattedWord[i], matrix[lang]) + 1, matrix[lang]) :
getLetter(getRow(formattedWord[i], matrix[lang]) - 1, getCol(formattedWord[i], matrix[lang]), matrix[lang]);
outputWord += (getRow(formattedWord[i + 1], matrix[lang]) - 1 < 0) ?
getLetter(rows - 1, getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]) :
getLetter(getRow(formattedWord[i + 1], matrix[lang]) - 1, getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]);
}
else
{
outputWord += getLetter(getRow(formattedWord[i], matrix[lang]), getCol(formattedWord[i + 1], matrix[lang]), matrix[lang]);
outputWord += getLetter(getRow(formattedWord[i + 1], matrix[lang]), getCol(formattedWord[i], matrix[lang]), matrix[lang]);
}
}
return outputWord;
}
}
public abstract class Gamming
{
public static string About()
{
return "Каждая буква входного слова заменяется на букву с индексом, равным сумме по модулю N индексов буквы слова и ключа" + Environment.NewLine +
"Недостаточная длина ключа восполняется повтором оного" + Environment.NewLine +
"Излишняя длина ключа игнорируется" + Environment.NewLine +
"Неалфавитные символы выдают ошибку";
}
private static int ModularAddition(int a, int b, int module) { return (a + b) % module; }
public static string Encode(string inputWord, string key, bool language, List<string> newAlphabet)
{
string outputWord = String.Empty;
string newKey = String.Empty;
while (newKey.Length < inputWord.Length) newKey += key;
newKey = newKey.Substring(0, inputWord.Length);
int lang = (language == false) ? 0 : 2;
foreach (var letter in inputWord)
if (!newAlphabet[lang].Contains(Char.ToUpper(letter)))
return symbolsErrorInputString;
for (int i = 0; i < inputWord.Length; i++) {
if (newAlphabet[lang].Contains(inputWord[i])) {
outputWord += getLetter(ModularAddition(newAlphabet[lang].IndexOf(inputWord[i]) + 1,
newAlphabet[lang].IndexOf(Char.ToUpper(newKey[i])),
newAlphabet[lang].Length),
newAlphabet[lang]);
} else if (newAlphabet[lang + 1].Contains(inputWord[i])) {
outputWord += getLetter(ModularAddition(newAlphabet[lang + 1].IndexOf(inputWord[i]) + 1,
newAlphabet[lang].IndexOf(Char.ToUpper(newKey[i])),
newAlphabet[lang].Length),
newAlphabet[lang + 1]);
} else
return symbolsErrorInputString;
}
return outputWord;
}
public static string Decode(string inputWord, string key, bool language, List<string> newAlphabet)
{
string outputWord = String.Empty;
string newKey = String.Empty;
while (newKey.Length < inputWord.Length) newKey += key;
newKey = newKey.Substring(0, inputWord.Length);
int lang = (language == false) ? 0 : 2;
foreach (var letter in inputWord)
if (!newAlphabet[lang].Contains(Char.ToUpper(letter)))
return symbolsErrorInputString;
for (int i = 0; i < inputWord.Length; i++)
{
if (newAlphabet[lang + 1].Contains(inputWord[i]))
{
outputWord += getLetter(ModularAddition(newAlphabet[lang + 1].IndexOf(inputWord[i]) - newAlphabet[lang].IndexOf(Char.ToUpper(newKey[i])),
newAlphabet[lang].Length,
newAlphabet[lang].Length) - 1,
newAlphabet[lang + 1]);
} else if (newAlphabet[lang].Contains(inputWord[i])) {
outputWord += getLetter(ModularAddition(newAlphabet[lang].IndexOf(inputWord[i]) - newAlphabet[lang].IndexOf(Char.ToUpper(newKey[i])),
newAlphabet[lang].Length,
newAlphabet[lang].Length) - 1,
newAlphabet[lang]);
} else
return symbolsErrorInputString;
}
return outputWord;
}
}
public abstract class BlockTransposing
{
public static string About()
{
return "Каждый блок символов, равный длине ключа, меняет порядок в соответствии с ним" + Environment.NewLine +
"Шифруются любые символы" + Environment.NewLine +
"Недостаточная длина ключа восполняется повтором оного" + Environment.NewLine +
"Излишняя длина ключа игнорируется" + Environment.NewLine +
"Нецифровые символы ключа выдают ошибку";
}
public static string Encode(string inputWord, string key)
{
List<string> list = key.Split(' ').ToList();
list.RemoveAll(ch => ch.Contains(' '));
list = list.Where(ch => !string.IsNullOrWhiteSpace(ch)).ToList();
string formattedWord = inputWord;
while (formattedWord.Length % list.Count != 0)
formattedWord += "~";
string output = String.Empty;
for (int i = 0; i < formattedWord.Length; i += list.Count)
for (int j = 0; j < list.Count; j++)
output += formattedWord.ElementAt(i + Convert.ToInt32(list[j]) - 1);
return output;
}
public static string Decode(string word, string key)
{
return Encode(word, key).Replace("~", "");
}
}
public abstract class SingleTransposing
{
public static string About()
{
return "Порядок символов в исходном слове формируются в соответствии с порядком в ключе" + Environment.NewLine +
"Шифруются любые символы" + Environment.NewLine +
"Нецифровые символы ключа выдают ошибку";
}
public static string Encode(string word, string key)
{
string output = String.Empty;
List<string> list = key.Split(' ').ToList();
list.Remove("");
foreach (var letter in list)
output += word.ElementAt(Convert.ToInt32(letter) - 1);
return output;
}
public static string Decode(string word, string key)
{
string output = String.Empty;
List<string> list = key.Split(' ').ToList();
list.Remove("");
for (int i = 1; i <= list.Count; i++)
output += word.ElementAt(list.IndexOf(i.ToString()));
return output;
}
}
public abstract class VerticalTransposing
{
public static string About()
{
return "Помогите";
}
public static string Encode(string word, string key, bool language)
{
int lang = (language == false) ? 0 : 2;
string output = String.Empty;
string formattedWord = word;
while (formattedWord.Length % key.Length != 0) formattedWord += " ";
int[] code = new int[key.Length];
int numerator = 1;
foreach (var letter in alphabetsList[lang])
for (int i = 0; i < key.Length; i++)
if (Char.ToUpper(key[i]) == letter)
code[i] = numerator++;
List<int> codeList = new List<int>();
for (int i = 0; i < key.Length; i++) codeList.Add(code[i]);
for (int i = 1; i <= key.Length; i++)
for (int j = codeList.IndexOf(i); j < formattedWord.Length; j += key.Length)
output += formattedWord[j];
return output;
}
public static string Decode(string word, string key, bool language)
{
int lang = (language == false) ? 0 : 2;
string output = String.Empty;
string formattedWord = word;
while (formattedWord.Length % key.Length != 0) formattedWord += " ";
int[] code = new int[key.Length];
int numerator = 1;
foreach (var letter in alphabetsList[lang])
for (int i = 0; i < key.Length; i++)
if (Char.ToUpper(key[i]) == letter)
code[i] = numerator++;
List<int> codeList = new List<int>();
for (int i = 0; i < key.Length; i++) codeList.Add(code[i]);
for (int i = 0; i < formattedWord.Length / key.Length; i++)
{
for (int j = 0; j < codeList.Count; j++)
{
output += formattedWord[codeList.IndexOf(j + 1) * formattedWord.Length / key.Length + i];
}
}
return output;
}
}
public abstract class MultiTransposing
{
public static string About()
{
return "О данном методе";
}
public static string Encode(string inputWord, string horizontalKey, string verticalKey)
{
string outputWord = String.Empty;
string formattedWord = inputWord;
while (formattedWord.Length < horizontalKey.Length * verticalKey.Length)
formattedWord += " ";
for (int i = 1; i <= horizontalKey.Length; i++)
for (int j = 1; j <= verticalKey.Length; j++)
outputWord += formattedWord[horizontalKey.Length * verticalKey.IndexOf(j.ToString()) + horizontalKey.IndexOf(i.ToString())];
return outputWord;
}
public static string Decode(string word, string horizontalKey, string verticalKey)
{
string outputWord = String.Empty;
string formattedWord = word;
while (formattedWord.Length < horizontalKey.Length * verticalKey.Length)
formattedWord += " ";
for (int i = 0; i < verticalKey.Length; i++)
for (int j = 0; j < horizontalKey.Length; j++)
outputWord += formattedWord[((int)Char.GetNumericValue(horizontalKey[j]) - 1) * verticalKey.Length + (int)Char.GetNumericValue(verticalKey[i]) - 1];
return outputWord;
}
}
public abstract class Gost
{
public static string About()
{
return "Ключ = 32 символа";
}
public struct FileWork
{
public byte[] FileToByte(string name)
{
try
{
return File.ReadAllBytes(name);
} catch {
return null;
}
}
public void WriteToFile(string name, byte[] fl) {
FileStream file = null;
try
{
file = new FileStream(name, FileMode.Create);
file.Write(fl, 0, fl.Length);
}
catch (IOException exc)
{
MessageBox.Show("Ошибка ввода-вывода: " + exc.Message);
}
catch { }
finally
{
if (file != null)
file.Close();
}
}
}
public abstract class ReplacementTab
{
internal static byte[] getTable { get; } = {
0x4, 0x2, 0xF, 0x5, 0x9, 0x1, 0x0, 0x8, 0xE, 0x3, 0xB, 0xC, 0xD, 0x7, 0xA, 0x6,
0xC, 0x9, 0xF, 0xE, 0x8, 0x1, 0x3, 0xA, 0x2, 0x7, 0x4, 0xD, 0x6, 0x0, 0xB, 0x5,
0xD, 0x8, 0xE, 0xC, 0x7, 0x3, 0x9, 0xA, 0x1, 0x5, 0x2, 0x4, 0x6, 0xF, 0x0, 0xB,
0xE, 0x9, 0xB, 0x2, 0x5, 0xF, 0x7, 0x1, 0x0, 0xD, 0xC, 0x6, 0xA, 0x4, 0x3, 0x8,
0x3, 0xE, 0x5, 0x9, 0x6, 0x8, 0x0, 0xD, 0xA, 0xB, 0x7, 0xC, 0x2, 0x1, 0xF, 0x4,
0x8, 0xF, 0x6, 0xB, 0x1, 0x9, 0xC, 0x5, 0xD, 0x3, 0x7, 0xA, 0x0, 0xE, 0x2, 0x4,
0x9, 0xB, 0xC, 0x0, 0x3, 0x6, 0x7, 0x5, 0x4, 0x8, 0xE, 0xF, 0x1, 0xA, 0x2, 0xD,
0xC, 0x6, 0x5, 0x2, 0xB, 0x0, 0x9, 0xD, 0x3, 0xE, 0x7, 0xA, 0xF, 0x4, 0x1, 0x8 };
}
public class BasicStep
{
uint N1, N2, X;
internal BasicStep(ulong dateFragment, uint keyFragment)
{
N1 = (uint)(dateFragment >> 32);
N2 = (uint)((dateFragment << 32) >> 32);
X = keyFragment;
}
internal ulong BasicEncrypt(bool IsLastStep) { return (FourthAndFifthStep(IsLastStep, ThirdStep(SecondStep(FirstStep())))); }
private uint FirstStep() { return (uint)((X + N1) % (Convert.ToUInt64(Math.Pow(2, 32)))); }
private uint SecondStep(uint S)
{
uint newS, S0, S1, S2, S3, S4, S5, S6, S7;
S0 = S >> 28;
S1 = (S << 4) >> 28;
S2 = (S << 8) >> 28;
S3 = (S << 12) >> 28;
S4 = (S << 16) >> 28;
S5 = (S << 20) >> 28;
S6 = (S << 24) >> 28;
S7 = (S << 28) >> 28;
S0 = ReplacementTab.getTable[S0];
S1 = ReplacementTab.getTable[0x10 + S1];
S2 = ReplacementTab.getTable[0x20 + S2];
S3 = ReplacementTab.getTable[0x30 + S3];
S4 = ReplacementTab.getTable[0x40 + S4];
S5 = ReplacementTab.getTable[0x50 + S5];
S6 = ReplacementTab.getTable[0x60 + S6];
S7 = ReplacementTab.getTable[0x70 + S7];
newS = S7 + (S6 << 4) + (S5 << 8) + (S4 << 12) + (S3 << 16) +
(S2 << 20) + (S1 << 24) + (S0 << 28);
return newS;
}
private uint ThirdStep(uint S)
{
return (uint)(S << 11) | (S >> 21);
}
private ulong FourthAndFifthStep(bool IsLastStep, uint S)
{
ulong N;
S = (S ^ N2);
if (!IsLastStep)
{
N2 = N1;
N1 = S;
} else
N2 = S;
N = ((ulong)N2) | (((ulong)N1) << 32);
return N;
}
}
public abstract class Converter
{
internal static byte[] ConvertToByte(ulong[] fl)
{
byte[] temp = new byte[8];
byte[] encrByteFile = new byte[fl.Length * 8];
for (int i = 0; i < fl.Length; i++)
{
temp = BitConverter.GetBytes(fl[i]);
for (int j = 0; j < temp.Length; j++)
encrByteFile[j + i * 8] = temp[j];
}
return encrByteFile;
}
internal static byte[] ConvertToByte(uint[] fl)
{
byte[] temp = new byte[4];
byte[] encrByteFile = new byte[fl.Length * 4];
for (int i = 0; i < fl.Length; i++)
{
temp = BitConverter.GetBytes(fl[i]);
for (int j = 0; j < temp.Length; j++)
encrByteFile[j + i * 4] = temp[j];
}
return encrByteFile;
}
internal static uint[] GetUIntKeyArray(byte[] byteKey)
{
uint[] key = new uint[byteKey.Length / 4];
for (int i = 0; i < key.Length; i++)
key[i] = BitConverter.ToUInt32(byteKey, i * 4);
return key;
}
internal static ulong[] GetULongDataArray(byte[] byteData)
{
ulong[] data = new ulong[byteData.Length / 8];
for (int i = 0; i < data.Length; i++)
data[i] = BitConverter.ToUInt64(byteData, i * 8);
return data;
}
}
public abstract class D32 : Converter
{
private static byte[] decrByteFile;
private static uint[] uintKey;
private static ulong[] ulongFile;
public static byte[] Decrypt(string word, string key)
{
byte[] byteKey = Encoding.Default.GetBytes(key);
if (word == String.Empty)
return Encoding.Default.GetBytes("Введите слово");
if ((byteKey == null) || (byteKey.Length != 32))
return Encoding.Default.GetBytes("Ключ должен содержать 32 символа");
byte[] btFile = Encoding.Default.GetBytes(word.PadRight(64, ' '));
uintKey = GetUIntKeyArray(byteKey);
ulongFile = GetULongDataArray(btFile);
decrByteFile = ConvertToByte(DecryptFile());
return decrByteFile;
}
internal byte[] GetDecryptFile { get { return decrByteFile; } }
private static ulong[] DecryptFile()
{
BasicStep[] K = new BasicStep[8];
ulong[] ulongDecrFile = new ulong[ulongFile.Length];
for (int k = 0; k < ulongFile.Length; k++)
{
ulongDecrFile[k] = ulongFile[k];
for (int i = 0; i < K.Length; i++)
{
K[i] = new BasicStep(ulongDecrFile[k], uintKey[i]);
ulongDecrFile[k] = K[i].BasicEncrypt(false);
}
for (int j = 0; j < 3; j++)
{
for (int i = K.Length - 1; i >= 0; i--)
{
K[i] = new BasicStep(ulongDecrFile[k], uintKey[i]);
ulongDecrFile[k] = ((j == 2) && (i == 0)) ?
K[i].BasicEncrypt(true) :
K[i].BasicEncrypt(false);
}
}
}
return ulongDecrFile;
}
}
public abstract class E32 : Converter
{