我正在将tkinter用于GUI,io和pyAesCrypt,用于将字符串转换为加密字符串并将其解密为原始字符串。 每当我第二次尝试发送输入时,它都会对其进行加密,但无法解密。
from tkinter import *
import pyAesCrypt
import io
import tkinter as tk
#import requests
bufferSize = 64 * 1024
password = "foopassword"
global fCiph,fDec
# initialize ciphertext binary stream
fCiph = io.BytesIO()
# initialize decrypted binary stream
fDec = io.BytesIO()
# encrypt stream
def EncryptFunc(entry):
output1.delete(0.0,END)
pbdata = bytes(entry,encoding= 'utf-8')
fIn = io.BytesIO(pbdata)
pyAesCrypt.encryptStream(fIn,fCiph,password,bufferSize)
# print encrypted data
print("This is the ciphertext:\n" + str(fCiph.getvalue()))
output_of_Cipher = str(fCiph.getvalue())
#label['text'] = output_of_Cipher
with open('CipherText.txt','w') as f:
f.write(output_of_Cipher)
f.close()
output1.insert(END,output_of_Cipher)
#calling the encryption function do this inside tkinter button
#EncryptFunc(entry)
def DecryptFunc():
output.delete(0.0,END)
fDec = io.BytesIO()
# get ciphertext length
ctlen = len(fCiph.getvalue())
print(ctlen)
# go back to the start of the ciphertext stream
fCiph.seek(0)
# decrypt stream
pyAesCrypt.decryptStream(fCiph,fDec,bufferSize,ctlen)
# print decrypted data
output_of_Decipher = str(fDec.getvalue(),'utf-8')
print("Decrypted data:\n" + str(fDec.getvalue(),'utf-8'))
output.insert(END,output_of_Decipher)
#calling DecryptFunc function do this inside tkinter button
#DecryptFunc()
#_____________________GUI_______________________
window = Tk()
window.title("AES algo")
window.configure(background="black")
Label(window,text="Enter the text",bg="black",fg="white",font="none 12 bold").grid(row=0,column=0,stick='w')
textentry = Entry(window,width=20,bg='white')
textentry.grid(row=2,sticky='w')
Button(window,text="Encrypt",width=6,command=lambda: EncryptFunc(textentry.get())).grid(row=2,column=3,sticky='e')
Label(window,text="\nOutput",font="none 12 bold").grid(row=4,stick='w')
output1 = Text(window,width=75,height=6,wrap=WORD,background="White")
output1.grid(row=5,columnspan=2,stick='w')
#_________________________for decrypted_________________#
Button(window,text="Decrypt",command=lambda: DecryptFunc()).grid(row=8,font="none 12 bold").grid(row=11,stick='w')
output = Text(window,background="White")
output.grid(row=14,stick='w')
window.mainloop()
错误是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Vinod Dali\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py",line 1705,in __call__
return self.func(*args)
File "E:\Vinod Dali\Desktop\CnsMiniProject\CNSmini1.py",line 71,in <lambda>
Button(window,sticky='e')
File "E:\Vinod Dali\Desktop\CnsMiniProject\CNSmini1.py",line 45,in DecryptFunc
pyAesCrypt.decryptStream(fCiph,ctlen)
File "C:\Users\Vinod Dali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyAesCrypt\crypto.py",line 425,in decryptStream
raise ValueError("File is corrupted.")
ValueError: File is corrupted.
[Finished in 81.4s]
是否有可能使它从头开始运行,就像关闭tkinter对话框并再次编译脚本一样, 由于它只能运行一次加密和解密,但是在调用解密函数时会引发异常