ch11.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:matasano 作者: shainer 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号