Skip to content
Advertisement

How do I encrypt and decrypt files with Python fernet without getting TokenError?

I’m trying to make a login program that encrypts the login file when it’s not in use. The program keeps raising “InvalidToken” error. Does anyone know how to fix this? There might be some other problems with the code, so if you find any, please let me know as well. My code is below. I didn’t use all of the imported modules, but I have them there for later.

import tkinter
import math
import os
import cryptography
from cryptography.fernet import Fernet

key = b'LjCWah7vQJCyL80Qurd17gYewzrZ11zvzs9JDQMxDOg='
f = Fernet(key)

create_logins_file = open("Random ScriptsLogin Programlogins.txt", "w")
create_logins_file.close()

with open("Random ScriptsLogin Programlogins.txt", "rb") as original_file:
    original_content = original_file.read()

decrypted = f.decrypt(original_content)

with open("Random ScriptsLogin Programlogins.txt", "wb") as decrypted_file:
    decrypted_file.write(decrypted)

# The login function #
def login(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    if f"{username},{password}" in file_content[:]:
        return "You were logged in successfully"
    else:
        return "We could not find your account. Please check your spelling and try again."

# The account creation function #
def newaccount(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    file_append = open(file_path, "a")

    if f"{username},{password}" in file_content[:]:
        file_append.close()
        return "You already have an account, and were logged in successfully"
    else:
        file_append.write(username + "," + password + "n")
        file_append.close()
        return "New account created."        

logins_path = "Random ScriptsLogin Programlogins.txt"

signin_message = input("Would you like to: n1. Create an account nor n2. Log inn")
if signin_message == "1":
    print("User chose to create account")
    newacc_username = input("Input a username: ")
    newacc_password = input("Input a password: ")
    print(newaccount(newacc_username, newacc_password, logins_path))
elif signin_message == "2":
    print("User chose to log in")
    username = input("Input your username: ")
    password = input("Input your password: ")
    print(login(username, password,logins_path))
else:
    print("Please enter 1 or 2")

with open("Random ScriptsLogin Programlogins.txt", "rb") as new_original_file:
    new_original_content = new_original_file.read()

encrypted = f.encrypt(new_original_content)

with open("Random ScriptsLogin Programlogins.txt", "wb") as encrypted_file:
    encrypted_file.write(encrypted)

Advertisement

Answer

Your code works just fine, the problem is that you try to decrypt an empty file then verify its authenticity. so you need to create this file and try to encrypt any thing into this file then you can decrypt this file back again.

import tkinter
import math
import os
import cryptography
from cryptography.fernet import Fernet

key = b'LjCWah7vQJCyL80Qurd17gYewzrZ11zvzs9JDQMxDOg='
f = Fernet(key)

create_logins_file = open("Random ScriptsLogin Programlogins.txt", "wb")
    # encrypt anything
create_logins_file.write(f.encrypt(b"hello"))
create_logins_file.close()

with open("Random ScriptsLogin Programlogins.txt", "rb") as original_file:
    original_content = original_file.read()
decrypted = f.decrypt(original_content)

with open("Random ScriptsLogin Programlogins.txt", "wb") as decrypted_file:
    decrypted_file.write(decrypted)

# The login function #
def login(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    if f"{username},{password}" in file_content[:]:
        return "You were logged in successfully"
    else:
        return "We could not find your account. Please check your spelling and try again."

# The account creation function #
def newaccount(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    file_append = open(file_path, "a")

    if f"{username},{password}" in file_content[:]:
        file_append.close()
        return "You already have an account, and were logged in successfully"
    else:
        file_append.write(username + "," + password + "n")
        file_append.close()
        return "New account created."        

logins_path = "Random ScriptsLogin Programlogins.txt"

signin_message = input("Would you like to: n1. Create an account nor n2. Log inn")
if signin_message == "1":
    print("User chose to create account")
    newacc_username = input("Input a username: ")
    newacc_password = input("Input a password: ")
    print(newaccount(newacc_username, newacc_password, logins_path))
elif signin_message == "2":
    print("User chose to log in")
    username = input("Input your username: ")
    password = input("Input your password: ")
    print(login(username, password,logins_path))
else:
    print("Please enter 1 or 2")

with open("Random ScriptsLogin Programlogins.txt", "rb") as new_original_file:
    new_original_content = new_original_file.read()

encrypted = f.encrypt(new_original_content)

with open("Random ScriptsLogin Programlogins.txt", "wb") as encrypted_file:
    encrypted_file.write(encrypted)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement