I am making a password manager and generator. The user creates the login information, and then I hash their password. After that, I generate the password and encrypt the password with a key stored unprotected. My Question is how do I protect the key?
I need an easy way to do it since I am just a beginner and working on this password manager as a practice. However, I can't find what I need, I also intend to put this into a GUI which I'll work on when I know how to store the keys safely.
Here is the python file:
import random,string,os,sys from cryptography.fernet import Fernet import bcrypt if os.path.isfile('salt.txt'): #Gets the salt with open('salt.txt','rb') as saltfile: salt = saltfile.read() else: with open('salt.txt','wb')as saltfile: salt = bcrypt.gensalt() saltfile.write(salt) #Hashes the item def hashPass(item): global passwordOut hashed = bcrypt.hashpw(item,salt) passwordOut = hashed # Random Password Generator def setPassword(length=30,char=string.ascii_letters+string.digits+string.punctuation): global generatedPassword generatedPassword= ''.join(random.choice(char) for x in range(length)) return generatedPassword if os.path.isfile('mykey.key') == True: #Opens the key with open('mykey.key', 'rb') as mykey: print('True') key = mykey.read() f = Fernet(key) elif os.path.isfile('mykey.key')== False: print('False') # Generates a kay key = Fernet.generate_key() # Writes a new key with open('mykey.key', 'wb') as mykey: mykey.write(key) f = Fernet(key) #Sets the key #Stating initalization print("Hello and welcome to your password manager!") while True: #If there is a user file if os.path.isfile('user.txt'): #Read the user file with open('user.txt','rb') as user_file: file = user_file.read() print("File ", file) #Gets the inputs getUser = input("Enter your username ").encode('utf-8') getPass = input('Enter your password: ').encode('utf-8') #Hashes the inputs through the hashing funcion hashPass(item=getUser) usr = passwordOut hashPass(item=getPass) passw = passwordOut #If the users hashed input is the same in the users file it carries on with the procedure if usr in file and passw in file: while True: print("""Pick from the list of what you want to do: 1. Generate a new password 2. See passwords 3. Quit""") usrinput = int(input('Enter a number from the menu: ')) if usrinput == 1: print("\nGenerating password...") setPassword() usrinput = input("What is the password for: ") splitter = ': ' if os.path.isfile('passwordenc.txt'): with open('passwordenc.txt','ab')as password_file: var = usrinput + splitter + generatedPassword encrypted = f.encrypt(bytes(var.encode('utf-8'))) password_file.write(encrypted) password_file.write(b"--END OF PASSWORD--") print("Your new password for: "+usrinput) print("And the password is: "+generatedPassword) else: with open('passwordenc.txt','wb')as password_file: var = usrinput + splitter + generatedPassword encrypted = f.encrypt(bytes(var.encode('utf-8'))) password_file.write(encrypted) password_file.write(b"--END OF PASSWORD--") print("Your new password for: " + usrinput) print("And the password is: " + generatedPassword) if usrinput == 2: if os.path.isfile('passwordenc.txt'): with open('passwordenc.txt','r') as password_file: whole_file = password_file.read() password_list = whole_file.split("--END OF PASSWORD--") for password in password_list: if password: decrypt = f.decrypt(bytes(password, encoding="utf-8")) print("Decrypted pass: ", decrypt) else: print('File not found! Need to create a new file.') if usrinput == 3: quit() #If not the same it loops back around else: print("\nUser not found!\n") #If there is no file: else: #Gets a user input username = input('Enter a username: ').encode('utf-8') password = input('Enter a password, cannot be changed! ').encode('utf-8') #Hashes the user input hashPass(item=username) usr = passwordOut hashPass(item=password) passw = passwordOut #Writes the user input with open('user.txt','wb') as user: user.write(usr) user.write(passw) print('\nUser has been created!\n')