def chal6(): """ The text in input/6.txt has been base64 encoded after being encrypted with repeating-key XOR. Decrypt it. """ f = open('input/6.txt') ciphertext = utils.base64_to_bytes(f.read()) best_english_score = 0 result = '' keysizes = utils.find_possible_keysizes(ciphertext, 3, 2, 40) for keysize in keysizes: key = [0] * keysize # Split the ciphertext into blocks of length keysize blocks = [ciphertext[i:i+keysize] for i in range(0, len(ciphertext), keysize)] # Transpose the blocks transposed = [bytes(t) for t in zip_longest(*blocks, fillvalue=0)] # Treat each block as if it's been encrypted with a # repeating-key XOR for i in range(0, len(transposed)-1): key[i] = utils.get_popular_byte(transposed[i]) plaintext = _crypto.xor_repeating_key(ciphertext, bytes(key)) english_score = utils.english_score(plaintext.decode('utf-8')) if english_score > best_english_score: result = plaintext.decode('utf-8')
def chal5(): """Encrypt the input under the key 'ICE' using repeating-key XOR.""" IN = "Burning 'em, if you ain't quick and nimble\nI go crazy" \ " when I hear a cymbal" OUT = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a2622' \ '6324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c69' \ '2b20283165286326302e27282f' KEY = 'ICE' ciphertext = _crypto.xor_repeating_key(bytes(IN, 'utf-8'), bytes(KEY, 'utf-8')) result = utils.bytes_to_hex(ciphertext) expect(result, OUT)