pythonAES加密中的一些报错

在使用AES加密中,出现报错

from Crypto.Cipher import AES
import base64
cipher = "u0uYYmh4yRpPIT/zSP7EL/MOCliVoVLt3gHcrXDymLc="
cipher_str = base64.b64decode(cipher)
key_str = "n1book"
key_str = (key_str.ljust(32, " "))
iv_str = "123456"
iv_str = (iv_str.ljust(16, " "))
aes = AES.new(key_str, AES.MODE_CBC, iv_str)
print(aes.decrypt(cipher_str))

报错如下
在这里插入图片描述
修改如下,在对应字符前加入.encode(‘utf-8’)即可

from Crypto.Cipher import AES
import base64
cipher = "u0uYYmh4yRpPIT/zSP7EL/MOCliVoVLt3gHcrXDymLc="
cipher_str = base64.b64decode(cipher)
key_str = "n1book"
key_str = (key_str.ljust(32, " ")).encode('utf-8')
iv_str = "123456"
iv_str = (iv_str.ljust(16, " ")).encode('utf-8')
aes = AES.new(key_str, AES.MODE_CBC, iv_str)
print(aes.decrypt(cipher_str))

在Vscode中还会有警告
在这里插入图片描述
提示应该使用安全模式和填充方案。
Encryption algorithms should be used with secure mode and padding scheme (python:S5542)
Encryption operation mode and the padding scheme should be chosen appropriately to guarantee data confidentiality, integrity and authenticity:

For block cipher encryption algorithms (like AES):
The GCM (Galois Counter Mode) mode which works internally with zero/no padding scheme, is recommended, as it is designed to provide both data authenticity (integrity) and confidentiality. Other similar modes are CCM, CWC, EAX, IAPM and OCB.
The CBC (Cipher Block Chaining) mode by itself provides only data confidentiality, it’s recommended to use it along with Message Authentication Code or similar to achieve data authenticity (integrity) too and thus to prevent padding oracle attacks.
The ECB (Electronic Codebook) mode doesn’t provide serious message confidentiality: under a given key any given plaintext block always gets encrypted to the same ciphertext block. This mode should not be used.
For RSA encryption algorithm, the recommended padding scheme is OAEP.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值