-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword_Manager.py
More file actions
106 lines (79 loc) · 2.81 KB
/
Password_Manager.py
File metadata and controls
106 lines (79 loc) · 2.81 KB
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
99
100
101
102
103
104
105
106
from cryptography.fernet import Fernet
class PasswordManager:
def __init__(self):
self.key = None
self.password_file = None
self.password_dictionary = {}
def create_key(self, path):
self.key = Fernet.generate_key()
with open(path, 'wb') as encrypted_file:
encrypted_file.write(self.key)
def loading_key(self, path):
with open(path, 'rb') as decrypted_file:
self.key = decrypted_file.read()
def create_password_file(self, path, init_value= None):
self.password_file = path
if init_value is not None:
for key, value in init_value.items():
self.add_password(key, value)
def load_password_file(self, path):
self.password_file = path
with open(path ,'r') as f:
for line in f:
site, encrypted = line.split(":", 1)
self.password_dictionary[site]= Fernet(self.key).decrypt(encrypted.encode()).decode()
def add_password(self, site, password):
self.password_dictionary[site] = password
if self.password_file is not None:
with open(self.password_file, 'a+') as f:
encrypted = Fernet(self.key).encrypt(password.encode())
f.write(site + ":" + encrypted.decode() + "\n")
def get_password(self,site):
return self.password_dictionary[site]
def main():
password = {
"email" : "1234567",
"Facebook" : "Meta1234",
"Youtube" : "CS50_",
"Instagram" : "Professor"
}
pm = PasswordManager()
print(""" What do you want to do?
1. Create a new key
2. Load an existing key
3. Create a new password
4. Load existing password
5. Add a new password
6. Get a password
q. Quit
""")
done = False
while not done:
choice = input("Enter choice: ")
match choice:
case "1":
path = input("Enter Path: ")
pm.create_key(path)
case "2":
path = input("Enter Path: ")
pm.loading_key(path)
case "3":
path = input("Enter Path: ")
pm.create_password_file(path, password)
case "4":
path = input("Enter Path: ")
pm.load_password_file(path)
case "5":
site = input("Enter Site: ")
password = input("Enter Password: ")
pm.add_password(site, password)
case "6":
site = input("Site: ")
print(f"Site: {site}\nPassword: {pm.get_password(site)}")
case "q":
done = True
print("Bye")
case _:
print("Invalid Choice")
if __name__ == "__main__":
main()