-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathSimple_Password_Manager
More file actions
87 lines (73 loc) · 2.82 KB
/
Simple_Password_Manager
File metadata and controls
87 lines (73 loc) · 2.82 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
import base64
import getpass
import json
# For demonstration purposes, using a simple XOR cipher
def xor_encrypt_decrypt(data, key):
"""Simple XOR encryption/decryption."""
data_bytes = data.encode('utf-8')
key_bytes = key.encode('utf-8')
key_len = len(key_bytes)
result = bytearray()
for i in range(len(data_bytes)):
result.append(data_bytes[i] ^ key_bytes[i % key_len])
return base64.b64encode(result).decode('utf-8')
def xor_decrypt_data(encrypted_data, key):
"""Simple XOR decryption."""
encrypted_bytes = base64.b64decode(encrypted_data.encode('utf-8'))
key_bytes = key.encode('utf-8')
key_len = len(key_bytes)
result = bytearray()
for i in range(len(encrypted_bytes)):
result.append(encrypted_bytes[i] ^ key_bytes[i % key_len])
return result.decode('utf-8')
def save_passwords(passwords, master_key):
"""Encrypt and save passwords to a file."""
encrypted_passwords = {
key: xor_encrypt_decrypt(value, master_key)
for key, value in passwords.items()
}
with open("passwords.json", "w") as f:
json.dump(encrypted_passwords, f)
print("Passwords saved successfully.")
def load_passwords(master_key):
"""Load and decrypt passwords from a file."""
try:
with open("passwords.json", "r") as f:
encrypted_passwords = json.load(f)
passwords = {}
for key, value in encrypted_passwords.items():
passwords[key] = xor_decrypt_data(value, master_key)
return passwords
except FileNotFoundError:
return {}
except Exception as e:
print(f"Error loading passwords: {e}")
return {}
def main():
"""Main function for the password manager."""
master_key = getpass.getpass("Enter your master password: ")
passwords = load_passwords(master_key)
while True:
print("\nWhat would you like to do?")
print("1. Add a new password")
print("2. Get a password")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
service = input("Enter the service/website name: ").strip()
password = getpass.getpass("Enter the password: ")
passwords[service] = password
save_passwords(passwords, master_key)
elif choice == "2":
service = input("Enter the service/website name to retrieve: ").strip()
if service in passwords:
print(f"Password for {service}: {passwords[service]}")
else:
print(f"No password found for {service}.")
elif choice == "3":
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()