-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCipherTests.cs
342 lines (262 loc) · 10.2 KB
/
CipherTests.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Science.Cryptography.Ciphers.Specialized;
using System;
namespace Science.Cryptography.Ciphers.Tests;
[TestClass]
[Obsolete("Encapsulate tests for each cipher into a separate class.")]
public class CipherTests
{
[TestMethod]
public void Caesar()
{
var cipher = new ShiftCipher();
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj.";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, WellKnownShiftCipherKeys.Caesar));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, WellKnownShiftCipherKeys.Caesar));
}
[TestMethod]
public void Rot13()
{
var cipher = new ShiftCipher();
const string plaintext = "Why did the chicken cross the road?";
const string ciphertext = "Jul qvq gur puvpxra pebff gur ebnq?";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, WellKnownShiftCipherKeys.Rot13));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, WellKnownShiftCipherKeys.Rot13));
}
[TestMethod]
public void TapCode()
{
var cipher = new TapCode();
const string plaintext = "WATER";
const string ciphertext = "..... .. . . .... .... . ..... .... ..";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext), true);
}
[TestMethod]
public void Multiplicative_Encrypt()
{
var cipher = new MultiplicativeCipher();
const string plaintext = "GEHEIMNIS";
const string ciphertext = "SMVMYKNYC";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, 3));
}
/*
[TestMethod]
public void Bifid()
{
var cipher = new BifidCipher();
char[,] key = new char[,] {
{ 'B', 'Q', 'I', 'F', 'T' },
{ 'G', 'P', 'O', 'C', 'H' },
{ 'W', 'N', 'A', 'L', 'Y' },
{ 'K', 'D', 'X', 'U', 'V' },
{ 'Z', 'S', 'E', 'M', 'R' }
};
const string plaintext = "FLEEATONCE";
const string ciphertext = "UAEOLWRINS";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key), true);
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key), true);
}
*/
[TestMethod]
public void Vigenère()
{
var cipher = new VigenèreCipher();
const string key = "Lemon";
const string plaintext = "Attack at dawn";
const string ciphertext = "Lxfopv ef rnhr";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
[TestMethod]
public void Bacon()
{
var cipher = new BaconCipher();
const string plaintext = "HELLO WORLD";
const string ciphertext = "AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext), true);
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext), true);
}
[TestMethod]
public void Autokey()
{
var cipher = new AutokeyCipher();
const string key = "QUEENLY";
const string plaintext = "Attack AT DAWN";
const string ciphertext = "Qnxepv YT WTWP";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
/*
[TestMethod]
public void Sandorf()
{
var cipher = new SandorfCipher();
bool[,] key = {
{ false, false, false, false, false, false, },
{ true, false, false, true, false, false },
{ false, false, true, false, false, false },
{ true, false, false, false, false, true },
{ false, true, false, true, false, false },
{ true, false, false, false, true, false }
};
const string plaintext = "Real Programmers use Fortran. Quiche Eaters use Pascal.";
const string ciphertext = "u#l# #as#r##ec##st##aa##EP# ## #e#s.geuhoc raPir sutrlrQae oemFR .meanrs";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
}
*/
[TestMethod]
public void TwoSquare()
{
var cipher = new TwoSquareCipher();
var key = (
PolybiusSquare.CreateFromKeyword("EXAMPLE", WellKnownAlphabets.EnglishWithoutQ),
PolybiusSquare.CreateFromKeyword("KEYWORD", WellKnownAlphabets.EnglishWithoutQ)
);
const string plaintext = "Help me Obiwan Kenobi!";
const string ciphertext = "Hedl xw Sdjyan Hotkdg!";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
}
[TestMethod]
public void FourSquare()
{
var cipher = new FourSquareCipher();
var key = (
PolybiusSquare.CreateFromAlphabet(WellKnownAlphabets.EnglishWithoutQ),
PolybiusSquare.CreateFromKeyword("EXAMPLE", WellKnownAlphabets.EnglishWithoutQ),
PolybiusSquare.CreateFromKeyword("KEYWORD", WellKnownAlphabets.EnglishWithoutQ),
PolybiusSquare.CreateFromAlphabet(WellKnownAlphabets.EnglishWithoutQ)
);
const string plaintext = "Help me Obiwan Kenobi";
const string ciphertext = "Fygm ky Hobxmf Kkkimd";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
}
[TestMethod]
public void Trithemius()
{
var cipher = new TrithemiusCipher();
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "Tig tynir jayhz scm zleim jrbp shf nddd jvo.";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void Gronsfeld()
{
var cipher = new GronsfeldCipher();
int[] key = { 3, 2, 6 };
const string plaintext = "Geheimnis";
const string ciphertext = "Jgnhksqky";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
}
[TestMethod]
public void VariantBeaufort()
{
var cipher = new VariantBeaufortCipher();
const string key = "CODE";
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "Rtb msuzg zdlsl rlt hgjlq asap fea jmwu bad.";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
[TestMethod]
public void Gudhayojya()
{
var cipher = new GudhayojyaCipher();
const string key = "dis";
const string plaintext = "will visit you tonight.";
const string ciphertext = "diswill disvisit disyou distonight.";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
[TestMethod]
public void Beaufort()
{
var cipher = new BeaufortCipher();
const string key = "m";
const string plaintext = "d J";
const string ciphertext = "j D";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
[TestMethod]
public void Base64()
{
var cipher = new Base64Encoder();
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void Hex()
{
var cipher = new HexEncoder();
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext), ignoreCase: true);
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void Binary()
{
var cipher = new AsciiBinaryBase();
const string plaintext = "The quick brown fox jumps over the lazy dog.";
const string ciphertext = "0101010001101000011001010010000001110001011101010110100101100011011010110010000001100010011100100110111101110111011011100010000001100110011011110111100000100000011010100111010101101101011100000111001100100000011011110111011001100101011100100010000001110100011010000110010100100000011011000110000101111010011110010010000001100100011011110110011100101110";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void Playfair()
{
var cipher = new PlayfairCipher();
const string plaintext = "Hide the GOLD IN THE TREXE STUMP.";
const string ciphertext = "Bmod zbx DNAB EK UDM UIXMM OUVIF.";
var square = PolybiusSquare.CreateFromCharacters("PLAYFIREXMBCDGHKNOQSTUVWZ");
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, square));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, square));
}
[TestMethod]
public void Malespin()
{
var cipher = new MalespinSlang();
const string plaintext = "Malespin";
const string ciphertext = "Pelasmon";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void Wolfenbütteler()
{
var cipher = new WolfenbüttelerCipher();
const string plaintext = "Beispielklartext";
const string ciphertext = "Bkdspdklelmrokxo";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext));
}
[TestMethod]
public void OneTimePad()
{
var cipher = new OneTimePadCipher();
const string plaintext = "Hello";
const string ciphertext = "Eqnvz";
const string key = "XMCKL";
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key.ToCharArray()));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key.ToCharArray()));
}
[TestMethod]
public void Scytale()
{
var cipher = new ScytaleCipher();
const string plaintext = "Iamhurtverybadlyhelp";
const string ciphertext = "Iryyatbhmvaehedlurlp";
const int key = 4;
Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, key));
Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, key));
}
}