在Python中,使用Cryptography.Hazmat模块的AES,加密的输出长度不是16的倍数;我是否实现了加密密码错误,如果是,有什么问题吗?我收到的输出长度是16 + len(input) (16,因为它是IV的长度)。以下代码如下:
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.ciphers.modes import CBC,OFB,CFB
class AES_Cipher:
def __init__(self,key):
self.key = key
def encrypt(self,plain_text):
initialization_vector = urandom(16)
cipher = Cipher(AES(self.key),OFB(initialization_vector),backend)
encryption_engine = cipher.encryptor()
return initialization_vector + encryption_engine.update(plain_text.encode("utf-8")) + encryption_engine.finalize()
def decrypt(self,cipher_text):
initialization_vector = cipher_text[:16]
cipher = Cipher(AES(self.key),OFB(initialization_vector),backend)
decryption_engine = cipher.decryptor()
return (decryption_engine.update(cipher_text[16:]) + decryption_engine.finalize()).decode("utf-8")密码的名称如下:
from hashlib import sha3_256
aes_key = sha3_256(b"Strong Encryption Key").digest()
aes_engine = AES_Cipher(aes_key)
aes_engine.encrypt("Hello World")这就是结果:
b'\xc4I\xf2\xe5\xf4\xaeX\x96\xa5\xfe\xbd+\xde\x8ca\xd5\xdb\xad\x97S\x01\x81C\x9e\xd5\xd8@'
这只有27字节长,而预期的32字节。27 = 16 +len(“”)。为什么它不是32字节长?代码丢失了什么?另一件事,解密工作得很好。
https://stackoverflow.com/questions/58049057
复制相似问题