Python实现AES加密解密

报错:

TypeError: decrypt() cannot be called after encrypt()

原因:

cryptor不能写在主函数中同时给加密函数与解密函数使用,尽管加密解密都是用: cryptor = AES.new(key, mode, iv),

但若将其定义在main并分别传给加密解密同时使用时,会报错。

解决:

在加密与解密方法分别写cryptor = AES.new(key, mode, iv)

报错:

ValueError: Incorrect IV length (it must be 16 bytes long)

原因:

偏移量必须16位

报错:

ValueError: Incorrect AES key length

原因:

密钥长度需为16的倍数

注意:

需加密的内容必须大于密钥长度且为16倍数

实现:

from binascii import b2a_hex, a2b_hex
from Crypto.Cipher import AES

# 密钥在使用该方法时可补足为16倍数
# 偏移量在使用该方法时,在不足16位时补为16位
# 偏移量必须为16位,不可多
def legth(value):
    l = len(value)
    flag = l % 16
    if flag != 0:
        add = 16 - (l % 16)
        value = value + ('\0' * add).encode('utf-8')
    return value


def encryp_str(content, key, mode, iv):
    cryptor = AES.new(key, mode, iv)
    # 被加密内容需大于密钥长度,且为16倍数
    key_length = len(key)
    content_legth = len(content)
    if content_legth < key_length:
        add = key_length - content_legth
        content = content + ('\0' * add).encode('utf-8')
    elif content_legth > key_length:
        add = 16 - (content_legth % 16)
        content = content + ('\0' * add).encode('utf-8')
    cipher_content = cryptor.encrypt(content)  # 加密
    print('加密1:', cipher_content)
    cipher_content_hex = b2a_hex(cipher_content)
    print('加密2:', cipher_content_hex)
    cipher_content_hex_de = cipher_content_hex.decode()
    print('密文:', cipher_content_hex_de)
    return cipher_content_hex_de


def decryp_str(en_content, key, mode, iv):
    cryptor = AES.new(key, mode, iv)
    content = a2b_hex(en_content)
    print('解密1:', content)
    content = cryptor.decrypt(content)
    print('解密2:', content)
    content = bytes.decode(content).rstrip('\0')
    print('明文:', content)
    return content


if __name__ == '__main__':
    # 加密内容
    content = input('输入加密内容:')
    # 密钥
    key = input('输入密钥:')
    key = key.encode('utf-8')  # 密钥需编码
    # 偏移量
    iv = input(r'输入偏移量(16位),少于16位将使用“\0”自动补齐:')
    print('原文:', content)
    content = content.encode('utf-8')  # 编码加密内容
    iv = iv.encode('utf-8')
    # 处理key和iv长度
    key = legth(key)
    iv = legth(iv)

    mode = AES.MODE_CBC  # 加密模式

    en_content = encryp_str(content, key, mode, iv)
    content = decryp_str(en_content, key, mode, iv)

效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值