-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.cs
253 lines (229 loc) · 8.78 KB
/
Processor.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace Laba1
{
public enum Languages
{
Ru,
Eng
}
public enum Keys
{
Straight,
Progressive,
Auto
}
public class Pair
{
public int Index { get; set; }
public int Value { get; set; }
}
public struct Frequency
{
public char Letter;
public int Count;
}
public class Doubles
{
public char Letter;
public double Frequency;
}
/// <summary>
/// Вычислительное ядро
/// </summary>
public class Processor
{
public static List<Button> Buttons = new List<Button>();
public static List<TextBox> TextBoxs = new List<TextBox>();
public static List<CheckBox> CheckBoxs = new List<CheckBox>();
public static TextBox Goal, KasiskeKey, KasiskeDigrams;
public static List<Doubles> RusDoubles = new List<Doubles>(), EngDoubles = new List<Doubles>();
public static ProgressBar Bar;
public const int MinDigramLength = 5; //+1
public static int DigramLength, KeyLen;
public static Languages Lang;
public static Keys Task;
public static string Text, Key, Cipertext, AnalizeText, AnalizeResult, AnalizeLog;
public const string
BigRus = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ",
SmlRus = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя",
BigEng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
SmlEng = "abcdefghijklmnopqrstuvwxyz";
public static readonly SortedSet<char> RussianText = new SortedSet<char>
{
'А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я',
'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я'
};
public static readonly SortedSet<char> EnglishText = new SortedSet<char>
{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static int Nod(int a, int b)
{
while (true)
{
if (b == 0) return a;
var a1 = a;
a = b;
b = a1%b;
}
}
public static string KeyPreparation(string key, Keys keyType, string text, Languages lang)
{
switch (keyType)
{
case Keys.Straight:
while (key.Length < text.Length)
{
key += key;
}
if (key.Length > text.Length)
{
key = key.Substring(0, text.Length);
}
break;
case Keys.Progressive:
var s = key;
while (key.Length < text.Length)
{
if (lang == Languages.Eng)
{
s = s.Where(c => c != ' ')
.Aggregate("", (current, c) => current + SmlEng[(c - 'a' + 1)%26]);
}
else
{
s = s.Where(c => c != ' ')
.Aggregate("",
(current, c) => current + SmlRus[(Regex.Match(SmlRus, c.ToString()).Index + 1)%33]);
}
key += s;
}
if (key.Length > text.Length)
{
key = key.Substring(0, text.Length);
}
break;
case Keys.Auto:
key += text.Substring(0, text.Length - key.Length);
break;
}
return key;
}
public static MatchCollection Find(string str, string substr)
{
try
{
return new Regex(substr, RegexOptions.IgnoreCase).Matches(str);
}
catch (Exception)
{
return new Regex("a").Matches("b");
}
}
public static List<int> FindProgressive(string String, string subString)
{
var result = new List<int>();
bool haveResult;
var lastDigram = 0;
do
{
haveResult = false;
var subResult = new List<int>();
var i = 0;
while (i < (Lang == Languages.Ru ? SmlRus.Length : SmlEng.Length))
{
var regex = new Regex(subString, RegexOptions.IgnoreCase);
var match = regex.Match(String, lastDigram);
if (match.Success)
subResult.Add(match.Index);
subString = IncString(subString);
i++;
}
subResult.Sort();
if (subResult.Count == 0) continue;
result.Add(subResult[0]);
lastDigram = subResult[0] + DigramLength;
haveResult = true;
} while (haveResult);
return result;
}
public static string IncString(string digram)
{
if (Lang == Languages.Ru)
return digram.Where(char.IsLetter)
.Aggregate("",
(current, c) =>
current + SmlRus[(Regex.Match(SmlRus, c.ToString(), RegexOptions.IgnoreCase).Index + 1)%33]);
return digram.Where(char.IsLetter)
.Aggregate("",
(current, c) =>
current + SmlEng[(Regex.Match(SmlEng, c.ToString(), RegexOptions.IgnoreCase).Index + 1)%26]);
}
public static void DeactivateElements()
{
foreach (var Object in Buttons)
{
Object.IsEnabled = !Object.IsEnabled;
}
foreach (var Object in TextBoxs)
{
Object.IsEnabled = !Object.IsEnabled;
}
foreach (var Object in CheckBoxs)
{
Object.IsEnabled = !Object.IsEnabled;
}
}
public static string GenerateKey(string text, int keyLength)
{
var key = "";
var strings = new string[keyLength];
for (var i = 0; i < text.Length; i++)
{
strings[i%keyLength] += Task == Keys.Straight
? text[i]
: Lang == Languages.Ru
? SmlRus[(SmlRus.IndexOf(char.ToLower(text[i])) + SmlRus.Length - (i/keyLength)%SmlRus.Length)%SmlRus.Length]
: SmlEng[(SmlEng.IndexOf(char.ToLower(text[i])) + SmlEng.Length - (i/keyLength)%SmlEng.Length)%SmlEng.Length];
}
for (var i = 0; i < keyLength; i++)
{
strings[i] = strings[i].ToLower();
}
for (var i = 0; i < keyLength; i++)
{
var frequency = new Frequency[SmlRus.Length];
if (Lang == Languages.Ru)
{
for (var j = 0; j < strings[i].Length; j++)
{
frequency[SmlRus.IndexOf(strings[i][j])].Count++;
frequency[SmlRus.IndexOf(strings[i][j])].Letter = strings[i][j];
}
frequency = frequency.OrderByDescending(p => p.Count).ToArray();
var number =
Math.Abs(SmlRus.IndexOf(frequency[0].Letter) + 33 - SmlRus.IndexOf(RusDoubles[0].Letter))%33;
key += SmlRus[number];
}
else
{
for (var j = 0; j < strings[i].Length; j++)
{
frequency[SmlEng.IndexOf(strings[i][j])].Count++;
frequency[SmlEng.IndexOf(strings[i][j])].Letter = strings[i][j];
}
frequency = frequency.OrderByDescending(p => p.Count).ToArray();
var number =
Math.Abs(SmlEng.IndexOf(frequency[0].Letter) + 26 - SmlEng.IndexOf(EngDoubles[0].Letter)) % 26;
key += SmlEng[number];
}
}
return key;
}
}
}