def test_challenge_6(self): """ Challenge 6: Break repeating key XOR """ file = open('files/6.txt', 'r') input = file.read() file.close() input_bytes = conv.base_64_to_bytes(input) key, output = xortools.breakxor(input_bytes) self.assertEqual(key, b'Terminator X: Bring the noise')
def test_challenge_18(self): """ Challenge 18: CTR Encryption """ test = 'L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==' key = "YELLOW SUBMARINE" nonce = bytes(8) self.assertEqual(b"Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby ", aestools.do_ctr(conv.base_64_to_bytes(test), key, nonce)) roundtrip_input = conv.base_64_to_bytes(aestools.TEXT) roundtrip_encrypt = aestools.do_ctr(roundtrip_input, key, nonce) self.assertEqual(roundtrip_input, aestools.do_ctr(roundtrip_encrypt, key, nonce))
def test_challenge_17(self): """ Challenge 17: CBC padding oracle """ input_file = open('files/17.txt', 'r') lines = [conv.base_64_to_bytes(line.rstrip()) for line in input_file] input_file.close() cipher, iv = aestools.provide_cbc_ecrypted() self.assertTrue(aestools.is_valid_padding(cipher, iv)) cracked = aestools.break_cbc_using_padding(aestools.is_valid_padding, cipher, iv) self.assertTrue(cracked in lines)
def test_challenge_20(self): """ Challenge 20: Break fixed-nonce CTR Statistically """ key = aestools.random_key(16) nonce = bytes(8) input_file = open('files/20.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] min_length = min([len(line) for line in encrypted_lines]) concatted = b''.join([line[0:min_length] for line in encrypted_lines]) test = xortools.breakxor(concatted, min_length)
def test_challenge_7(self): """ Challenge 7: Decrypt AES """ in_file = open("files/7.txt", "r") input = conv.base_64_to_bytes(in_file.read()) in_file.close() output_bytes = aestools.decrypt_ecb(input, "YELLOW SUBMARINE", True) output = output_bytes.decode("utf-8") expected_file = open('files/7_expected.txt', 'r') self.assertEqual(output, expected_file.read()) expected_file.close()
def test_challenge_10(self): """ Challenge 10: Decrypt CBC """ file = open('files/10.txt', 'r') input = conv.base_64_to_bytes(file.read().rstrip()) file.close() expected_file = open('files/10_expected.txt', 'r') expected_bytes = expected_file.read().encode("utf-8") expected_file.close() decrypted = aestools.decrypt_cbc(input, bytes("YELLOW SUBMARINE", "ascii"), b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') self.assertEqual(decrypted, expected_bytes)
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])
def provide_cbc_ecrypted(): input_file = open('files/17.txt', 'r') lines = [line.rstrip() for line in input_file] input_file.close() line = lines[random.randint(0, len(lines) - 1)] return (encrypt_cbc(conv.base_64_to_bytes(line), BLACK_BOX_KEY, CBC_IV), CBC_IV)
def black_box(input, prefix=False): combined = bytearray() if(prefix): combined.extend(PREFIX) combined.extend(input) combined.extend(conv.base_64_to_bytes(TEXT)) return encrypt_ecb(bytes(combined), BLACK_BOX_KEY, True)
def test_challenge_14(self): """ Challenge 14: Decrypt ECB 1 byte at a time HARD """ result = aestools.break_ECB_1_byte(aestools.get_black_box(True)) self.assertEqual(result, conv.base_64_to_bytes(aestools.TEXT))