-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar.cs
executable file
·321 lines (276 loc) · 13.3 KB
/
Caesar.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtensionMethods;
namespace CryptanalysisCore
{
public class Caesar : Cipher
{
public const int BruteForceID = 0;
public const int TriangleID = 1;
public Caesar()
: base()
{
ExceptionText = "Klíčem musí být právě jedno malé písmeno anglické abecedy.";
}
protected override void SetCrackMethods()
{
CrackMethods = new CrackMethod[] { BruteForceAttack, TriangleAttack };
}
/// <summary>
/// Metoda zašifruje předaný text pomocí Caesarovy šifry. Každé písmeno v řetězci
/// bude posunuto o tolik znaků, kolik udává parametr 'Klíč'.
/// </summary>
/// <param name="OpenText">Řetězec, který má být zašifrován.</param>
/// <param name="Key">Znak anglické abecedy, který bude hrát úlohu klíče.</param>
/// <returns>Zašifrovaný řetězec.</returns>
public override string Encrypt(string opentext, string key)
{
base.Encrypt(opentext, key);
char[] ciphertext = new char[opentext.Length];
for (int i = 0; i < ciphertext.Length; i++)
{
if (opentext[i] == ' ')
ciphertext[i] = ' ';
else
ciphertext[i] = Analyse.MoveCharacter(opentext[i], key[0]);
}
return new string(ciphertext).ToUpper();
}
/*public override List<string> Crack(string ciphertext, int attackType, Storage.Languages language)
{
return CrackMethods[attackType](ciphertext, language);
}*/
/// <summary>
/// Dešifruje zašifrovaný text podle předaného klíče.
/// </summary>
/// <param name="CipherText">Text, který má být dešifrován.</param>
/// <param name="Key">Klíč, podle kterého má probíhat dešifrování.</param>
/// <returns>Dešifrovaný text.</returns>
public override string Decrypt(string ciphertext, string key)
{
base.Decrypt(ciphertext, key);
// Transformujeme klic tak, abychom mohli pouzit standardni
// funkci na Caesarovu sifru
int movedKey = (int)'z' - (int)key[0] + (int)'a' + 1;
if (movedKey > 'z')
movedKey -= Analyse.AlphabetLetterCount;
char decryptKey = (char)movedKey;
string cipherTextLower = ciphertext.ToLower();
char[] opentext = new char[ciphertext.Length];
for (int i = 0; i < opentext.Length; i++)
{
if (cipherTextLower[i] == ' ')
opentext[i] = ' ';
else
opentext[i] = Analyse.MoveCharacter(cipherTextLower[i], decryptKey);
}
return new string(opentext).ToLower();
}
/// <summary>
/// Ze dvou zadaných znaků vypočítá klíč, kterým byly tyto znaky zašifrovány.
/// </summary>
/// <param name="cipherLetter">Znak z otevřeného textu</param>
/// <param name="openLetter">Znak ze zašifrovaného textu</param>
/// <returns>Klíč</returns>
private char GetKeyFromLetters(char cipherLetter, char openLetter)
{
int c = (int)cipherLetter - (int)'a';
int d = (int)openLetter - (int)'a';
int temp = (cipherLetter - openLetter + Analyse.AlphabetLetterCount);
return ((char)((temp % Analyse.AlphabetLetterCount) + 'a'));
}
private char[] GetPolygonKeys(char[] topTextLetters, char[] topLetters, int complementSize)
{
return TextAnalysis.PolygonAttack(topTextLetters, topLetters, complementSize)
.Select(x => GetKeyFromLetters(x[0], topLetters[0])).ToArray();
}
/// <summary>
/// Provede trojúhelníkový útok na šifru
/// </summary>
/// <param name="packet"></param>
/// <returns></returns>
public List<string> TriangleAttack(string ciphertext, Storage.Languages language)
{
bool modern = true;
if (modern)
{
ciphertext = Analyse.NormalizeText(ciphertext, Analyse.TextTypes.WithoutSpacesLower);
int polygonSize = 3;
int complementSize = 10;
var topLetters = Storage.GetLangChar(language).Letters.OrderByDescending(x => x.Value).Take(polygonSize).Select(x => x.Key[0]).ToArray();
var bottomLetters = Storage.GetLangChar(language).Letters.OrderBy(x => x.Value).Take(polygonSize).Select(x => x.Key[0]).ToArray();
var letters = TextAnalysis.GetOccurrence(ciphertext, 1);
var topTextLetters = letters.OrderByDescending(x => x.Value).Take(complementSize).Select(x => x.Key[0]).ToArray();
var bottomTextLetters = letters.OrderBy(x => x.Value).Take(complementSize).Select(x => x.Key[0]).ToArray();
var keysTop = GetPolygonKeys(topTextLetters, topLetters, complementSize);
var keysBottom = GetPolygonKeys(bottomTextLetters, bottomLetters, complementSize);
if (keysTop.Length == 0)
throw new Exceptions.MatchNotFound();
var interKeys = keysBottom.Intersect(keysTop).ToList();
if (interKeys.Count > 0)
{
return interKeys.Select(x => x.ToString()).ToList();
}
else
{
var unionKeys = keysBottom.Union(keysTop).Select(x => x.ToString()).ToList();
if (unionKeys.Count > 0)
{
return unionKeys;
}
else
{
throw new Exceptions.MatchNotFound();
}
}
}
else
{
char[] keysTop = GetTriangle(ciphertext, true, language);
char[] keysLess = GetTriangle(ciphertext, false, language);
char[] universalKeys = keysLess.Intersect(keysTop).ToArray();
string key;
if (universalKeys.Length == 1)
{
key = universalKeys[0].ToString();
return new List<string>() { key };
}
else
{
char[] unionsKeys = keysLess.Union(keysTop).ToArray();
if (unionsKeys.Length != 0)
{
key = unionsKeys[0].ToString();
return new List<string>() { key };
}
}
throw new Exceptions.MatchNotFound();
}
}
/// <summary>
/// Vrátí pole klíčů, které jsou možnými klíči dané šifry
/// </summary>
/// <param name="packet">Informace o šifrovém textu</param>
/// <returns>Pole klíčů</returns>
private char[] GetTriangle(string ciphertext, bool orderByDescending, Storage.Languages language)
{
char[] topCiphertextLetters;
char[] topLanguagesLetters;
int max = 9;
if (orderByDescending)
{
topCiphertextLetters = Analyse.CharsRelativeOccurrence(ciphertext.ToLower()).OrderByDescending(x => x.Value).Take(max).Select(x => x.Key.ToString().ToLower()[0]).ToArray();
topLanguagesLetters = Storage.GetLangChar(language).Letters.Where(x => x.Key != " ").OrderByDescending(x => x.Value).Take(3).Select(x => x.Key.ToLower()[0]).ToArray();
}
else
{
topCiphertextLetters = Analyse.CharsRelativeOccurrence(ciphertext.ToLower()).OrderBy(x => x.Value).Take(max).Select(x => x.Key.ToString().ToLower()[0]).ToArray();
topLanguagesLetters = Storage.GetLangChar(language).Letters.Where(x => x.Key != " ").OrderBy(x => x.Value).Take(3).Select(x => x.Key.ToLower()[0]).ToArray();
}
List<char[]> triples = FindEquivalentTriple(topLanguagesLetters, topCiphertextLetters);
if (triples.Count == 0)
throw new Exceptions.MatchNotFound();
return GetKeysFromTriples(topLanguagesLetters[0], triples);
}
/// <summary>
/// Vrátí seznam klíčů, kterým byl zašifrován předaný text
/// </summary>
/// <param name="topLetter">Nejčastější písmeno v daném jazyce</param>
/// <param name="triples">Seznam trojic</param>
/// <returns>Pole klíčů</returns>
private char[] GetKeysFromTriples(char topLetter, List<char[]> triples)
{
List<char> keys = new List<char>();
triples.ForEach(triple => keys.Add(GetKeyFromLetters(triple[0], topLetter)));
return keys.ToArray();
}
/// <summary>
/// Zjistí všechny trojice písmen v zašifrovaném textu, které mají stejné vzdálenosti
/// jako nejčastější písmena v běžném textu.
/// </summary>
/// <param name="topLanguagesLetters"></param>
/// <param name="topCiphertextLetters"></param>
/// <returns>Seznam odpovídajících trojic písmen.</returns>
private List<char[]> FindEquivalentTriple(char[] topLanguagesLetters, char[] topCiphertextLetters)
{
int[] distances = GetDistances(topLanguagesLetters);
List<char[]> equivalentTriples = new List<char[]>();
for (int i = 0; i < topCiphertextLetters.Length; i++)
{
for (int j = 0; j < topCiphertextLetters.Length; j++)
{
if (Analyse.MinDistance(topCiphertextLetters[i], topCiphertextLetters[j]) == distances[0])
{
for (int k = 0; k < topCiphertextLetters.Length; k++)
{
if (k != i && k != j)
{
if (Analyse.MinDistance(topCiphertextLetters[j], topCiphertextLetters[k]) == distances[1] &&
Analyse.MinDistance(topCiphertextLetters[i], topCiphertextLetters[k]) == distances[2])
{
equivalentTriples.Add(new char[] { topCiphertextLetters[i], topCiphertextLetters[j], topCiphertextLetters[k] });
}
}
}
}
}
}
return equivalentTriples;
}
/// <summary>
/// Vrací pole reprezentující vzdálenosti v předaných písmenech
/// </summary>
/// <param name="topLanguagesLetters">pole písmen, ve kterých budeme hledat vzdálenosti</param>
/// <returns>Pole vzdáleností.</returns>
private int[] GetDistances(char[] topLanguagesLetters)
{
int[] distances = new int[3];
Cycles.From0ToI(3, i => distances[i] = Analyse.MinDistance(topLanguagesLetters[i], topLanguagesLetters[(i + 1) % 3]));
return distances;
}
/// <summary>
/// Provede útok testováním všech dostupných klíčů
/// a následně vybere nejpravděpodobnější variantu.
/// </summary>
/// <param name="packet"></param>
/// <returns></returns>
public List<string> BruteForceAttack(string ciphertext, Storage.Languages language)
{
Dictionary<char, double> probability = new Dictionary<char, double>();
Caesar caesar = new Caesar();
string opentext;
Analyse.DoAlphabet(letter =>
{
opentext = caesar.Decrypt(ciphertext, letter.ToString());
probability[letter] = Analyse.SimilarityIndex(Analyse.NormalizeText(opentext, Analyse.TextTypes.WithoutSpacesLower), Storage.GetLangChar(language));
});
var resultPacket = probability.OrderBy(x => x.Value).ToArray()[0].Key;
return new List<string>() { resultPacket.ToString() };
}
/// <summary>
/// Zjistí, zda je předaný klíč platný klíč Caesarovy šifry
/// </summary>
/// <param name="Key">Šifrovací klíč</param>
/// <returns>True v případě, že je klíč platný, false jinak</returns>
protected override bool IsKeyValid(string key)
{
return key.Length == 1 && (key[0] >= 'a' && key[0] <= 'z');
}
/// <summary>
/// Vygeneruje náhodný klíč pro Caesarovu šifru
/// Nevygeneruje špatný klíč 'a'.
/// </summary>
/// <returns>Náhodný klíč.</returns>
public override string RandomKey()
{
Random Rand = new Random();
return ((char)((int)'b' + Rand.Next(24))).ToString();
}
public override string ToString()
{
return "Caesar";
}
}
}