-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword_Manager.py
More file actions
50 lines (40 loc) · 1.44 KB
/
Password_Manager.py
File metadata and controls
50 lines (40 loc) · 1.44 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
from cryptography.fernet import Fernet #fernet is use to encrypt and decrypt password
import os #use to interact the other file in program
def write_key():
key = Fernet.generate_key() #create a new random secret key
with open("key.key", "wb") as f: #open a file key.key in write binary mode
f.write(key) #write the key
#return the secret key from key.key
def load_key():
with open("key.key", "rb") as f:
return f.read()
# Generate key if not present
if not os.path.exists("key.key"): #check if the file or folder exist at given path
write_key() #create the key file
key = load_key() #load the key
fer = Fernet(key) #create Ferne object as named fer
def add():
name = input("Account: ")
pwd = input("Password: ")
with open("Passwords.txt", "a") as f:
f.write(f"{name} | {fer.encrypt(pwd.encode()).decode()}\n")
def view():
if not os.path.exists("Passwords.txt"):
print("No passwords saved yet.")
return
with open("Passwords.txt", "r") as f:
for line in f.readlines():
if "|" not in line:
continue
u, p = line.strip().split("|")
print(f"User: {u.strip()} | Password: {fer.decrypt(p.strip().encode()).decode()}")
while True:
mode = input("(view, add), q to quit: ").lower()
if mode == "q":
break
elif mode == "view":
view()
elif mode == "add":
add()
else:
print("Invalid mode")