This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
437 lines (356 loc) · 19.3 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import sys
import string
import random
import sqlite3
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow, QFileDialog, QMessageBox
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Hash import SHA512
from Crypto.Random import get_random_bytes
import json
from base64 import b64encode, b64decode
from Crypto.Cipher import AES
import re
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi("welcome_screen.ui", self)
self.pushButtonLogin.clicked.connect(self.gotoLogin)
self.pushButtonCreateNewPasswordDatabase.clicked.connect(self.gotoCreateNewPasswordDatabase)
# Loads the welcome screen gui and connects the login and create password database buttons to the functions
def gotoLogin(self):
login = LoginScreen()
widget.addWidget(login)
widget.setCurrentIndex(widget.currentIndex() + 1)
# Changes the screen to the login gui
def gotoCreateNewPasswordDatabase(self):
createDB = CreateDatabase()
widget.addWidget(createDB)
widget.setCurrentIndex(widget.currentIndex() + 1)
# Changes the screen to the create new password databse gui
class CreateDatabase(QDialog):
def __init__(self):
super(CreateDatabase, self).__init__()
loadUi("dialogCreateNewPasswordDatabase.ui", self)
# self.lineEditMasterPassword.setEchoMode(QtWidgets.QLineEdit.Password)
self.pushButtonCreate.clicked.connect(self.create)
self.pushButtonSelectPasswordDatabaseLocation.clicked.connect(self.select_password_db_location)
self.master_password = None
self.password_db_location = None
# Loads in the create new password database gui and connects the create and select password database buttons to the functions
def gotoPasswordManager(self):
password_manager = PasswordManager("[]", self.master_password, self.password_db_location)
widget.addWidget(password_manager)
widget.setCurrentIndex(widget.currentIndex() + 1)
# Changes the screen to the password manager gui
def select_password_db_location(self):
directory = str(QtWidgets.QFileDialog.getExistingDirectory())
file = directory + "/passwords.txt"
self.lineEditPasswordDatabaseLocation.setText(file)
# Open the file browser window and let the user select a location for the new password database
def create(self):
self.master_password = self.lineEditMasterPassword.text()
self.password_db_location = str(self.lineEditPasswordDatabaseLocation.text())
db = []
# Take user input for the master password and the new database location.
try:
with open(self.password_db_location, "w") as file:
for i in range(len(db)):
current_tuple = list(db[i])
current_tuple = tuple(current_tuple)
plaintext = ",".join(current_tuple)
# Create the new password database text file
salt = get_random_bytes(16)
key = PBKDF2(self.master_password, salt, 16, count=1000000, hmac_hash_module=SHA512)
# Create a salt and key
file.write(f"salt={b64encode(salt)},")
# Encode the salt with base64
header = b"Password Number" + str(i).encode()
cipher = AES.new(key, AES.MODE_GCM)
cipher.update(header)
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode())
# Create the header and cipher text
json_k = ['nonce', 'header', 'ciphertext', 'tag']
json_v = [b64encode(x).decode('utf-8') for x in (cipher.nonce, header, ciphertext, tag)]
result = json.dumps(dict(zip(json_k, json_v)))
# Combine the cipher text, nonce, tag and header into a single array and write it to the text file
self.gotoPasswordManager()
# Open the password manager screen
except FileNotFoundError:
warningBox = QMessageBox()
warningBox.setIcon(QMessageBox.Critical)
warningBox.setText("You need to enter a valid file path")
warningBox.setWindowTitle("Warning")
warningBox.setStandardButtons(QMessageBox.Ok)
warningBox.exec_()
# This creates a warning box if the user didn't input a valid file path
class LoginScreen(QDialog):
def __init__(self):
super(LoginScreen, self).__init__()
loadUi("login.ui", self)
self.lineEditMasterPassword.setEchoMode(QtWidgets.QLineEdit.Password)
self.pushButtonLogin.clicked.connect(self.decrypt_db)
self.pushButtonChangePasswordDatabaseLocation.clicked.connect(self.change_password_database_location)
# Connect the login button and change password database location button to the fucntions
self.records = []
self.master_password = None
self.password_db_location = None
# Create variables
def gotoPasswordManager(self):
password_manager = PasswordManager(self.records, self.master_password, self.password_db_location)
widget.addWidget(password_manager)
widget.setCurrentIndex(widget.currentIndex() + 1)
# Go to password manager screen
def change_password_database_location(self):
file_name = QFileDialog.getOpenFileName(self, 'Open Password Database File', r"C:", "Text files (*.txt)")
self.lineEditPasswordDatabaseLocation.setText(file_name[0])
# Open file browser and let the user find the password database in windows explorer
def decrypt_db(self):
self.master_password = self.lineEditMasterPassword.text()
self.password_db_location = self.lineEditPasswordDatabaseLocation.text()
# Get master password and password db location from user input
if len(self.master_password) == 0:
self.labelError.setText("Please input a master password")
# If the master password box is empty, prompt the user to enter a password.
elif len(self.password_db_location) == 0:
self.labelError.setText("Please input the file location for the password db")
# If the password box is empty, prompt the user to enter the location
else:
try:
with open(self.password_db_location, "r") as file:
for line in file:
stripped_line = line.strip()
ssalt = re.findall('''salt=b'(.*)',''', str(stripped_line))
salt = b64decode(ssalt[0])
# Open the password db file, get the salt and decode it from base64
key = PBKDF2(self.master_password, salt, 16, count=1000000, hmac_hash_module=SHA512)
# Create the key from the salt and master password
json_input = re.findall("salt=b'.*',({.*})", str(stripped_line))
b64 = json.loads(json_input[0])
json_k = ['nonce', 'header', 'ciphertext', 'tag']
jv = {k: b64decode(b64[k]) for k in json_k}
# Get the ciphertext, nonce, header and tag
cipher = AES.new(key, AES.MODE_GCM, nonce=jv['nonce'])
cipher.update(jv['header'])
plaintext = cipher.decrypt_and_verify(jv['ciphertext'], jv['tag'])
# Create the cipher and check if the ciphertext has been tampered with. If it hasn't, decrypt
plaintext = tuple(plaintext.decode().strip().split(","))
self.records.append(plaintext)
# Put each record into a tuple, and add each tuple to an array.
self.gotoPasswordManager()
except ValueError:
warningBox = QMessageBox()
warningBox.setIcon(QMessageBox.Critical)
warningBox.setText("The password is invalid. Please try again")
warningBox.setWindowTitle("Warning")
warningBox.setStandardButtons(QMessageBox.Ok)
warningBox.exec_()
# This creates a warning box if the user didn't input the correct password
except FileNotFoundError:
warningBox = QMessageBox()
warningBox.setIcon(QMessageBox.Critical)
warningBox.setText("You need to enter a valid file path")
warningBox.setWindowTitle("Warning")
warningBox.setStandardButtons(QMessageBox.Ok)
warningBox.exec_()
# This creates a warning box if the user didn't input a valid file path
class PasswordManager(QDialog):
def __init__(self, records, master_password, db_location):
super(PasswordManager, self).__init__()
loadUi("PasswordManagerGuiNew.ui", self)
widget.setFixedWidth(1271)
widget.setFixedHeight(861)
self.tableWidgetPasswords.setColumnWidth(0, 307)
self.tableWidgetPasswords.setColumnWidth(1, 307)
self.tableWidgetPasswords.setColumnWidth(2, 307)
self.tableWidgetPasswords.setColumnWidth(3, 307)
self.pushButtonAdd.clicked.connect(self.open_DialogAdd)
self.pushButtonDelete.clicked.connect(self.open_DialogDelete)
self.pushButtonSearch.clicked.connect(self.search)
self.pushButtonSave.clicked.connect(self.save)
self.pushButtonPasswordGenerator.clicked.connect(self.open_DialogPasswordGenerator)
self.records = records
self.master = master_password
self.db_location = db_location
# print(self.records)
# Load the password manager Ui, and connect all the buttons to the appropriate functions and set the window's width and height
self.conn = sqlite3.connect(":memory:")
self.curs = self.conn.cursor()
self.curs.execute("""CREATE TABLE IF NOT EXISTS passwords (
ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Username TEXT,
Email TEXT,
Password TEXT NOT NULL,
App TEXT NOT NULL
);""")
# Create the sql database and store it in main memory
for record in self.records:
try:
self.curs.execute(f"""INSERT INTO passwords
VALUES (NULL, ?, ?, ?, ?)""", record)
self.conn.commit()
except Exception as e:
print(e)
# For each record, insert it into the database
self.load_data("SELECT * FROM passwords")
# Get all passwords from the database
def load_data(self, query):
self.tableWidgetPasswords.clear()
self.tableWidgetPasswords.setRowCount(len(self.records))
row = 0
for record in self.curs.execute(query):
self.tableWidgetPasswords.setItem(row, 0, QtWidgets.QTableWidgetItem(record[1]))
self.tableWidgetPasswords.setItem(row, 1, QtWidgets.QTableWidgetItem(record[2]))
self.tableWidgetPasswords.setItem(row, 2, QtWidgets.QTableWidgetItem(record[3]))
self.tableWidgetPasswords.setItem(row, 3, QtWidgets.QTableWidgetItem(record[4]))
row += 1
self.tableWidgetPasswords.setRowCount(row)
# Clear the table and add all the records in
def search(self):
keyword = self.lineEditSearchKeywords.text()
if keyword == "":
query = "SELECT * FROM passwords"
else:
query = f"SELECT * FROM passwords WHERE username='{keyword}' OR email='{keyword}' OR app='{keyword}';"
self.load_data(query)
# Get the keyword from user input and use sql query to search for data in the database
def open_DialogAdd(self):
self.dialogAdd = loadUi("dialogAdd.ui")
self.dialogAdd.pushButtonClear.clicked.connect(self.clear_DialogAdd)
self.dialogAdd.pushButtonAdd.clicked.connect(self.add_record)
self.dialogAdd.exec()
# Open the add record ui
def clear_DialogAdd(self):
self.dialogAdd.lineEditUsername.clear()
self.dialogAdd.lineEditEmail.clear()
self.dialogAdd.lineEditPassword.clear()
self.dialogAdd.lineEditApp.clear()
# Clear all of the user input boxes
def add_record(self):
if len(self.dialogAdd.lineEditUsername.text()) == 0:
print("U need to input something in username")
elif len(self.dialogAdd.lineEditEmail.text()) == 0:
print("U need to input something in email")
elif len(self.dialogAdd.lineEditPassword.text()) == 0:
print("U need to input something in password")
elif len(self.dialogAdd.lineEditApp.text()) == 0:
print("U need to input something in app")
# Check if any of the boxes are empty
else:
try:
self.records = self.records + str(
((self.dialogAdd.lineEditUsername.text(), self.dialogAdd.lineEditEmail.text(),
self.dialogAdd.lineEditPassword.text(), self.dialogAdd.lineEditApp.text())))
self.curs.execute(f"""INSERT INTO passwords
VALUES (NULL, ?, ?, ?, ?)""", (
self.dialogAdd.lineEditUsername.text(), self.dialogAdd.lineEditEmail.text(),
self.dialogAdd.lineEditPassword.text(), self.dialogAdd.lineEditApp.text()))
self.conn.commit()
except Exception as e:
print(e)
# Add all of the records into the database
self.load_data("SELECT * FROM passwords")
# Load the database with the new data
def open_DialogDelete(self):
self.dialogDelete = loadUi("dialogDelete.ui")
self.dialogDelete.pushButtonDelete.clicked.connect(self.delete_record)
self.dialogDelete.comboBoxEmail.addItem("...")
self.dialogDelete.comboBoxApp.addItem("...")
# Open the delete record ui
for record in self.curs.execute("SELECT * FROM passwords"):
self.dialogDelete.comboBoxEmail.addItem(record[2])
self.dialogDelete.comboBoxApp.addItem(record[4])
self.dialogDelete.exec()
# Fill the email and app combo boxes with each record
def delete_record(self):
try:
if self.dialogDelete.comboBoxEmail.currentText() == "...":
print("You need to select a value for email")
elif self.dialogDelete.comboBoxApp.currentText() == "...":
print("You need to select a value for app")
# Check if any of the two boxes are empty
else:
self.curs.execute(
f"DELETE FROM passwords WHERE email='{self.dialogDelete.comboBoxEmail.currentText()}' AND app='{self.dialogDelete.comboBoxApp.currentText()}';")
self.conn.commit()
self.load_data("SELECT * FROM passwords")
except Exception as e:
print(e)
# Delete all of the records that match the keywords
def encrypt_db(self):
self.curs.execute("SELECT * FROM passwords")
rows = self.curs.fetchall()
# Get all of the records
with open(self.db_location, "w") as file:
for i in range(len(rows)):
current_tuple = rows[i]
current_tuple = list(current_tuple)
del current_tuple[0]
current_tuple = tuple(current_tuple)
plaintext = ",".join(current_tuple)
# Open the password db text file and get all of the records into an array of tuples
salt = get_random_bytes(16)
key = PBKDF2(self.master, salt, 16, count=1000000, hmac_hash_module=SHA512)
# Create a salt and then create an encryption key with the master password and salt
file.write(f"salt={b64encode(salt)},")
# Encode the salt with base64 and write it to the text file
header = b"Password Number" + str(i).encode()
cipher = AES.new(key, AES.MODE_GCM)
cipher.update(header)
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode())
# Create the header and tag and create the cipher
json_k = ['nonce', 'header', 'ciphertext', 'tag']
json_v = [b64encode(x).decode('utf-8') for x in (cipher.nonce, header, ciphertext, tag)]
result = json.dumps(dict(zip(json_k, json_v)))
# Get the nonce, header, ciphertext and tag into json format
# print(result, "\n")
file.write(result + "\n")
# Write the nonce, header, ciphertext and tag to the file
def password_generator(self):
if self.dialogPasswordGenerator.checkBoxNumbers.isChecked() and self.dialogPasswordGenerator.checkBoxSpecialCharacters.isChecked():
characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
elif self.dialogPasswordGenerator.checkBoxNumbers.isChecked():
characters = list(string.ascii_letters + string.digits)
elif self.dialogPasswordGenerator.checkBoxSpecialCharacters.isChecked():
characters = list(string.ascii_letters + "!@#$%^&*()")
else:
characters = list(string.ascii_letters)
# Get the criteria for what the password needs to include and not include
random.shuffle(characters)
password_list = []
for i in range(self.dialogPasswordGenerator.spinBoxLength.value()):
password_list.append(random.choice(characters))
# Shuffle the list of characters
random.shuffle(password_list)
password = ""
for i in range(len(password_list)):
password += password_list[i]
# Randomly select random characters to make the password
self.dialogPasswordGenerator.lineEditPassword.setText(password)
# Display the password to the ui
def open_DialogPasswordGenerator(self):
self.dialogPasswordGenerator = loadUi("dialogPasswordGenerator.ui")
self.dialogPasswordGenerator.pushButtonGenerate.clicked.connect(self.password_generator)
self.dialogPasswordGenerator.exec()
# Open the password generator ui
def save(self):
self.hide()
self.encrypt_db()
sys.exit()
# The save and close function, 1). Hide the window, 2). Encrypt the database and 3). Exit the program
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
# Run the ui window and create the stacked widget class
welcome = WelcomeScreen()
widget.addWidget(welcome)
widget.setFixedWidth(1149)
widget.setFixedHeight(889)
# Open the window screen and set the fixed width and height
widget.show()
widget.setWindowTitle("Password Manager")
widget.setWindowIcon(QtGui.QIcon("lock.ico"))
# Show the welcome screen with the title and icon
sys.exit(app.exec_())
# Close the program