def aes_cbc_decrypt(ciphertext, key, iv): blocks = [ciphertext[i:i + 16] for i in range(0, len(ciphertext), 16)] plaintext = b"" for block in blocks: deciphered = aes_ecb_decrypt(block, key) plain_block = xor_repeating_key(deciphered, iv) iv = block plaintext += plain_block return plaintext
def aes_cbc_encrypt(plaintext, key, iv): blocks = [plaintext[i:i + 16] for i in range(0, len(plaintext), 16)] ciphertext = b"" for block in blocks: xored_block = xor_repeating_key(block, iv) ciphered = aes_ecb_encrypt(xored_block, key) iv = ciphered ciphertext += ciphered return ciphertext
def decode_hex(inputStr): bestStr = "" bestScore = 0 thisStr = xor_repeating_key(inputStr) if score(thisStr) > bestScore: bestScore = score(thisStr) bestStr = thisStr return "Plaintext: {}".format(bestStr)
#!/usr/bin/env python3 import sys import binascii sys.path.append('..') from helpers import abort from xor import xor_repeating_key if __name__ == "__main__": if len(sys.argv) == 3: with open(sys.argv[1]) as input_file: line = input_file.readline().rstrip() print( str( binascii.hexlify( xor_repeating_key(line, sys.argv[2].encode())), 'ascii')) else: abort(f'{sys.argv[0]}: filename key')
from xor import xor_repeating_key msg = """Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal""".encode('ascii') key = "ICE".encode('ascii') xored = xor_repeating_key(msg, key) print(xored.hex()) expected = ("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a262263" "24272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b2028" "3165286326302e27282f") assert(xored.hex() == expected)
from distance import hamming from base64 import b64decode from xor import (xor_single_char_key, xor_repeating_key, break_xor_repeating_key, guess_key_lengths) from frequency import english_test with open("06.txt") as f: lines = f.readlines() cipher = b64decode(''.join(lines)) key_length = guess_key_lengths(cipher)[0] print("guessed key length: %i" % key_length) key = break_xor_repeating_key(cipher, key_length) print("key: %s" % key.decode('ascii')) decrypted = xor_repeating_key(cipher, key) message = decrypted.decode('ascii') print(message) assert(message.startswith("I'm back and I'm ringin' the bell"))