I am new in python and I am doing these kind of little projects so that I can build myself. This is a little project which will only add and view the username and passwords with a master password. I was able to add username and passwords with encrypted the passwords in the file. But to view the passwords it shows me two problems in 23 and 39 line. And also the master password is not working.
from cryptography.fernet import Fernet
'''
def write_key():
key = Fernet.generate_key()
with open("key.key", "wb") as key_file:
key_file.write(key)'''
def load_key():
file = open("key.key", "rb")
key = file.read()
file.close()
return key
master_pwd = input("What is the master password? ")
key = load_key() + master_pwd.encode()
fer = Fernet(key)
def view():
with open('passwords.txt', 'r') as f:
for line in f.readlines():
data = line.rstrip()
user, passw = data.split("|")
print("User:", user, "| Password:",
fer.decrypt(passw.encode()).decode())
def add():
name = input("Account name: ")
pwd = input("Input password: ")
with open('passwords.txt', 'a') as f:
f.write(name + "|" + fer.encrypt(pwd.encode()).decode() + "\n")
while True:
mode = input("Would you like to add a new password or view existing ones?(Add/View):\nOtherwise press 'Q' to Quit: \n").lower()
if mode == "q":
break
elif mode == "view":
view()
elif mode == "add":
add()
else:
print("Invalid mode.")
continue