在使用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.