-
Notifications
You must be signed in to change notification settings - Fork 49
/
Password Manager
98 lines (82 loc) · 3.16 KB
/
Password Manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Code for Password Generator
import os
import json
from cryptography.fernet import Fernet, InvalidToken
# Functions for password manager
def generate_key():
return Fernet.generate_key()
def create_fernet(key):
return Fernet(key)
def encrypt_password(fernet, password):
return fernet.encrypt(password.encode())
def decrypt_password(fernet, encrypted_password):
return fernet.decrypt(encrypted_password).decode()
def load_passwords(fernet, filename):
try:
with open(filename, 'r') as file:
data = json.load(file)
return {key: decrypt_password(fernet, value) for key, value in data.items()}
except (FileNotFoundError, InvalidToken):
return {}
def save_passwords(fernet, passwords, filename):
data = {key: encrypt_password(fernet, value) for key, value in passwords.items()}
with open(filename, 'w') as file:
json.dump(data, file)
def add_password(fernet, passwords, account, password, filename):
passwords[account] = password
save_passwords(fernet, passwords, filename)
def update_password(fernet, passwords, account, password, filename):
if account in passwords:
passwords[account] = password
save_passwords(fernet, passwords, filename)
else:
print(f"Account '{account}' not found.")
def delete_password(fernet, passwords, account, filename):
if account in passwords:
del passwords[account]
save_passwords(fernet, passwords, filename)
else:
print(f"Account '{account}' not found.")
# Main function
def main():
key = input("Enter the master password (keep this safe!): ").encode()
fernet = create_fernet(key)
filename = "passwords.json"
passwords = load_passwords(fernet, filename)
while True:
print("\nOptions:")
print("1. Show all accounts")
print("2. Retrieve password")
print("3. Add account")
print("4. Update account password")
print("5. Delete account")
print("6. Quit")
choice = input("Enter your choice: ")
if choice == "1":
print("Accounts:")
for account in passwords.keys():
print(account)
elif choice == "2":
account = input("Enter the account name: ")
if account in passwords:
print(f"Password for {account}: {passwords[account]}")
else:
print(f"Account '{account}' not found.")
elif choice == "3":
account = input("Enter the account name: ")
password = input("Enter the password: ")
add_password(fernet, passwords, account, password, filename)
print(f"Account '{account}' added.")
elif choice == "4":
account = input("Enter the account name: ")
password = input("Enter the new password: ")
update_password(fernet, passwords, account, password, filename)
elif choice == "5":
account = input("Enter the account name: ")
delete_password(fernet, passwords, account, filename)
elif choice == "6":
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()