-
Notifications
You must be signed in to change notification settings - Fork 4
/
p03.py
39 lines (24 loc) · 906 Bytes
/
p03.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
32
33
34
35
36
37
38
39
from binascii import unhexlify
from operator import itemgetter
from main import Solution
def char_freq(chars: bytes) -> int:
freq = 0
most_freq_letters = b'etaoinhs'
for c in chars:
if c in most_freq_letters:
freq += 1
return freq
def get_single_byte_key(ctxt: bytes) -> int:
key_to_char_count = {}
for key in range(0xff + 1):
chars = bytes([(c ^ key) for c in ctxt])
key_to_char_count[key] = char_freq(chars)
return max(key_to_char_count.items(), key=itemgetter(1))[0]
def single_byte_cipher(ctxt: bytes) -> bytes:
key = get_single_byte_key(ctxt)
return bytes(map(lambda c: key ^ c, ctxt))
def p03() -> bytes:
ctxt = unhexlify(b'1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736')
return single_byte_cipher(ctxt)
def main() -> Solution:
return Solution('3: Single-byte XOR cipher', p03)