【发布时间】:2019-05-13 05:54:33
【问题描述】:
我在一个文件中有一条加密消息,由以下代码加密。 我写了一个函数来解密这条消息。我知道用来加密它的密码。
但我收到以下错误:
python3 decrypt.py enim_msg.txt
Traceback (most recent call last):
File "decrypt.py", line 45, in <module>
print(":: Decrypted: \n" + bytes.decode(decrypted))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 2: invalid start byte
请问我该如何解决这个问题? 我的解密功能有问题吗?
我的代码:
加密函数
import os
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
def encrypt(key, filename):
chunksize = 64*1024
outputFile = "en" + filename
filesize = str(os.path.getsize(filename)).zfill(16)
IV = Random.new().read(16)
encryptor = AES.new(key, AES.MODE_CBC, IV)
with open(filename, 'rb') as infile:
with open(outputFile, 'wb') as outfile:
outfile.write(filesize.encode('utf-8'))
outfile.write(IV)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' ' * (16 - (len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))
def getKey(password):
hasher = SHA256.new(password.encode('utf-8'))
return hasher.digest()
我写的解密函数
def decrypt(enc, password):
#print(":: enc => " + enc)
private_key = hashlib.sha256(password.encode("utf-8")).digest()
iv = enc[:16]
cipher = AES.new(private_key, AES.MODE_CBC, iv)
return cipher.decrypt(enc[16:])
我如何调用这个函数
password = "azerty123"
secret_file_path = sys.argv[1]
the_file = open(secret_file_path, "rb")
encrypted = the_file.read()
decrypted = decrypt(encrypted, password)
the_file.close()
print(":: Decrypted: \n" + bytes.decode(decrypted))
【问题讨论】:
-
我不太了解 Python - 但我知道在加密方面将任何内容存储为字符串是一个坏主意。加密数据、密钥等本质上都是二进制数据——你也应该以这种方式存储它们。您不能将任意二进制文件转换为字符串并返回。
-
哇,我见过很多“不平衡”的加密/解密函数,但这个占了上风。作为开发人员,您怎么能认为这会起作用?您的加密/解密功能应该反过来做同样的事情。为什么
filesize是一个字符串? -
哦,我明白了,它只是一个 16 字节/字符的十进制字符串,这只是次优的。玉。我看不到您在解密期间处理此问题的任何地方。块可能用空格填充的方式是绝对错误的,没有完全检索到的块可能会让这个功能失败,例如当缓冲区大小不是块大小的倍数时。加密只是一个等待发生的错误。
标签: python encryption aes sha256