public void EncipherText() { var message = Basic.AsciiToBytes(Basic.GetFileText(@"..\..\Assets\TestIsEnglishDistributed.txt")); var key = Basic.AsciiToBytes("ICE"); var cipher = Basic.XorRepeatKey(message, key); Console.WriteLine(Basic.BytesToHex(cipher)); }
public void TestS2C9() { const string message = "YELLOW SUBMARINE"; const string answer = "59454c4c4f57205355424d4152494e4504040404"; var paddedMessage = Basic.BytesToHex(BlockAndStream.Pad(Basic.AsciiToBytes(message), 20)); Console.WriteLine(paddedMessage); Console.WriteLine(answer); Assert.AreEqual(answer, paddedMessage); }
public void TestTransposeMatrix() { var before = "db135345f20a225c01010101c6c6c6c6"; var after = "dbf201c6130a01c6532201c6455c01c6"; Console.WriteLine("Before: {0}\nGoal: {1}", before, after); var result = Basic.BytesToHex(Basic.Aes.Util.TransposeMatrix(Basic.HexToBytes(before))); Console.WriteLine("Transposed: {0}", result); Assert.AreEqual(result, after); }
public void TestS1C2() { const string input = "1c0111001f010100061a024b53535009181c"; const string fixedXor = "686974207468652062756c6c277320657965"; const string target = "746865206b696420646f6e277420706c6179"; var inputByteArray = Basic.HexToBytes(input); var fixedXorByteArray = Basic.HexToBytes(fixedXor); var xor = Basic.Xor(inputByteArray, fixedXorByteArray); Assert.AreEqual(Basic.BytesToHex(xor).ToUpper(), target.ToUpper()); }
public void TestS1C5() { const string message = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; const string target = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"; const string key = "ICE"; var messageBytes = Basic.AsciiToBytes(message); var keyBytes = Basic.AsciiToBytes(key); var eMessageBytes = Basic.XorRepeatKey(messageBytes, keyBytes); var eMessage = Basic.BytesToHex(eMessageBytes); Assert.AreEqual(target, eMessage); }
public void TestS1C4() { const string path = @"..\..\Assets\S1C4.txt"; var ciphers = new List <string>(); var cipherMatches = new List <List <Tuple <double, byte, char> > >(); using (var r = new StreamReader(path)) { string line; while ((line = r.ReadLine()) != null) { List <Tuple <double, byte, char> > matches; var score = Basic.IsLanguage(Basic.HexToBytes(line), Basic.EnglishCharacterFrequencies, out matches); if (score <= 0.8) { continue; } ciphers.Add(line); cipherMatches.Add(matches); } } for (var i = 0; i < ciphers.Count; i++) { var cipher = ciphers[i]; var cipherMatch = cipherMatches[i]; Console.WriteLine("\n-- Cipher: {0} --", cipher); var keys = Basic.GetKeysFromLanguageMatches(cipherMatch, 5); foreach (var key in keys) { var translation = Basic.XorRepeatKey(Basic.HexToBytes(cipher), new[] { key }); if (Basic.IsEnglish(translation, Basic.EnglishCharacterFrequencies) > 0.5) { Console.WriteLine("key: {0}[{1}] - trans: {2}", Basic.BytesToHex(new[] { key }), Basic.BytesToAscii(new[] { key }), Basic.BytesToAscii(translation)); } } } }
public void TestIsLanguage() { const string path = @"..\..\Assets\TestIsEnglishDistributed.txt"; string plaintext; using (var s = new StreamReader(path)) { plaintext = s.ReadToEnd().ToLower(); } var bytes = Basic.AsciiToBytes(plaintext); List <Tuple <double, byte, char> > matches; var score = Basic.IsLanguage(bytes, Basic.EnglishCharacterFrequencies, out matches); var keys = new Dictionary <byte, double>(); Console.WriteLine("-- {0:P} similar to English character distribution --", score); foreach (var match in matches) { var key = Basic.Xor(Basic.AsciiToBytes(match.Item3 + ""), new[] { match.Item2 }); if (keys.ContainsKey(key[0])) { keys[key[0]] += match.Item1; } else { keys.Add(key[0], match.Item1); } Console.WriteLine("Score: {0:P} - Char: {1} - Byte: {2} - Key: {3}", match.Item1, match.Item3, Basic.BytesToAscii(new[] { match.Item2 }), Basic.BytesToHex(key)); } Console.WriteLine("\n-- Possible Keys --"); foreach (var key in keys.OrderByDescending(x => x.Value)) { Console.WriteLine("score: {0:P} - key: {1}", key.Value, Basic.BytesToHex(new[] { key.Key })); } }