-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_manager.py
More file actions
70 lines (58 loc) · 1.89 KB
/
password_manager.py
File metadata and controls
70 lines (58 loc) · 1.89 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
import os
from cryptography.fernet import Fernet
KEY_FILE = "key.key"
PASSWORD = "password.txt"
MASTER_PASSWORD = "pwd123"
#key generation
def write_key():
if not os.path.exists(KEY_FILE):
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as file:
file.write(key)
write_key()
# loading key
def load_key():
with open(KEY_FILE, "rb") as file:
return file.read()
key = load_key()
fer = Fernet(key)
m_pwd_entered = input("Enter the master password: ")
if m_pwd_entered != MASTER_PASSWORD:
print("Access Denied.")
quit()
def add():
a_name = input("Account Name: ")
pwd = input("Password: ")
encrypted_pwd = fer.encrypt(pwd.encode()).decode() # Take a password string → turn it into bytes → encrypt → turn it back into a string.
with open(PASSWORD, "a") as file:
file.write(a_name + "|" + encrypted_pwd+ "\n")
print("✅ Password saved successfully!!")
def view():
try:
with open(PASSWORD, "r") as file:
lines = file.readlines()
if not lines:
print("No passwords found.")
return
for line in lines:
account, enc_pwd = line.strip().split("|")
try:
decrypted = fer.decrypt(enc_pwd.encode()).decode()
print(f"🔐 Account: {account}, Password: {decrypted}")
except Exception as e:
print(f"⚠ Error decoding password for {account}: {e}")
except FileNotFoundError:
print("No Passwords saved yet.")
while True:
print("\n🔧 What would you like to do?")
print("Type 'add' to save, 'view' to read, or 'q' to quit.")
mode = input("> ").strip().lower()
if mode == "q":
break
elif mode == "add":
add()
elif mode == "view":
view()
else:
print("❌ Invalid mode.")
continue