我的程序有问题,它所做的一切都是基于密钥对文本进行加密和解密。但是,当我尝试对加密的单词进行解密时,它只会显示错误
raise InvalidToken cryptography.fernet.InvalidToken
这是代码
#Import Libraries
from cryptography.fernet import Fernet
from tkinter import *
import base64
def Encrypt(text_f):
f = Fernet(b'eY_snWFGBTxC55GsmloucJhPtiLt_3bANhHnikOlXFQ=')
print(f.encrypt((str(text_f).encode())))
def Decrypt(text_f):
f = Fernet(b'eY_snWFGBTxC55GsmloucJhPtiLt_3bANhHnikOlXFQ=')
print(f.decrypt((bytes(text_f).encode())))
#Set Window
root = Tk()
#Define Elements
text_user = ""
instruction_1 = Label(root, text="Input Text")
text_input = Entry(root, textvariable=text_user)
button_encode = Button(root, text='Encode', command = lambda : Encrypt(str(text_user.encode())))
button_decode = Button(root, text='Decode', command = lambda : Decrypt(str(text_user.encode())))
text_description = Label(root, text="")
#Pack Elements
instruction_1.pack(ipady = 10, ipadx = 5)
text_input.pack(ipady = 5, ipadx = 4)
button_encode.pack(ipady = 3, ipadx = 12)
button_decode.pack(ipady = 3, ipadx = 12)
#Setup Window Properties
root.geometry('800x650')
root.title("APEP | Encoder & Decoder")
#Loop Window Runtime
root.mainloop()
发布于 2020-04-30 23:40:12
我没有你提供的相同的代码。我想你已经向加密/解密函数传递了无效值。
我添加了一些更改:
#Import Libraries
from functools import partial
from cryptography.fernet import Fernet
from tkinter import *
import base64
key = Fernet.generate_key()
f = Fernet(key)
def Encrypt(text_f: Entry):
encrypted = f.encrypt(bytes(text_f.get(), 'utf-8'))
print("[*] Encrypted: {}".format(encrypted))
return encrypted
def Decrypt(text_f: Entry):
plain = f.decrypt(bytes(text_f.get(), 'utf-8'))
print("[*] Plain: {}".format(plain))
return plain
#Set Window
root = Tk()
#Define Elements
text_user = ""
instruction_1 = Label(root, text="Input Text")
text_input = Entry(root, textvariable=text_user)
button_encode = Button(root, text='Encode', command=partial(Encrypt, text_input))
button_decode = Button(root, text='Decode', command=partial(Decrypt, text_input))
text_description = Label(root, text="")
#Pack Elements
instruction_1.pack(ipady = 10, ipadx = 5)
text_input.pack(ipady = 5, ipadx = 4)
button_encode.pack(ipady = 3, ipadx = 12)
button_decode.pack(ipady = 3, ipadx = 12)
#Setup Window Properties
root.geometry('800x650')
root.title("APEP | Encoder & Decoder")
#Loop Window Runtime
root.mainloop()我已经添加了导入和接收来自入口小部件(text_f.get())的文本的partial函数。
结果可能是您所期望的:
[*] Encrypted: b'gAAAAABequ_Y0sJVQVDTTcES3nHKm50gTlKqECPmEyLUgh3A1ehw0ANkKmk9PF3Y-vZ8wS6oGwvL6l432WiNO3U0LlTkD1ilhQ=='
[*] Plain: b'test'https://stackoverflow.com/questions/61523353
复制相似问题