def rand_encrypt(key, secret): block_size = 16 secret = pad(pad_rand(secret), block_size) if randint(0, 1): return encrypt_ECB(key, secret) else: return encrypt_CBC(key, secret)
def encrypt_CBC(key, text): block_size = 16 text_blocks = generate_blocks(pad(text), block_size) initialisation_vector = b'\x00' * block_size cipher_blocks = [initialisation_vector] for i in range(len(text_blocks)): xor = fixed_xor(cipher_blocks[i], text_blocks[i]) cipher_blocks.append(encrypt_ECB(key, xor, add_padding=False)) return "".join(cipher_blocks[1:])
def encrypt_ECB(key, text, add_padding=True): cipher = AES.AESCipher(key, AES.MODE_ECB) if add_padding: text = pad(text, block_size = len(key)) ciphertext = cipher.encrypt(text) return ciphertext