Skip to content

Commit 69441b8

Browse files
authored
Create password_manager
1 parent 0bf482a commit 69441b8

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

password_manager

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from cryptography.fernet import Fernet, InvalidToken
2+
import json
3+
import getpass
4+
5+
class PasswordManager:
6+
def __init__(self, master_password):
7+
self.master_password = master_password
8+
self.fernet_key = None
9+
self.fernet = None
10+
self.passwords = {}
11+
12+
def generate_key(self):
13+
return Fernet.generate_key()
14+
15+
def initialize_fernet(self):
16+
self.fernet_key = self.generate_key()
17+
self.fernet = Fernet(self.fernet_key)
18+
19+
def save_passwords(self):
20+
encrypted_master_password = self.fernet.encrypt(self.master_password.encode())
21+
encrypted_passwords = {}
22+
for category, account_info in self.passwords.items():
23+
encrypted_account_info = {}
24+
for account, password in account_info.items():
25+
encrypted_password = self.fernet.encrypt(password.encode())
26+
encrypted_account_info[account] = encrypted_password.decode() # Convert to string
27+
encrypted_passwords[category] = encrypted_account_info
28+
29+
data_to_save = {
30+
'master_password': encrypted_master_password.decode(), # Convert to string
31+
'passwords': encrypted_passwords
32+
}
33+
34+
with open("passwords.txt", "w") as file:
35+
json.dump(data_to_save, file)
36+
37+
def load_passwords(self):
38+
try:
39+
with open("passwords.txt", "r") as file:
40+
file_content = file.read()
41+
print("File content:", file_content)
42+
43+
data = json.loads(file_content)
44+
encrypted_master_password = data.get('master_password', '')
45+
print("Encrypted Master Password:", encrypted_master_password) # Print encrypted master password
46+
47+
self.master_password = self.fernet.decrypt(encrypted_master_password.encode()).decode()
48+
49+
encrypted_passwords = data.get('passwords', {})
50+
self.passwords = {}
51+
for category, account_info in encrypted_passwords.items():
52+
decrypted_account_info = {}
53+
for account, encrypted_password in account_info.items():
54+
try:
55+
print(f"Decrypting password for {category} - {account}")
56+
decrypted_password = self.fernet.decrypt(encrypted_password.encode()).decode()
57+
decrypted_account_info[account] = decrypted_password
58+
except InvalidToken:
59+
print("InvalidToken exception: Password decryption failed.")
60+
decrypted_account_info[account] = "Decryption failed"
61+
self.passwords[category] = decrypted_account_info
62+
63+
except FileNotFoundError:
64+
self.passwords = {}
65+
except (json.JSONDecodeError, InvalidToken) as e:
66+
print(f"Error loading passwords: {str(e)}")
67+
self.passwords = {}
68+
69+
def add_password(self):
70+
category = input("Enter the category: ")
71+
account = input("Enter the account: ")
72+
password = getpass.getpass("Enter the password: ")
73+
74+
if category not in self.passwords:
75+
self.passwords[category] = {}
76+
self.passwords[category][account] = password
77+
self.save_passwords()
78+
print("Password successfully saved.")
79+
80+
def get_password(self):
81+
category = input("Enter the category: ")
82+
account = input("Enter the account: ")
83+
84+
try:
85+
password = self.passwords.get(category, {}).get(account)
86+
if password:
87+
entered_password = getpass.getpass("Enter your master password to retrieve the password: ")
88+
if entered_password == self.master_password:
89+
return f"Retrieved password: {password}"
90+
else:
91+
return "Incorrect master password. Access denied."
92+
else:
93+
return "Password not found."
94+
except Exception as e:
95+
return f"Unexpected error: {str(e)}"
96+
97+
# Example usage:
98+
try:
99+
master_password = getpass.getpass("Enter your master password: ")
100+
password_manager = PasswordManager(master_password)
101+
password_manager.initialize_fernet()
102+
password_manager.load_passwords()
103+
print(r"""
104+
____
105+
| _ \ _ _ _
106+
| |_) ( )_ __ | | __ _ ___| |
107+
| _ - | _ \| | _ / _` |/ __| |___
108+
| |_) | | | | | (_) | (_) |\__ \ _ \
109+
|____/|_|_| |_|_,___ |\__,_|_ __/ | |_|
110+
""")
111+
112+
while True:
113+
print(r""" ***** Password Manager ***** """)
114+
print("\nOptions:")
115+
print("1. Add Password")
116+
print("2. Retrieve Password")
117+
print("3. Exit")
118+
119+
choice = input("Enter your choice (1, 2, or 3): ")
120+
121+
if choice == "1":
122+
password_manager.add_password()
123+
elif choice == "2":
124+
result = password_manager.get_password()
125+
print(result)
126+
elif choice == "3":
127+
print("Exiting the Password Manager. Goodbye!")
128+
break
129+
else:
130+
print("Invalid choice. Please enter 1, 2, or 3.")
131+
132+
except Exception as e:
133+
print(f"An error occurred: {str(e)}")

0 commit comments

Comments
 (0)