-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge20.cs
executable file
·120 lines (107 loc) · 4.37 KB
/
Challenge20.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CryptoPalsChallenge
{
/// <seealso cref="https://cryptopals.com/sets/3/challenges/20"/>
/// <remarks>
/// We already automated a lot of this for Challenge 19 - therefore, we will go for extra credit by using simulated
/// annealing to decode the last bit. While this isn't specified by the challenge, challenge #20 is particularly
/// vague about what criteria constitutes success
/// </remarks>
public static class Challenge20
{
private static string[] DecodeWithXor(byte[][] cipherTexts, byte[] xor)
{
return cipherTexts
.Select(x => Challenge2.XorBytes(x, xor))
.Select(x => Encoding.ASCII.GetString(x))
.ToArray();
}
private static IEnumerable<string> GetWords(string text)
{
return text.Split(' ', '-');
}
private static Regex _pseudoWord = new Regex(@"^[A-Za-z][a-z]*$", RegexOptions.Compiled);
private static Regex _containsVowel = new Regex(@"A|E|I|O|U", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static int Score(byte[][] cipherTexts, byte[] xor)
{
int result = 0;
foreach(var word in from line in DecodeWithXor(cipherTexts, xor)
from word in GetWords(line)
select word.TrimEndSingle('!', ',', '.', '?'))
{
if (Utility.IsWord(word))
{
result += 100;
}
else if (_pseudoWord.IsMatch(word))
{
result += 25;
if (_containsVowel.IsMatch(word))
{
result += 50;
}
}
// Score against caps after the beginning
if (word.Length > 0 && char.IsLower(word[0]))
{
for (int i = 1; i < word.Length; i++)
{
if (char.IsUpper(word, i))
{
result -= 10;
break;
}
}
}
}
return result;
}
public static void Run()
{
// "Encrypted" ciphertext
byte[][] cipherTexts = Challenge19.CtrEncodeBase64Strings(Utility.GetResource("20.txt"));
// We're treating decrypting as nothing more than applying a XOR; we deduced the value
byte[] xor = Challenge19.CrackXor(cipherTexts);
// Anneal
int score = Score(cipherTexts, xor);
int[] tweakCounts = new[] { 1, 1, 1, 2, 2, 3, 3, 4, 5, 6 };
int startTemp = 100000;
for (int i = startTemp; i >= 0; i--)
{
byte[] newXor = Utility.Dupe(xor);
int? firstPos = null;
int tweakCount = tweakCounts[Utility.Random(tweakCounts.Length)];
for (int j = 0; j < tweakCount; j++)
{
int range = 7;
int pos = firstPos == null
? Utility.Random(xor.Length)
: firstPos.Value + Utility.Random(range * 2 + 1) - range;
if (pos >= 0 && pos < xor.Length)
{
firstPos = pos;
bool setOrClear = Utility.Random(2) == 1;
byte mask = (byte)(1 << Utility.Random(8));
newXor[pos] |= (byte)(setOrClear ? mask : 0);
newXor[pos] &= (byte)~(setOrClear ? 0 : mask);
}
}
int newScore = Score(cipherTexts, newXor);
if (newScore > score)
{
xor = newXor;
score = newScore;
}
}
string[] results = DecodeWithXor(cipherTexts, xor);
for (int i = 0; i < results.Length; i++)
{
Console.WriteLine($"#{i:d2}: {results[i]}");
}
}
}
}