forked from aajmorgan6/FileEncrypter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
343 lines (285 loc) · 12.5 KB
/
Copy pathgui.py
File metadata and controls
343 lines (285 loc) · 12.5 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
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
import sys
import pathlib
from random import randrange
from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QAction
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import (QApplication, QDial, QGridLayout, QHBoxLayout,
QLabel, QMainWindow, QPushButton, QTabWidget,
QVBoxLayout, QWidget, QFileDialog, QLineEdit, QMessageBox)
from encrypt import make_key, encrypt_file_algo, decrypt_file_algo, write_secret_string, read_secret_string, delete_keychain
class SecureDial():
"""
Is same class from Mast.py
Will be the component for second part of password, with first part being typed
"""
def __init__(self):
super().__init__()
self.r1 = randrange(100)
self.r2 = randrange(100)
self.dial = QDial()
self.dial.setRange(0,99)
self.dial.setSingleStep(0)
self.dial.setPageStep(0)
self.dial.setWrapping(True)
self.dial.setSliderPosition(self.r1)
self.v = (self.r1+self.r2)%100
self.dial.valueChanged.connect(self.dial_value)
self.mouse_press = False
self.label = QLabel()
self.label.setText("****")
self.layout = QVBoxLayout()
self.layout.addWidget(self.dial)
self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.layout.addWidget(self.label)
self.dial.sliderPressed.connect(self.d_pressed)
self.dial.sliderReleased.connect(self.d_released)
def d_pressed(self):
self.label.setText(str(self.v))
def d_released(self):
self.label.setText("****")
def dial_value(self, v):
self.v = (v+self.r2)%100
self.label.setText(str(self.v))
class EncryptWidget(QWidget):
"""
Window to choose file, enter typed password, and then enter on screen password.
Will then change the specific file to something (*.txt.box?) and remove unencrypted file.
"""
def __init__(self, num_dials=4):
super().__init__()
layout = QGridLayout()
row_1 = QHBoxLayout()
self.file_button = QPushButton("Choose File")
self.file_button.clicked.connect(self.launchDialog)
row_1.addWidget(self.file_button)
self.file_label = QLabel()
row_1.addWidget(self.file_label)
row_2 = QHBoxLayout()
self.pass_1_label = QLabel()
self.pass_1_label.setText("Enter Password: ")
row_2.addWidget(self.pass_1_label)
self.pass_1 = QLineEdit()
self.pass_1.setEchoMode(QLineEdit.Password)
row_2.addWidget(self.pass_1)
self.pass_2_label = QLabel()
self.pass_2_label.setText("Renter Password: ")
row_2.addWidget(self.pass_2_label)
self.pass_2 = QLineEdit()
self.pass_2.setEchoMode(QLineEdit.Password)
row_2.addWidget(self.pass_2)
self.pass_match = QLabel()
row_2.addWidget(self.pass_match)
self.num_dials = num_dials
self.dials = [SecureDial() for _ in range(self.num_dials)]
row_3 = QHBoxLayout()
for dial in self.dials:
row_3.addLayout(dial.layout)
row_4 = QHBoxLayout()
self.encrypt_button = QPushButton("Encrypt")
self.encrypt_button.clicked.connect(self.runEncryption)
row_4.addWidget(self.encrypt_button)
layout.addLayout(row_1, 1, 1, 1, 1)
layout.addLayout(row_2, 2, 1, 1, 1)
layout.addLayout(row_3, 3, 1, 1, 1)
layout.addLayout(row_4, 4, 1, 1, 1)
self.setLayout(layout)
def launchDialog(self):
response = QFileDialog.getOpenFileName(
parent=self,
caption="Select a File",
)
self.file_label.setText(str(response[0]))
self.file_path = pathlib.Path(str(response[0]))
self.file_path_str = str(response[0])
def runEncryption(self):
if self.pass_1.text() != self.pass_2.text():
dlg = QMessageBox(self)
dlg.setIcon(QMessageBox.Warning)
dlg.setText("Passwords don't match")
dlg.setStandardButtons(QMessageBox.Ok)
dlg.exec()
else:
value = int("".join([str(dial.v) for dial in self.dials]))
key = make_key(bytes(value), self.pass_1.text())
encrypt_file_algo(self.file_path_str, key)
dlg = QMessageBox(self)
dlg.setWindowTitle("Successfully Encrypted File")
dlg.setText("Successfully Encrypted File")
dlg.setStandardButtons(QMessageBox.Ok)
dlg.exec()
# clear things up
dlg = QMessageBox(self)
dlg.setWindowTitle("Sucessfully Encrypted File")
dlg.setText("Would you like to save password to keychain?")
dlg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
button = dlg.exec()
if button == QMessageBox.Yes:
# Save to keychain?
success = read_secret_string(self.file_path.name)
if success:
dlg = QMessageBox(self)
dlg.setText("File name already saved in keychain.")
dlg.setInformativeText("Would you like to overwrite?")
dlg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
result = dlg.exec()
if result == QMessageBox.Yes:
delete_keychain(self.file_path.name)
write_secret_string(self.file_path.name, f"{self.pass_1.text()},{value}")
self.pass_1.clear()
self.pass_2.clear()
self.file_label.setText("")
# reset dials so next user can't use same presets
for dial in self.dials:
dial.r1 = randrange(100)
dial.r2 = randrange(100)
dial.dial.setSliderPosition(dial.r1)
dial.v = (dial.r1+dial.r2)%100
dial.label.setText("****")
class DecryptWidget(QWidget):
def __init__(self, num_dials=4):
super().__init__()
layout = QGridLayout()
self.key = None
row_1 = QHBoxLayout()
self.file_button = QPushButton("Choose File")
self.file_button.clicked.connect(self.launchDialog)
row_1.addWidget(self.file_button)
self.file_label = QLabel()
row_1.addWidget(self.file_label)
row_2 = QHBoxLayout()
self.pass_1_label = QLabel()
self.pass_1_label.setText("Enter Password: ")
row_2.addWidget(self.pass_1_label)
self.pass_1 = QLineEdit()
self.pass_1.setEchoMode(QLineEdit.Password)
row_2.addWidget(self.pass_1)
self.load_key_btn = QPushButton("Load Password")
self.load_key_btn.clicked.connect(self.loadKey)
row_2.addWidget(self.load_key_btn)
self.num_dials = num_dials
self.dials = [SecureDial() for _ in range(self.num_dials)]
row_3 = QHBoxLayout()
for dial in self.dials:
row_3.addLayout(dial.layout)
row_4 = QHBoxLayout()
self.encrypt_button = QPushButton("Decrypt")
self.encrypt_button.clicked.connect(self.runDecryption)
row_4.addWidget(self.encrypt_button)
layout.addLayout(row_1, 1, 1, 1, 1)
layout.addLayout(row_2, 2, 1, 1, 1)
layout.addLayout(row_3, 3, 1, 1, 1)
layout.addLayout(row_4, 4, 1, 1, 1)
self.setLayout(layout)
def launchDialog(self):
response = QFileDialog.getOpenFileName(
parent=self,
caption="Select a File",
filter="Box files (*.box)"
)
self.file_label.setText(str(response[0]))
self.file_path = pathlib.Path(str(response[0]))
self.file_path_str = str(response[0])
def loadKey(self):
if self.file_label == "":
dlg = QMessageBox(self)
dlg.setWindowTitle("No File Selected")
dlg.setText("Please select a file to decrypt first.")
dlg.setStandardButtons(QMessageBox.Ok)
button = dlg.exec()
else:
dlg = QMessageBox(self)
dlg.setWindowTitle("Loading Password")
dlg.setText(f"Would you like to load password for {self.file_path.name}?")
dlg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
button = dlg.exec()
if button == QMessageBox.Yes:
# Save to keychain?
stored = read_secret_string(self.file_path.name[:-len(".box")])
info = stored.split(",")
password, value = ",".join(info[:-1]), info[-1]
self.key = make_key(bytes(int(value)), password)
self.runDecryption()
def runDecryption(self):
if not self.key:
value = int("".join([str(dial.v) for dial in self.dials]))
self.key = make_key(bytes(value), self.pass_1.text())
success = decrypt_file_algo(self.file_path_str, self.key)
if not success:
dlg = QMessageBox(self)
dlg.setIcon(QMessageBox.Warning)
dlg.setText("Unable to Decrypt File")
dlg.setStandardButtons(QMessageBox.Ok)
dlg.exec()
else:
dlg = QMessageBox(self)
dlg.setText("Successfully Decrypted File")
dlg.setStandardButtons(QMessageBox.Ok)
dlg.exec()
self.file_label.setText("")
# clear things up
self.pass_1.clear()
self.file_label.setText("")
# reset dials so next user can't use same presets
for dial in self.dials:
dial.r1 = randrange(100)
dial.r2 = randrange(100)
dial.dial.setSliderPosition(dial.r1)
dial.v = (dial.r1+dial.r2)%100
dial.label.setText("****")
class WelcomeWidget(QWidget):
def __init__(self):
super().__init__()
self.browser = QWebEngineView()
s = """
<html>
<head>
<style>
</style>
<body style="padding: 20px; text-align: center">
<h1 style="text-align: center;">Welcome to FileEncrypter</h1>
<p>File Encrypter allows you to encypt and decrypt your files in a more secure way.</p>
<p>Simply choose a file and add both a typed password as well as 4 numbers chosen on screen.
</p>
<h2 style="text-align: center; margin-top: 30px">Why FileEncrypter?</h2>
<p>In order to combat keyloggers, both on screen and typed passwords are used to encrypt a file.</p>
<p>No passwords are saved, so be careful about forgetting passwords or typing them in wrong!</p>
<h2 style="text-align: center; margin-top: 30px">Using the Keychain</h2>
<p>Sometimes the best passwords are the hardest to remember, especially when you add in the extra numbers involved.</p>
<p>If you want, save your password to the Apple Keychain for secure storage.</p>
<p>If you are reencrypting a file, you will have the option to overwrite a previously saved password for your new one, but be careful, as that old password will be lost!</p>
</body>
</head>
</html>
"""
self.browser.setHtml(s)
layout = QVBoxLayout()
layout.addWidget(self.browser)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("File Encrypter")
self.setFixedSize(QSize(800, 600))
menu = self.menuBar()
self.encrypt = EncryptWidget()
self.welcome = WelcomeWidget()
self.decrypt = DecryptWidget()
self.tabbed = QTabWidget()
self.tabbed.addTab(self.welcome, "Welcome")
self.tabbed.addTab(self.encrypt, "Encrypt")
self.tabbed.addTab(self.decrypt, "Decrypt")
self.tabbed.setDocumentMode(True)
self.tabbed.setCurrentIndex(0)
file_menu = menu.addMenu("&File")
close_button_action = QAction("Close", self)
close_button_action.triggered.connect(self.clickClose)
file_menu.addAction(close_button_action)
self.setCentralWidget(self.tabbed)
def clickClose(self, s):
print("Click on Close Button", s)
self.close()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()