Skip to content

Commit a132e47

Browse files
authored
Merge pull request #28 from KirtiPratihar/patch-1
Create Simple_Password_Manager
2 parents 91e7010 + dc5ac01 commit a132e47

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import base64
2+
import getpass
3+
import json
4+
5+
# For demonstration purposes, using a simple XOR cipher
6+
7+
def xor_encrypt_decrypt(data, key):
8+
"""Simple XOR encryption/decryption."""
9+
data_bytes = data.encode('utf-8')
10+
key_bytes = key.encode('utf-8')
11+
key_len = len(key_bytes)
12+
result = bytearray()
13+
for i in range(len(data_bytes)):
14+
result.append(data_bytes[i] ^ key_bytes[i % key_len])
15+
return base64.b64encode(result).decode('utf-8')
16+
17+
def xor_decrypt_data(encrypted_data, key):
18+
"""Simple XOR decryption."""
19+
encrypted_bytes = base64.b64decode(encrypted_data.encode('utf-8'))
20+
key_bytes = key.encode('utf-8')
21+
key_len = len(key_bytes)
22+
result = bytearray()
23+
for i in range(len(encrypted_bytes)):
24+
result.append(encrypted_bytes[i] ^ key_bytes[i % key_len])
25+
return result.decode('utf-8')
26+
27+
def save_passwords(passwords, master_key):
28+
"""Encrypt and save passwords to a file."""
29+
encrypted_passwords = {
30+
key: xor_encrypt_decrypt(value, master_key)
31+
for key, value in passwords.items()
32+
}
33+
with open("passwords.json", "w") as f:
34+
json.dump(encrypted_passwords, f)
35+
print("Passwords saved successfully.")
36+
37+
def load_passwords(master_key):
38+
"""Load and decrypt passwords from a file."""
39+
try:
40+
with open("passwords.json", "r") as f:
41+
encrypted_passwords = json.load(f)
42+
43+
passwords = {}
44+
for key, value in encrypted_passwords.items():
45+
passwords[key] = xor_decrypt_data(value, master_key)
46+
47+
return passwords
48+
except FileNotFoundError:
49+
return {}
50+
except Exception as e:
51+
print(f"Error loading passwords: {e}")
52+
return {}
53+
54+
def main():
55+
"""Main function for the password manager."""
56+
master_key = getpass.getpass("Enter your master password: ")
57+
passwords = load_passwords(master_key)
58+
59+
while True:
60+
print("\nWhat would you like to do?")
61+
print("1. Add a new password")
62+
print("2. Get a password")
63+
print("3. Exit")
64+
65+
choice = input("Enter your choice: ")
66+
67+
if choice == "1":
68+
service = input("Enter the service/website name: ").strip()
69+
password = getpass.getpass("Enter the password: ")
70+
passwords[service] = password
71+
save_passwords(passwords, master_key)
72+
73+
elif choice == "2":
74+
service = input("Enter the service/website name to retrieve: ").strip()
75+
if service in passwords:
76+
print(f"Password for {service}: {passwords[service]}")
77+
else:
78+
print(f"No password found for {service}.")
79+
80+
elif choice == "3":
81+
print("Exiting...")
82+
break
83+
84+
else:
85+
print("Invalid choice. Please try again.")
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)