-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
60 lines (50 loc) · 2.07 KB
/
Code.py
File metadata and controls
60 lines (50 loc) · 2.07 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
import vault_core
import json
import os
class PasswordManager:
def __init__(self, db_path="passwords.json"):
self.db_path = db_path
self.engine = vault_core.VaultEngine()
self.data = self._load_db()
def _load_db(self):
"""Loads the database from a JSON file."""
if os.path.exists(self.db_path):
with open(self.db_path, "r") as f:
return json.load(f)
return {}
def _save_db(self):
"""Saves the encrypted database to a JSON file."""
with open(self.db_path, "w") as f:
json.dump(self.data, f, indent=4)
def add_password(self, service, password, master_key):
"""Encrypts and stores a new password."""
try:
# Use the C++ engine to encrypt
encrypted_pwd = self.engine.encrypt(password, master_key)
# Store as hex string so it can be saved in JSON
self.data[service] = encrypted_pwd.hex()
self._save_db()
print(f"[+] Password for '{service}' saved successfully.")
except Exception as e:
print(f"[!] Error: {e}")
def get_password(self, service, master_key):
"""Retrieves and decrypts a password."""
if service not in self.data:
print(f"[!] Service '{service}' not found.")
return
try:
# Convert hex back to binary bytes for the C++ engine
encrypted_pwd_bytes = bytes.fromhex(self.data[service])
decrypted_pwd = self.engine.decrypt(encrypted_pwd_bytes, master_key)
print(f"[*] Password for {service}: {decrypted_pwd}")
except Exception as e:
print(f"[!] Decryption failed: Likely wrong Master Key.")
# --- Example Usage ---
if __name__ == "__main__":
# IMPORTANT: The master key must be exactly 32 characters for AES-256
MY_KEY = "my_very_secure_master_key_32_ch!"
manager = PasswordManager()
# 1. Store a password
manager.add_password("GitHub", "SuperSecret123!", MY_KEY)
# 2. Retrieve a password
manager.get_password("GitHub", MY_KEY)