user12624402
user12624402

Reputation:

raise InvalidToken, cryptography.fernet.InvalidToken

I am having issues with my program, all it is doing is encrypting and decrypting text based on a key. However when I try and decrypt the encrypted words, it just spits out the error

raise InvalidToken cryptography.fernet.InvalidToken

This is the code

#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()

Upvotes: 0

Views: 4171

Answers (1)

Filip Górczyński
Filip Górczyński

Reputation: 765

I't not identical code that you provided. I suppose you've passed invalid value to Encrypt/Decrypt function.

I've added some changes:

#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()

I've added partial function import and receiving text from Entry widget (text_f.get()).

Results are what you probably expected:

[*] Encrypted: b'gAAAAABequ_Y0sJVQVDTTcES3nHKm50gTlKqECPmEyLUgh3A1ehw0ANkKmk9PF3Y-vZ8wS6oGwvL6l432WiNO3U0LlTkD1ilhQ=='
[*] Plain: b'test'

Upvotes: 2

Related Questions