-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_3.py
31 lines (23 loc) · 867 Bytes
/
test_3.py
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
import unittest
from util.somecode import *
from bitstring import BitArray as BA
def find_best_message(e:BA) -> str:
scores = []
for c in map(chr, range(256)):
i = ord(c)
key = BA(hex(i) * len(e.bytes))
if len(key) < len(e):
key = key * 2
candidate = (e ^ key)
scores.append((dictionary_word_count(candidate), c, to_str(candidate)))
best = sorted(scores, key=lambda x: x[0])[-1]
return best[2]
class TestChallenge3(unittest.TestCase):
def test_decrypt(self):
# I guess assume these people are thinking in C
# Single char is 1 byte (8 bit)
e = BA("0x1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
self.assertEqual("Cooking MC's like a pound of bacon", find_best_message(e))
key = find_best_key(e)
d = BA(hex(ord(key)) * len(e.bytes))
self.assertEqual("Cooking MC's like a pound of bacon", to_str(e ^ d))