def test_setitem(self): result = ByteData(b'\x00\x01\x02\x03\x04\x05\x06\x07') byte = ByteData(b'\x00\x00\x00\x03\x04\x05\x06\x07') byte[1] = 1 byte[2] = b'\x02' self.assertEqual(result, byte)
def test_pkcs7_padding_remove_correct(self): result = ByteData(b'some text') data = ByteData(b'some text\x06\x06\x06\x06\x06\x06') padded_data = data.pkcs7_pad_remove() self.assertEqual(result, padded_data)
def test_pkcs7_padding_remove_wrong_length(self): result = "Data does not have valid pkcs7 padding: b't\x07\x07\x07\x07\x07\x07'" data = ByteData(b'some text\x07\x07\x07\x07\x07\x07') with pytest.raises(Exception) as info: assert (data.pkcs7_pad_remove()) self.assertEqual(result, info)
def test_xor_multiple_bytes(self): result = ByteData(b'\x04' * 16) byte1 = ByteData(b'\x04' * 16) byte2 = ByteData(b'\x00' * 16) xor = byte1 ^ byte2 self.assertEqual(result, xor)
def test_hamming_distance(self): result = 37 text1 = ByteData('this is a test', UTF8Converter()) text2 = ByteData('wokka wokka!!!', UTF8Converter()) edit_distance = text1.hamming_distance(text2) self.assertEqual(result, edit_distance)
def test_pkcs7_padding(self): result = ByteData(b'some text\x06\x06\x06\x06\x06\x06') data = ByteData(b'some text') block_size = 15 padded_data = data.pkcs7_pad(block_size) self.assertEqual(result, padded_data)
def test_repeating_key_xor_encrypt(self): result = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20' key = ByteData('ICE', UTF8Converter()) text = ByteData("Burning 'em, if you ain't quick and nimble", UTF8Converter()) cipher = text.repeating_key_xor(key) cipher_hex = cipher.encode(HexConverter()) self.assertEqual(result, cipher_hex)
def test_xor_sigle_byte(self): result = ByteData(bytes([0xbc])) byte1 = ByteData(bytes([0x16])) byte2 = ByteData(bytes([0xaa])) xor = byte1 ^ byte2 self.assertEqual(result, xor)
def test_one_byte_xor(self): result = ByteData(bytes([0xbc, 0xbc, 0xbc])) byte = ByteData(bytes([0x16])) byte_data = ByteData(bytes([0xaa, 0xaa, 0xaa])) xor = byte_data.repeating_key_xor(byte) self.assertEqual(result, xor)
def test_repeating_key_xor_decrypt(self): result = "Burning 'em, if you ain't quick and nimble" key = ByteData('ICE', UTF8Converter()) cipher = ByteData( '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20', HexConverter()) byte_text = cipher.repeating_key_xor(key) text = byte_text.encode(UTF8Converter()) self.assertEqual(result, text)
def test_breaking_repeating_key_xor(self): result = ByteData('Terminator X: Bring the noise', UTF8Converter()) with open('files/6.txt') as f: base64_cipher_text = f.read().replace('\n', '') cipher_data = ByteData(base64_cipher_text, Base64Converter()) data = RepeatingXor(cipher_data, EnglishScore()) key = data.break_multiple_byte_key() self.assertEqual(result, key)
def test_fixed_xor(self): result = '746865206b696420646f6e277420706c6179' hex_string1 = '1c0111001f010100061a024b53535009181c' hex_string2 = '686974207468652062756c6c277320657965' data1 = ByteData(hex_string1, HexConverter()) data2 = ByteData(hex_string2, HexConverter()) data_xor = data1 ^ data2 hex_xor = data_xor.encode(HexConverter()) self.assertEqual(result, hex_xor)
def test_implement_repeating_key_xor(self): result = ByteData( '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a2622632427276527' + '2a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f', HexConverter()) key = ByteData('ICE', UTF8Converter()) text = ByteData( "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal", UTF8Converter()) cipher = text.repeating_key_xor(key) self.assertEqual(result, cipher)
def test_aes_in_ecb_mode(self): with open('files/7_result.txt') as f: result = f.read() with open('files/7.txt') as f: base64_cipher_text = f.read().replace('\n', '') key = ByteData('YELLOW SUBMARINE', UTF8Converter()) data = ByteData(base64_cipher_text, Base64Converter()) cipher = AesECB(data) cleartext = cipher.decrypt(key) self.assertEqual(result, cleartext.encode(UTF8Converter()))
def test_single_byte_xor_chiper(self): result = ByteData("Cooking MC's like a pound of bacon", UTF8Converter()) result_key = ByteData('X', UTF8Converter()) data = ByteData( '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736', HexConverter()) repeating_xor = RepeatingXor(data, EnglishScore()) key = repeating_xor.break_one_byte_key() best_string = data.repeating_key_xor(key) self.assertEqual(result, best_string) self.assertEqual(result_key, key)
def test_detect_aes_in_ecb_mode(self): result = 'd880619740a8a19b7840a8a31c810a3d08649af70dc06f4fd5d2d69c744cd283e2dd052f6b641dbf' +\ '9d11b0348542bb5708649af70dc06f4fd5d2d69c744cd2839475c9dfdbc1d46597949d9c7e82bf5a' +\ '08649af70dc06f4fd5d2d69c744cd28397a93eab8d6aecd566489154789a6b0308649af70dc06f4f' +\ 'd5d2d69c744cd283d403180c98c8f6db1f2a3f9c4040deb0ab51b29933f2c123c58386b06fba186a' key_size = 16 with open('files/8.txt') as f: ciphers = f.read().split('\n')[:-1] for cipher in ciphers: data = ByteData(cipher, HexConverter()) cipher_data = AesECB(data) if cipher_data.verify_ecb_mode(key_size): ecb_cipher = data.encode(HexConverter()) break self.assertEqual(result, ecb_cipher)
def test_detect_single_character_xor(self): result = ByteData("Now that the party is jumping\n", UTF8Converter()) result_key = ByteData('5', UTF8Converter()) with open('files/4.txt') as f: score = 10000 best_string = ByteData() for line in f: data = ByteData(line.rstrip('\n'), HexConverter()) cipher = RepeatingXor(data, EnglishScore()) temp_key = cipher.break_one_byte_key() string = data.repeating_key_xor(temp_key) temp_score = cipher.score if temp_score > score: continue score = temp_score best_string = string key = temp_key self.assertEqual(result, best_string) self.assertEqual(result_key, key)
#!/usr/bin/env python3 from crypto_tools import ByteData from crypto_tools import UTF8Converter from crypto_tools import Base64Converter from crypto_tools import CTRFixedNonce from crypto_tools import RepeatingXor from crypto_tools import EnglishScore if __name__ == '__main__': backend = CTRFixedNonce() ciphers = backend.encrypt() lengths = list() for cipher in ciphers: lengths.append(len(cipher)) concatinated_cipher = ByteData() for cipher in ciphers: concatinated_cipher += cipher[:min(lengths)] xor = RepeatingXor(concatinated_cipher, EnglishScore()) xor.min_key_size = min(lengths) xor.max_key_size = min(lengths) + 1 key = xor.break_multiple_byte_key() data = concatinated_cipher.repeating_key_xor(key) for num in range(0, len(concatinated_cipher), min(lengths)): print(data[num:num + min(lengths)].get_data())
def test_getitem(self): result = ByteData(b'\x42') byte = ByteData(b'\x00\x00\x00\x00\x42\x00\x00\x00') self.assertEqual(result, byte[4])