-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
executable file
·624 lines (531 loc) · 23 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
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace CryptoPalsChallenge
{
class Program
{
static byte[] FromHex(string str)
{
byte[] bytes = new byte[str.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = byte.Parse(str.Substring(i * 2, 2), NumberStyles.HexNumber);
}
return bytes;
}
static string ToHex(byte[] bytes)
{
var builder = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
{
builder.AppendFormat("{0:x2}", bytes[i]);
}
return builder.ToString();
}
static byte[] XorBytes(byte[] b1, byte[] b2)
{
return Challenge2.XorBytes(b1, b2);
}
static byte[] XorBytes(byte[] b1, byte b2)
{
var result = new byte[b1.Length];
for (int i = 0; i < b1.Length; i++)
{
result[i] = (byte)(b1[i] ^ b2);
}
return result;
}
static int HammingDistance(Span<byte> b1, Span<byte> b2)
{
int result = 0;
for (int i = 0; i < b1.Length; i++)
{
byte b = (byte)(b1[i] ^ b2[i]);
while (b > 0)
{
if ((b & 0x01) == 0x01)
result++;
b >>= 1;
}
}
return result;
}
static bool LooksLikeAscii(byte b)
{
return (b >= 32 && b <= 127)
|| (b == 13) || (b == 10);
}
static string SingleCharacterXor(IEnumerable<byte> cipherText, byte xor)
{
var decodedBytes = cipherText.Select(b => (byte)(b ^ xor)).ToArray();
return decodedBytes.All(LooksLikeAscii)
? Encoding.ASCII.GetString(decodedBytes).Replace('\r', '\n')
: null;
}
static void Challenge4()
{
string[] textArray = Utility.GetResource("4.txt").Split('\r', '\n');
for (int i = 0; i < textArray.Length; i++)
{
var cipherText = FromHex(textArray[i]);
var bytes = new byte[cipherText.Length];
for (int xor = 0x00; xor <= 0xFF; xor++)
{
string s = SingleCharacterXor(cipherText, (byte)xor);
if (s != null)
{
Console.WriteLine($"{i}:{xor}: {s}");
}
}
}
}
/// <summary>
/// Gauge which key sizes are likely to be the target
/// </summary>
static int[] AppraiseKeySizes(Span<byte> cipherText)
{
var dict = new Dictionary<int, double>();
for (int keySize = 2; keySize <= 40; keySize++)
{
double distance = 0;
Span<byte> firstSlice = cipherText.Slice(0, keySize);
for (int i = 1; i <= 10; i++)
{
Span<byte> thisSlice = cipherText.Slice(keySize * i, keySize);
distance += HammingDistance(firstSlice, thisSlice) / (double)keySize;
}
dict.Add(keySize, distance);
}
return (from keySize in dict.Keys.OrderBy(ks => dict[ks])
select keySize).ToArray();
}
private struct PossibleXor
{
public byte Xor;
public int Quality;
}
static IEnumerable<byte[]> EnumeratePossibleKeys(byte[] cipherText, int keySize)
{
var possibleXors = new List<PossibleXor>[keySize];
for (int i = 0; i < keySize; i++)
{
possibleXors[i] = new List<PossibleXor>();
for (int xor = 0x00; xor <= 0xFF; xor++)
{
var stag = Utility.Stagger<byte>(cipherText, keySize, i);
string s = SingleCharacterXor(stag, (byte)xor);
if (s != null)
{
PossibleXor px;
px.Xor = (byte)xor;
px.Quality = s.Count(c => char.IsLetter(c) || c == ' ');
possibleXors[i].Add(px);
}
}
// Sort by our quality heuristic - we don't know precisely which printable characters will be
// present and which ones won't be, but we know that letters are likely
possibleXors[i].Sort((x, y) => System.Collections.Comparer.Default.Compare(y.Quality, x.Quality));
}
// We now have a list of possible XORs for each position. Now we need to return
// every permutation
var indexes = new int[keySize];
var result = new byte[keySize];
bool done = false;
while(!done)
{
// Build the result and return it
for (int i = 0; i < keySize; i++)
result[i] = possibleXors[i][indexes[i]].Xor;
yield return result;
// Bump the counter
bool doneIncrementing = false;
int incrementPos = 0;
while(!doneIncrementing && incrementPos < keySize)
{
doneIncrementing = ++indexes[incrementPos] < possibleXors[incrementPos].Count;
if (!doneIncrementing)
indexes[incrementPos++] = 0;
}
done = incrementPos >= keySize;
}
}
private static string TryDecode(byte[] cipherText, byte[] key)
{
byte[] decodedCipherText = new byte[cipherText.Length];
for (int i = 0; i < decodedCipherText.Length; i++)
{
decodedCipherText[i] = (byte)(cipherText[i] ^ key[i % key.Length]);
}
return Encoding.ASCII.GetString(decodedCipherText);
}
static void Challenge6()
{
string text = Utility.GetResource("6.txt");
byte[] cipherText = Convert.FromBase64String(text);
// Get candidate keys
var candidates = from keySize in AppraiseKeySizes(cipherText)
from possibleKey in EnumeratePossibleKeys(cipherText, keySize)
select TryDecode(cipherText, possibleKey);
foreach(var cand in candidates)
{
Console.WriteLine(cand);
Console.WriteLine("---------------");
}
}
static byte[] EcbDecrypt(byte[] cipherText, byte[] keyBytes)
{
using (var aes = Aes.Create())
{
aes.Key = keyBytes;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.ECB;
using (var decryptor = aes.CreateDecryptor(keyBytes, aes.IV))
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (MemoryStream target = new MemoryStream())
{
var buffer = new byte[1024];
int len;
while((len = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, len);
}
return target.ToArray();
}
}
}
static byte[] Encrypt(byte[] plainText, byte[] keyBytes, byte[] iv, CipherMode cipherMode)
{
return Utility.Encrypt(plainText, keyBytes, iv, cipherMode);
}
static void Challenge7()
{
string text = Utility.GetResource("7.txt");
byte[] cipherText = Convert.FromBase64String(text);
string key = "YELLOW SUBMARINE";
byte[] keyBytes = Encoding.ASCII.GetBytes(key);
byte[] resultBytes = EcbDecrypt(cipherText, keyBytes);
Console.WriteLine(Encoding.ASCII.GetString(resultBytes));
}
static void Challenge8()
{
string[] texts = (from string line in Utility.GetResource("8.txt").Split('\r', '\n')
where !string.IsNullOrEmpty(line)
select line).ToArray();
byte[][] cipherTexts = texts.Select(FromHex).ToArray();
var distances = new Dictionary<int, int>();
for (int i = 0; i < cipherTexts.Length; i++)
{
if (cipherTexts[i].Length != 160)
{
throw new Exception();
}
int dist = 0;
for (int j = 0; j < 16; j++)
{
dist += Utility.Stagger(cipherTexts[i], 16, j).Distinct().Count();
}
distances[i] = dist;
}
foreach(var index in distances.OrderBy(pair => pair.Value).Select(pair => pair.Key))
{
Console.WriteLine($"index={index} distance={distances[index]}");
}
}
static byte[] Pkcs7(byte[] bytes, int multiple)
{
int paddingSize = multiple - (bytes.Length % multiple);
byte[] newBytes = new byte[bytes.Length + paddingSize];
for (int i = 0; i < bytes.Length; i++)
{
newBytes[i] = bytes[i];
}
for (int i = bytes.Length; i < newBytes.Length; i++)
{
newBytes[i] = (byte)paddingSize;
}
return newBytes;
}
static void Challenge9()
{
byte[] bytes = Encoding.ASCII.GetBytes("YELLOW SUBMARINE");
byte[] newBytes = Pkcs7(bytes, 20);
}
private static byte[] CbcDecrypt(byte[] cipherText, byte[] keyBytes)
{
using (MemoryStream memStream = new MemoryStream())
{
const int chunkSize = 16;
byte[] lastChunk = new byte[chunkSize];
for (int i = 0; i < cipherText.Length; i += chunkSize)
{
int thisChunkSize = Math.Min(chunkSize, cipherText.Length - i);
byte[] chunk = new byte[thisChunkSize];
Array.Copy(cipherText, i, chunk, 0, thisChunkSize);
byte[] decodedBytes = EcbDecrypt(chunk, keyBytes);
for (int j = 0; j < decodedBytes.Length; j++)
{
decodedBytes[j] ^= lastChunk[j];
}
lastChunk = chunk;
memStream.Write(decodedBytes, 0, decodedBytes.Length);
}
return memStream.ToArray();
}
}
static void Challenge10()
{
byte[] keyBytes = Encoding.ASCII.GetBytes("YELLOW SUBMARINE");
string text = Utility.GetResource("10.txt");
byte[] cipherText = Convert.FromBase64String(text);
byte[] decodedBytes = CbcDecrypt(cipherText, keyBytes);
Console.Write(Encoding.ASCII.GetString(decodedBytes));
}
static byte[] EncryptionOracle(byte[] plainText, out CipherMode mode)
{
var rand = new Random();
// Generate random key
byte[] keyBytes = new byte[16];
rand.NextBytes(keyBytes);
// Prepend and append random bytes
int prependBytes = rand.Next() % 6 + 5;
int appendBytes = rand.Next() % 6 + 5;
byte[] newPlainText = new byte[plainText.Length + prependBytes + appendBytes];
rand.NextBytes(newPlainText);
Array.Copy(plainText, 0, newPlainText, prependBytes, plainText.Length);
byte[] randomIv = new byte[16];
rand.NextBytes(randomIv);
mode = rand.Next() % 2 == 0 ? CipherMode.CBC : CipherMode.ECB;
return Encrypt(
newPlainText,
keyBytes,
mode == CipherMode.CBC ? randomIv : null,
mode);
}
static void Challenge11()
{
// A big zero byte plaintext
byte[] plainText = new byte[0x1000];
for (int i = 0; i < 20; i++)
{
byte[] cipherText = EncryptionOracle(plainText, out CipherMode actualMode);
int dist = 0;
for (int j = 0; j < 16; j++)
{
dist += Utility.Stagger(cipherText, 16, j).Distinct().Count();
}
var predictedCipherMode = dist > 200 ? CipherMode.CBC : CipherMode.ECB;
Console.WriteLine($"predictedCipherMode={predictedCipherMode} actualMode={actualMode} success={predictedCipherMode == actualMode}");
}
}
static byte[] RandomKey()
{
var rand = new Random();
var bytes = new byte[16];
rand.NextBytes(bytes);
return bytes;
}
private static byte[] keyBytesChallenge12 = RandomKey();
static byte[] EncryptionOracle12(byte[] plainText)
{
var keyBytes = keyBytesChallenge12;
// Prepend and append random bytes
byte[] appendBytes = Convert.FromBase64String(Utility.GetResource("12.txt"));
byte[] newPlainText = Utility.Concat(plainText, appendBytes);
return Encrypt(
newPlainText,
keyBytes,
null,
CipherMode.ECB);
}
static byte[] Repeat(byte b, int count)
{
var result = new byte[count];
for (int i = 0; i < count; i++)
{
result[i] = b;
}
return result;
}
static bool Compare<T>(T[] array1, int startX, T[] array2, int startY, int count)
{
if (startX < 0) throw new ArgumentOutOfRangeException(nameof(startX));
if (startY < 0) throw new ArgumentOutOfRangeException(nameof(startY));
if ((count < 0) || (startX + count > array1.Length) || (startY + count > array2.Length))
throw new ArgumentOutOfRangeException(nameof(count));
for (int i = 0; i < count; i++)
{
if (!Equals(array1[startX + i], array2[startY + i]))
return false;
}
return true;
}
static bool Compare<T>(T[] array, int startX, int startY, int count)
{
return Compare(array, startX, array, startY, count);
}
static void Challenge12()
{
int GetKeySize()
{
int potentialKeySize;
for (potentialKeySize = 1; potentialKeySize < 32; potentialKeySize++)
{
byte[] bytes = Repeat(42, potentialKeySize * 2);
byte[] encryptedBytes = EncryptionOracle12(bytes);
if (Compare(encryptedBytes, 0, potentialKeySize, potentialKeySize))
return potentialKeySize;
}
throw new Exception();
}
// Get the key size
int keySize = GetKeySize();
// Is this ECB?
byte[] zeroPlainText = new byte[0x1000];
byte[] zeroCipherText = EncryptionOracle12(zeroPlainText);
int dist = 0;
for (int j = 0; j < keySize; j++)
{
dist += Utility.Stagger(zeroCipherText, keySize, j).Distinct().Count();
}
bool isECB = dist <= 200;
if (!isECB)
throw new Exception();
// Get the length of the message we're trying to decode
int messageLength = zeroCipherText.Length - zeroPlainText.Length;
byte[] decodedMessage = new byte[messageLength];
for (int i = 0; i < messageLength; i++)
{
byte arbitraryByte = 42;
var buffer = Repeat(arbitraryByte, keySize - 1 - (i % keySize));
byte[] encryptedBuffer = EncryptionOracle12(buffer);
byte? thisByte = null;
for (int b = 0x00; thisByte == null && (b <= 0xFF); b++)
{
var concatBuffer = Utility.Concat(
buffer,
Utility.Pluck(decodedMessage, 0, i),
new[] { (byte)b });
var encryptedConcatBuffer = EncryptionOracle12(concatBuffer);
if (Compare(encryptedBuffer, 0, encryptedConcatBuffer, 0, concatBuffer.Length))
thisByte = (byte)b;
}
decodedMessage[i] = thisByte.Value;
}
Console.Write(Encoding.ASCII.GetString(decodedMessage).Replace("\0", ""));
}
static string ProfileFor(string email)
{
var queryBuilder = new QueryBuilder();
queryBuilder.Add("email", email);
queryBuilder.Add("uid", "10");
queryBuilder.Add("role", "user");
return queryBuilder.ToQueryString().Value;
}
static void Challenge13()
{
var keyBytes = RandomKey();
string profile = ProfileFor("[email protected]");
byte[] encryptedProfile = Encrypt(Encoding.ASCII.GetBytes(profile), keyBytes, null, CipherMode.ECB);
byte[] decryptedProfile = EcbDecrypt(encryptedProfile, keyBytes);
var parsed = QueryHelpers.ParseQuery(Encoding.ASCII.GetString(decryptedProfile));
}
static byte[] _encryptionOracle14Prefix;
static byte[] EncryptionOracle14(byte[] plainText)
{
if (_encryptionOracle14Prefix == null)
{
var r = new Random();
_encryptionOracle14Prefix = new byte[(r.Next() % 100) + 50];
r.NextBytes(_encryptionOracle14Prefix);
}
var concatPlainText = Utility.Concat(_encryptionOracle14Prefix, plainText);
return EncryptionOracle12(concatPlainText);
}
static void Challenge14()
{
int GetKeySize()
{
byte[] encryptedEmptyBytes = EncryptionOracle14(new byte[0]);
int potentialKeySize;
for (potentialKeySize = 1; potentialKeySize < 32; potentialKeySize++)
{
byte[] bytes = Repeat(42, potentialKeySize);
byte[] encryptedBytes = EncryptionOracle14(bytes);
if (Compare(encryptedEmptyBytes, encryptedEmptyBytes.Length - potentialKeySize, encryptedBytes, encryptedBytes.Length - potentialKeySize, potentialKeySize))
return potentialKeySize;
}
throw new Exception();
}
// Get the key size
int keySize = GetKeySize();
// Now determine how many complete prefix blocks we have
var cipherText1 = EncryptionOracle14(Enumerable.Range(0, keySize).Select(i => (byte)i).ToArray());
var cipherText2 = EncryptionOracle14(Enumerable.Range(1, keySize).Select(i => (byte)i).ToArray());
int completePrefixBlocks = 0;
while (Compare(cipherText1, completePrefixBlocks * keySize, cipherText2, completePrefixBlocks * keySize, keySize))
completePrefixBlocks++;
bool TryPartialPrefixSize(int potentialPartialPrefixLength)
{
// Arbitrary values, must be different
const byte b1 = 0x00;
const byte b2 = 0xFF;
var buffer1 = Utility.Concat(
Repeat(b1, keySize - potentialPartialPrefixLength),
Repeat(b2, potentialPartialPrefixLength));
var buffer2 = Repeat(b2, keySize);
var encryptedBuffer1 = EncryptionOracle14(buffer1);
var encryptedBuffer2 = EncryptionOracle14(buffer2);
return Compare(
encryptedBuffer1, (completePrefixBlocks + 1) * keySize,
encryptedBuffer2, (completePrefixBlocks + 1) * keySize,
keySize);
}
// Now determine the partial prefix size
int partialPrefixLength = keySize - 1;
while (partialPrefixLength > 0 && TryPartialPrefixSize(partialPrefixLength - 1))
partialPrefixLength--;
// We now know the full prefix length
int prefixLength = completePrefixBlocks * keySize + partialPrefixLength;
// Get the length of the message we're trying to decode
int messageLength = EncryptionOracle14(new byte[] { }).Length - prefixLength;
// Create padding to neutralize the prefix
var padding = Repeat(0xCD, keySize - partialPrefixLength);
int paddedOffset = keySize * (completePrefixBlocks + 1);
byte[] decodedMessage = new byte[messageLength];
for (int i = 0; i < messageLength; i++)
{
byte arbitraryByte = 42;
var buffer = Utility.Concat(
padding,
Repeat(arbitraryByte, keySize - 1 - (i % keySize)));
byte[] encryptedBuffer = EncryptionOracle14(buffer);
byte? thisByte = null;
for (int b = 0x00; thisByte == null && (b <= 0xFF); b++)
{
var concatBuffer = Utility.Concat(
buffer,
Utility.Pluck(decodedMessage, 0, i),
new[] { (byte)b });
var encryptedConcatBuffer = EncryptionOracle14(concatBuffer);
if (Compare(encryptedBuffer, paddedOffset, encryptedConcatBuffer, paddedOffset, concatBuffer.Length - padding.Length))
thisByte = (byte)b;
}
decodedMessage[i] = thisByte.Value;
}
Console.Write(Encoding.ASCII.GetString(decodedMessage).Replace("\0", ""));
}
static void Main(string[] args)
{
Challenge20.Run();
Console.ReadLine();
}
}
}