我在python3中创建了这个软件。问题是如果我想要解密文件,我的软件不会写结果。 我使用easygui作为gui的gui。最后,我没有任何错误消息。该软件通常关闭,但文件保持加密状态。
def decrypt(key, filename):
chunksize = 64 * 1024
outputFile = filename[11:]
with open(filename, 'rb') as infile:
filesize = int(infile.read(16))
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
with open(outputFile, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(filesize)
def getKey(password):
hasher = SHA256.new(password.encode('utf-8'))
return hasher.digest()
def Main():
image = "./images/encryption.gif"
msg = "Do you want to encrypt or decrypt a file ?"
choices = ["Encrypt", "Decrypt", "Exit"]
reply = buttonbox(msg, image=image, choices=choices)
if reply == 'Encrypt':
filename = fileopenbox(msg="Select the file to Encrypt",title="Select the file to Encrypt", default='*', filetypes=None, multiple=False)
password = passwordbox(msg="Enter a password",title="Enter a password",default="")
encrypt(getKey(password),filename)
elif reply == 'Decrypt':
filename = fileopenbox(msg="Select the file to Decrypt", title="Select the file to Decrypt", default='*', filetypes=None, multiple=False)
password = passwordbox(msg="Enter a password",title="Enter a password",default="")
decrypt(getKey(password),filename)
else:
sys.exit(0)
if __name__ == '__main__':
Main()
答案 0 :(得分:1)
您忘记考虑AESCipher.encrypt
and AESCipher.decrypt
的要求:您需要自己执行必要的填充:
对于MODE_ECB,MODE_CBC和MODE_OFB,明文长度(以字节为单位)必须是block_size的倍数。
如果由于消息的某些部分保留在缓存中而未提供正好是16字节的倍数的消息(文件内容),则可能会遗漏部分密文。因此,每次执行加密时,可能会从输入文件中删除0到15个字节。
显然,对于最多15个字节的非常小的文件,不会留下任何东西。