首页 > 解决方案 > 如何在不出现 TokenError 的情况下使用 Python fernet 加密和解密文件?

问题描述

我正在尝试制作一个登录程序,在登录文件不使用时对其进行加密。该程序不断引发“InvalidToken”错误。有谁知道如何解决这一问题?代码可能还有其他问题,所以如果你发现任何问题,请告诉我。我的代码如下。我没有使用所有导入的模块,但我有它们以备后用。

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

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

create_logins_file = open("Random Scripts\Login Program\logins.txt", "w")
create_logins_file.close()

with open("Random Scripts\Login Program\logins.txt", "rb") as original_file:
    original_content = original_file.read()

decrypted = f.decrypt(original_content)

with open("Random Scripts\Login Program\logins.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 Scripts\Login Program\logins.txt"

signin_message = input("Would you like to: \n1. Create an account \nor \n2. Log in\n")
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 Scripts\Login Program\logins.txt", "rb") as new_original_file:
    new_original_content = new_original_file.read()

encrypted = f.encrypt(new_original_content)

with open("Random Scripts\Login Program\logins.txt", "wb") as encrypted_file:
    encrypted_file.write(encrypted)

标签: pythonencryptioncryptography

解决方案


您的代码工作得很好,问题是您尝试解密一个空文件然后验证其真实性。所以你需要创建这个文件并尝试将任何东西加密到这个文件中,然后你可以再次解密这个文件。

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

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

create_logins_file = open("Random Scripts\Login Program\logins.txt", "wb")
    # encrypt anything
create_logins_file.write(f.encrypt(b"hello"))
create_logins_file.close()

with open("Random Scripts\Login Program\logins.txt", "rb") as original_file:
    original_content = original_file.read()
decrypted = f.decrypt(original_content)

with open("Random Scripts\Login Program\logins.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 Scripts\Login Program\logins.txt"

signin_message = input("Would you like to: \n1. Create an account \nor \n2. Log in\n")
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 Scripts\Login Program\logins.txt", "rb") as new_original_file:
    new_original_content = new_original_file.read()

encrypted = f.encrypt(new_original_content)

with open("Random Scripts\Login Program\logins.txt", "wb") as encrypted_file:
    encrypted_file.write(encrypted)

推荐阅读