from cryptography.fernet import Fernet
correct_master = "Master"
while True:
user_input_password = input("Master password: ")
if user_input_password == correct_master:
while True:
key = Fernet.generate_key()
f = Fernet(key)
user_input = input('What would you like to do? Type "add" to add a account, type "see" to see the accounts and type "q" to quit. ').lower()
if user_input == "add":
with open("passwords.txt", "a") as wf:
user_input1 = input("Account: ")
user_input2 = input("Password: ")
token1 = f.encrypt(user_input1.encode()).decode()
token2 = f.encrypt(user_input2.encode()).decode()
wf.write(f"Account: {token1}")
wf.write("\n")
wf.write(f"Password: {token2}")
wf.write("\n")
wf.write("\n")
elif user_input == "see":
with open("passwords.txt", "r") as rf:
content = rf.readlines()
if len(content) !=0:
for line in content:
content0 = line.rstrip()
content1 = content0.split(": ")
useful = content1[1]
useful1 = f.decrypt(useful.encode()).decode() # The error
print(useful1)
else:
pass
elif user_input == "q":
print("bye", end="")
quit()
else:
print("I don't understand. Do you want to add/see/q? ")
else:
print("Wrong master password")
它不会让我使用 Fernet 解密。错误:签名与摘要不匹配。后跟一个 InvalidToken。有什么解决办法吗?