about summary refs log tree commit diff
path: root/users/wpcarro/scratch/cryptopals/set1/c3.py
blob: 187b58c1c70ac05aebd97b787b6b8d013b5a1944 (plain) (blame)
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
from c2 import fixed_xor
from collections import Counter
import string

alphabet = string.ascii_lowercase + string.ascii_uppercase

def score(bs):
    chars = [b for b in bs if b in alphabet]
    return sum(Counter(chars).values())

def decode_cipher(x):
    x = bytearray.fromhex(x)
    num_bytes = len(x)

    mx, result, key = 0, None, None
    for c in alphabet:
        mask = bytearray(bytes(c, 'ascii') * num_bytes)
        y = fixed_xor(x, mask, decode_hex=False, encode_hex=False).decode('ascii')
        test = score(y)
        if test > mx:
            result = y
            mx = test
            key = mask.decode('ascii')
    return result

run_tests = False
if run_tests:
    print(decode_cipher("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"))