-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordManager.py
More file actions
106 lines (86 loc) · 3.19 KB
/
PasswordManager.py
File metadata and controls
106 lines (86 loc) · 3.19 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
import os
# ---- KEY HANDLING ----
def load_or_create_key():
if not os.path.exists("secret.key"): # agar key file nahi hai to banao
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
else:
with open("secret.key", "rb") as key_file:
key = key_file.read()
return key
# Key load
key = load_or_create_key()
fernet = Fernet(key)
# ---- FUNCTIONS ----
def addPass():
name = input("Enter Account Name: ")
psw = input("Enter Password: ")
# Encrypt password
enc_psw = fernet.encrypt(psw.encode()).decode()
with open("password.txt", "a") as f:
f.write(name + "|" + enc_psw + "\n")
print("Password saved successfully!")
def viewPass():
if not os.path.exists("password.txt"):
print("No password saved yet!")
return
with open("password.txt", "r") as f:
for line in f.readlines():
data = line.rstrip()
if "|" in data:
user, enc_psw = data.split("|")
# Decrypt password
try:
dec_psw = fernet.decrypt(enc_psw.encode()).decode()
except Exception:
dec_psw = "ERROR (wrong key?)"
print(f"User: {user} | Password: {dec_psw}")
def editPass(account_name, new_password):
lines = []
with open("password.txt", "r") as f:
lines = f.readlines()
with open("password.txt", "w") as f:
for line in lines:
user, enc_psw = line.strip().split("|")
if user == account_name:
# update password
new_enc = fernet.encrypt(new_password.encode()).decode()
f.write(user + "|" + new_enc + "\n")
else:
f.write(line)
def deletePass(account_name):
lines = []
with open("password.txt", "r") as f:
lines = f.readlines()
with open("password.txt", "w") as f:
for line in lines:
user, enc_psw = line.strip().split("|")
if user != account_name: # only keep others
f.write(line)
print(f"{account_name} deleted successfully (if it existed).")
# ---- MAIN PROGRAM ----
MasterPassword = "Sahil"
MasterPass = input("Enter your master password to access: ")
if MasterPass == MasterPassword:
while True:
option = input("\nEnter 'add' to add a password, 'view' to view passwords,'edit' to edit passwords , 'delete' to delete passwords ,'q' to quit: ").lower()
if option == "q":
print("Bye...")
break
elif option == "add":
addPass()
elif option == "view":
viewPass()
elif option == "edit":
accout = input("Enter Account Name:")
newpass = input("Enter YOur new pass:")
editPass(accout,newpass)
elif option == "delete":
acc = input("Enter account name to delete: ")
deletePass(acc)
else:
print("Invalid Option!")
else:
print("Wrong master password!")