def RandomEncrypt(plaintext):
"""Returns a tuple with the encrypted text and the mode used."""
# Random key.
key = Random.new().read(AES.block_size)
# Random padding both before and after the plaintext. The
# size of the second padding cannot be random since the result
# needs to have a number of bytes multiple of 16.
paddingSize = random.randint(5, 10)
prepend = Random.new().read(paddingSize)
append = Random.new().read(AES.block_size - paddingSize)
# Pick encryption mode at random.
mode = None
if random.randint(0, 1) == 0:
mode = AES.MODE_ECB
else:
mode = AES.MODE_CBC
# Perform the encryption.
aes = aes_lib.AESCipher(key, mode=mode)
text = prepend + plaintext + append
return (aes.aes_encrypt(text), mode)
评论列表
文章目录