X-Git-Url: https://code.kerkeslager.com/?p=sandbox;a=blobdiff_plain;f=cryptopals-python%2Fcryptopals.py;h=56501a1c29e3bb315aebeaffc62d0f2960180655;hp=c5d2704e8842a305165152c7d70b58366461f3d3;hb=0e145fcc62b755cb151c10252a992c55f03d09b2;hpb=2970421707a14c859a48f4e3c409af215ea4f43e diff --git a/cryptopals-python/cryptopals.py b/cryptopals-python/cryptopals.py index c5d2704..56501a1 100644 --- a/cryptopals-python/cryptopals.py +++ b/cryptopals-python/cryptopals.py @@ -27,6 +27,11 @@ def compare_frequency_deviation(base_frequency, comparison_frequency): for character, frequency in base_frequency.items() ) / len(base_frequency) +with open('sample.txt','r') as sample_file: + sample_text = sample_file.read() + +SAMPLE_FREQUENCIES = get_character_frequencies(sample_text) + class Set1Challenge1Tests(unittest.TestCase): def test_converts_hex_to_base64(self): expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n' @@ -45,11 +50,6 @@ class Set1Challenge2Tests(unittest.TestCase): class Set1Challenge3Tests(unittest.TestCase): def test_gets_message(self): - with open('sample.txt','r') as sample_file: - sample_text = sample_file.read() - - sample_frequencies = get_character_frequencies(sample_text) - xored_string = bytes.fromhex('1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736') lowest_frequency_deviation_string = None @@ -60,7 +60,7 @@ class Set1Challenge3Tests(unittest.TestCase): key = bytes([i]) * len(xored_string) try_string = xor_bytes(xored_string, key).decode('utf-8') try_string_frequency_deviation = compare_frequency_deviation( - sample_frequencies, + SAMPLE_FREQUENCIES, get_character_frequencies(try_string), ) @@ -74,5 +74,39 @@ class Set1Challenge3Tests(unittest.TestCase): self.assertEqual(expected, actual) +class Set1Challenge4Tests(unittest.TestCase): + def test_gets_message(self): + with open('set1challenge4.txt','r') as f: + lines = f.readlines() + + + lowest_frequency_deviation_string = None + lowest_frequency_deviation = None + + for line in lines: + line_bytes = bytes.fromhex(line) + + for i in range(128): + key_char = bytes([i]).decode('utf-8') + key = bytes([i]) * len(line_bytes) + + try: + try_string = xor_bytes(line_bytes, key).decode('utf-8') + try_string_frequency_deviation = compare_frequency_deviation( + SAMPLE_FREQUENCIES, + get_character_frequencies(try_string), + ) + + if lowest_frequency_deviation is None or try_string_frequency_deviation < lowest_frequency_deviation: + lowest_frequency_deviation_string = try_string + lowest_frequency_deviation = try_string_frequency_deviation + except: + pass + + expected = 'Now that the party is jumping\n' + actual = lowest_frequency_deviation_string + + self.assertEqual(expected, actual) + if __name__ == '__main__': unittest.main()