1

I was trying to use a library I found in python called fernet and make a simple program encrypting and decrypting in python and it worked but when I tried to make it GUI it kept spiting out the error "invalid token". I don't know what I did wrong but it seems that it is only throwing a error in the GUI, Can someone help? (Just so you know I am not the smartest in python so can you explain in simple terms)

Here is the full error:

line 121, in _get_unverified_token_data raise InvalidToken cryptography.fernet.InvalidToken

from cryptography.fernet import Fernet
from tkinter import *
import tkinter as tk


def Encrypt():
    get=tk.StringVar()
    message=get.get()



    byte_message = str.encode(message)

    f = Fernet(b'U_v31LOlh9SOe3eN-18CNUropIalPD02eqxgTxUnuAI=')
    encrypted_message = f.encrypt(byte_message)
    enmess.config(text = encrypted_message.decode(), font=('Poppins',5))

def Decrypt():
    get=tk.StringVar()
    message=get.get()
    f = Fernet(b'U_v31LOlh9SOe3eN-18CNUropIalPD02eqxgTxUnuAI=')
    message = str.encode(message)
    encrypted_message = f.decrypt(message)
    print(encrypted_message.decode())


#decrypt_message = f.decrypt(b'gAAAAABkTNLJxD-1doUad45Srrb0vku4QdC0sQ-KZLr4-Z5OdgJ5R118ApojM-3HQTc3_YcYLJ6FYNQgYylc1V9QE2G_lTt1_A==')
#print(decrypt_message)
root=tk.Tk()
root.geometry("600x400")
message=tk.StringVar()


    
name_label = tk.Label(root, text = "Enter Message", font=('Poppins',10, 'bold'))
name_entry = tk.Entry(root,textvariable = message, font=('Poppins',10,'normal'))
enmess = tk.Label(root, text = "will be updated soon", font=('Poppins',10))
encrypt_btn=tk.Button(root,text = 'Encrypt', command = Encrypt)
decrypt_btn=tk.Button(root,text = 'Decrypt', command = Decrypt)



name_label.grid(row=0,column=1)
name_entry.grid(row=0,column=2)
encrypt_btn.grid(row=2,column=1)
decrypt_btn.grid(row=2,column=2)
enmess.grid(row=3, column=0)
root.mainloop()



I fiddled around a lot and so the code may look a little weird but thank you for your help (:

  • _when I tried to make it GUI it kept spiting out the error "invalid token"_ Please edit the question and add that full error message. – John Gordon Apr 30 '23 at 00:06

1 Answers1

0

The error message you're seeing is related to the cryptography library.

InvalidToken exception is raised when the token passed to Fernet.decrypt() method is either invalid or the message was tampered with.

Here are a few things you can check:

Make sure you are entering the correct token to decrypt. It should be the exact same token that was generated during encryption.

Ensure that the token is being passed correctly to the Fernet.decrypt() method.

Check if there is any change in the message or token during transfer or storage.

  • I don't think it was because I copied and pasted the message, and I will try to make it 'Fernet.encrypt' rather than 'f.encrypt' –  May 01 '23 at 21:54