gpt4 book ai didi

python - 使用 pyaes 进行 AES 加密时,明文 block 必须为 16 字节错误

转载 作者:行者123 更新时间:2023-12-01 00:41:26 31 4
gpt4 key购买 nike

我正在尝试使用 pyaes 进行 AES 加密。

我的下面的代码运行完美。

text = 'Hello world !!!!'
encrypter = pyaes.AESModeOfOperationCBC('my_test_key_0001', 'my_test_vec_0001')
encrypted = base64.b64encode(encrypter.encrypt(text))
print(encrypted)

但是,当我将文本值更改为

text = 'rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAI3z7LN2KbyKE='

返回错误。

Traceback (most recent call last):
File "/home/path/cryptolib/test.py", line 54, in <module>
encrypted = base64.b64encode(encrypter.encrypt(text))
File "/home/path/pyaes/aes.py", line 389, in encrypt
raise ValueError('plaintext block must be 16 bytes')
ValueError: plaintext block must be 16 bytes

我不是 AES 方面的专家,所以可能缺少基础知识。

我无法使用 pycrypto,因为我正在为 redshift 开发 UDF,并且根据我的发现,那里不支持 pycrypto

最佳答案

pyaes#AESModeOfOperationCBC 只允许加密恰好一个 block (16 字节)长的文本。对于较长的文本,必须使用 BlockFeeder:

import pyaes, base64

#Encryption
text = 'rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAI3z7LN2KbyKE='
encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCBC('my_test_key_0001', 'my_test_vec_0001'))
ciphertext = encrypter.feed(text)
ciphertext += encrypter.feed()
ciphertext = base64.b64encode(ciphertext)

#Decryption
decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCBC('my_test_key_0001', 'my_test_vec_0001'))
decrypted = decrypter.feed(base64.b64decode(ciphertext))
decrypted += decrypter.feed()
print('>' + decrypted + '<\n')

BlockFeeder 还会自动执行 padding 。填充是将数据添加到消息末尾,直到长度对应于 block 长度的整数倍(通常很重要,但与您的示例无关,因为已经满足长度条件)。

编辑:

Encrypter#feed(<plaindata>) 缓冲明文,对除最后一个 block (如果最后一个 block 完整)或最后两个 block (如果最后一个 block 不完整)之外的数据进行加密,并返回加密后的数据。最后的 Encrypter#feed() 调用表示明文结束,触发剩余部分的填充和加密,并返回加密数据。这可以是用下面的代码片段来说明:

import pyaes

#Encryption
encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCBC('my_test_key_0001', 'my_test_vec_0001'))
ciphertext = encrypter.feed ('0123456789ABCDEF') # 1. block buffered, ciphertext = ''
ciphertext += encrypter.feed('0123456789ABCDE') # 1. and incomplete 2. block buffered, ciphertext += ''
print("Current length of ciphertext: " + str(len(ciphertext)) + " Bytes\n")
ciphertext += encrypter.feed('F') # 1. block flushed, 2. block buffered, ciphertext += '<encrypted 1. block>'
print("Current length of ciphertext: " + str(len(ciphertext)) + " Bytes\n")
ciphertext += encrypter.feed('0123456789ABCDEF') # 2. block flushed, 3. block buffered, ciphertext += '<encrypted 2. block>'
print("Current length of ciphertext: " + str(len(ciphertext)) + " Bytes\n")
ciphertext += encrypter.feed('0123456') # 3. and incomplete 4. block buffered, ciphertext += ''
print("Current length of ciphertext: " + str(len(ciphertext)) + " Bytes\n")
ciphertext += encrypter.feed() # 3. and padded 4. block flushed, ciphertext += '<encrypted 3. and 4. padded block >'
print("Current length of ciphertext: " + str(len(ciphertext)) + " Bytes\n")

#Decryption
decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCBC('my_test_key_0001', 'my_test_vec_0001'))
decrypted = decrypter.feed(ciphertext)
decrypted += decrypter.feed()
print('>' + decrypted + '<\n')

在该示例中,最后一个纯文本 block 不完整。如果纯文本的最后一个 block 已经完成,则会填充一个附加的完整 block 。这里使用的填充是 PKCS7

关于python - 使用 pyaes 进行 AES 加密时,明文 block 必须为 16 字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320303/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com