首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提升InvalidToken,cryptography.fernet.InvalidToken

提升InvalidToken,cryptography.fernet.InvalidToken
EN

Stack Overflow用户
提问于 2020-04-30 20:25:58
回答 1查看 1.9K关注 0票数 0

我的程序有问题,它所做的一切都是基于密钥对文本进行加密和解密。但是,当我尝试对加密的单词进行解密时,它只会显示错误

raise InvalidToken cryptography.fernet.InvalidToken

这是代码

代码语言:javascript
复制
#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()

EN

回答 1

Stack Overflow用户

发布于 2020-04-30 23:40:12

我没有你提供的相同的代码。我想你已经向加密/解密函数传递了无效值。

我添加了一些更改:

代码语言:javascript
复制
#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函数。

结果可能是您所期望的:

代码语言:javascript
复制
[*] Encrypted: b'gAAAAABequ_Y0sJVQVDTTcES3nHKm50gTlKqECPmEyLUgh3A1ehw0ANkKmk9PF3Y-vZ8wS6oGwvL6l432WiNO3U0LlTkD1ilhQ=='
[*] Plain: b'test'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61523353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档