-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_credsmgr-sqlite3.py
84 lines (73 loc) · 2.77 KB
/
device_credsmgr-sqlite3.py
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
from cryptography.fernet import Fernet
import sqlite3
import os
# Generate a secret key for encryption (you should store this securely)
key = Fernet.generate_key()
cipher_suite = Fernet(key)
# Function to encrypt a password
def encrypt_password(password):
encrypted_password = cipher_suite.encrypt(password.encode())
return encrypted_password
# Function to decrypt a password
def decrypt_password(encrypted_password):
decrypted_password = cipher_suite.decrypt(encrypted_password).decode()
return decrypted_password
# Check if the database file exists; if not, create it
db_file = 'passwords.db'
if not os.path.exists(db_file):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Create the 'passwords' table with 'device' as a unique field
cursor.execute('''
CREATE TABLE passwords (
username TEXT,
device TEXT UNIQUE,
encrypted_password BLOB
)
''')
conn.commit()
else:
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Function to save an encrypted password and device to the database
def save_password(username, device, password):
encrypted_password = encrypt_password(password)
cursor.execute('INSERT OR REPLACE INTO passwords (username, device, encrypted_password) VALUES (?, ?, ?)',
(username, device, encrypted_password))
conn.commit()
# Function to retrieve and decrypt a password by device name
def get_password(device):
cursor.execute('SELECT username, encrypted_password FROM passwords WHERE device = ?', (device,))
result = cursor.fetchone()
if result:
username, encrypted_password = result
decrypted_password = decrypt_password(encrypted_password)
return username, decrypted_password
else:
return None, None
# Create or retrieve credentials by device name
while True:
print("1. Create credentials")
print("2. Retrieve credentials by device")
print("3. Exit")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
username = input("Enter username: ")
device = input("Enter device name: ")
password = input("Enter password: ")
save_password(username, device, password)
print("Credentials saved successfully!\n")
elif choice == '2':
device = input("Enter device name: ")
username, retrieved_password = get_password(device)
if username and retrieved_password:
print(f"Username: {username}")
print(f"Decrypted Password for {device}: {retrieved_password}\n")
else:
print(f"No credentials found for device {device}.\n")
elif choice == '3':
break
else:
print("Invalid choice. Please enter 1, 2, or 3.\n")
# Close the database connection when done
conn.close()