def test_challenge_3(self): """ Challenge 3: Find xor cipher byte """ input = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' input_bytes = conv.hex_to_bytes(input) b, output, score = xortools.solve_xor_block(input_bytes) self.assertEqual(b, 88) self.assertEqual(output, b"Cooking MC's like a pound of bacon")
def test_challenge_19(self): """ Challenge 19: Break fixed-nonce CTR """ key = aestools.random_key(16) nonce = bytes(8) input_file = open('files/19.txt', 'r') lines = [conv.base_64_to_bytes(line.rstrip()) for line in input_file] input_file.close() encrypted_lines = [aestools.do_ctr(line, key, nonce) for line in lines] index = 0 probable_bytes = bytearray() while(True): rotated = "".join([chr(line[index]) if index < len(line) else '' for line in encrypted_lines]) b, all, score = xortools.solve_xor_block(bytes(rotated, 'utf-8')) probable_bytes.append(b) index += 1 if len(rotated) == 0: break for line in encrypted_lines: close = xortools.xor_bytes(line, bytes(probable_bytes[0 : len(line)])) readable = " ".join([chr(b) if b in range(32, 127) else 'X' for b in close])