-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassword_manager
133 lines (113 loc) · 5.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from cryptography.fernet import Fernet, InvalidToken
import json
import getpass
class PasswordManager:
def __init__(self, master_password):
self.master_password = master_password
self.fernet_key = None
self.fernet = None
self.passwords = {}
def generate_key(self):
return Fernet.generate_key()
def initialize_fernet(self):
self.fernet_key = self.generate_key()
self.fernet = Fernet(self.fernet_key)
def save_passwords(self):
encrypted_master_password = self.fernet.encrypt(self.master_password.encode())
encrypted_passwords = {}
for category, account_info in self.passwords.items():
encrypted_account_info = {}
for account, password in account_info.items():
encrypted_password = self.fernet.encrypt(password.encode())
encrypted_account_info[account] = encrypted_password.decode() # Convert to string
encrypted_passwords[category] = encrypted_account_info
data_to_save = {
'master_password': encrypted_master_password.decode(), # Convert to string
'passwords': encrypted_passwords
}
with open("passwords.txt", "w") as file:
json.dump(data_to_save, file)
def load_passwords(self):
try:
with open("passwords.txt", "r") as file:
file_content = file.read()
print("File content:", file_content)
data = json.loads(file_content)
encrypted_master_password = data.get('master_password', '')
print("Encrypted Master Password:", encrypted_master_password) # Print encrypted master password
self.master_password = self.fernet.decrypt(encrypted_master_password.encode()).decode()
encrypted_passwords = data.get('passwords', {})
self.passwords = {}
for category, account_info in encrypted_passwords.items():
decrypted_account_info = {}
for account, encrypted_password in account_info.items():
try:
print(f"Decrypting password for {category} - {account}")
decrypted_password = self.fernet.decrypt(encrypted_password.encode()).decode()
decrypted_account_info[account] = decrypted_password
except InvalidToken:
print("InvalidToken exception: Password decryption failed.")
decrypted_account_info[account] = "Decryption failed"
self.passwords[category] = decrypted_account_info
except FileNotFoundError:
self.passwords = {}
except (json.JSONDecodeError, InvalidToken) as e:
print(f"Error loading passwords: {str(e)}")
self.passwords = {}
def add_password(self):
category = input("Enter the category: ")
account = input("Enter the account: ")
password = getpass.getpass("Enter the password: ")
if category not in self.passwords:
self.passwords[category] = {}
self.passwords[category][account] = password
self.save_passwords()
print("Password successfully saved.")
def get_password(self):
category = input("Enter the category: ")
account = input("Enter the account: ")
try:
password = self.passwords.get(category, {}).get(account)
if password:
entered_password = getpass.getpass("Enter your master password to retrieve the password: ")
if entered_password == self.master_password:
return f"Retrieved password: {password}"
else:
return "Incorrect master password. Access denied."
else:
return "Password not found."
except Exception as e:
return f"Unexpected error: {str(e)}"
# Example usage:
try:
master_password = getpass.getpass("Enter your master password: ")
password_manager = PasswordManager(master_password)
password_manager.initialize_fernet()
password_manager.load_passwords()
print(r"""
____
| _ \ _ _ _
| |_) ( )_ __ | | __ _ ___| |
| _ - | _ \| | _ / _` |/ __| |___
| |_) | | | | | (_) | (_) |\__ \ _ \
|____/|_|_| |_|_,___ |\__,_|_ __/ | |_|
""")
while True:
print(r""" ***** Password Manager ***** """)
print("\nOptions:")
print("1. Add Password")
print("2. Retrieve Password")
print("3. Exit")
choice = input("Enter your choice (1, 2, or 3): ")
if choice == "1":
password_manager.add_password()
elif choice == "2":
result = password_manager.get_password()
print(result)
elif choice == "3":
print("Exiting the Password Manager. Goodbye!")
break
else:
print("Invalid choice. Please enter 1, 2, or 3.")
except Exception as e:
print(f"An error occurred: {str(e)}")