-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
207 lines (191 loc) · 9.06 KB
/
gui.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
# -*- coding:utf-8 -*-
import sys, os
from crypter import Fcrypter
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
defdir = os.getcwd()
crypt_key = {'key': ''}
def getOpenFilesAndDirs(parent=None, caption='', directory='', filter='', initialFilter='', options=None):
"""
This function not coded by me.
Source: https://stackoverflow.com/questions/64336575/select-a-file-or-a-folder-in-qfiledialog-pyqt5
"""
def updateText():
# update the contents of the line edit widget with the selected files
selected = []
for index in view.selectionModel().selectedRows():
selected.append('"{}"'.format(index.data()))
lineEdit.setText(' '.join(selected))
dialog = QtWidgets.QFileDialog(parent, windowTitle=caption)
dialog.setFileMode(dialog.ExistingFiles)
if options:
dialog.setOptions(options)
dialog.setOption(dialog.DontUseNativeDialog, True)
if directory:
dialog.setDirectory(directory)
if filter:
dialog.setNameFilter(filter)
if initialFilter:
dialog.selectNameFilter(initialFilter)
# by default, if a directory is opened in file listing mode,
# QFileDialog.accept() shows the contents of that directory, but we
# need to be able to "open" directories as we can do with files, so we
# just override accept() with the default QDialog implementation which
# will just return exec_()
dialog.accept = lambda: QtWidgets.QDialog.accept(dialog)
# there are many item views in a non-native dialog, but the ones displaying
# the actual contents are created inside a QStackedWidget; they are a
# QTreeView and a QListView, and the tree is only used when the
# viewMode is set to QFileDialog.Details, which is not this case
stackedWidget = dialog.findChild(QtWidgets.QStackedWidget)
view = stackedWidget.findChild(QtWidgets.QListView)
view.selectionModel().selectionChanged.connect(updateText)
lineEdit = dialog.findChild(QtWidgets.QLineEdit)
# clear the line edit contents whenever the current directory changes
dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
dialog.exec_()
return dialog.selectedFiles()
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.ui()
def ui(self):
self.window = QtWidgets.QWidget()
self.window.setWindowTitle("4crypter")
self.window.setGeometry(750, 500, 420, 250)
self.hbox = QtWidgets.QHBoxLayout()
self.h_box = QtWidgets.QHBoxLayout()
self.hh = QtWidgets.QHBoxLayout()
self.keytext = QtWidgets.QLabel('Key (max size 32)')
self.hbox.addWidget(self.keytext)
self.keyinput = QtWidgets.QLineEdit()
self.hbox.addWidget(self.keyinput)
self.setbutton = QtWidgets.QPushButton('Set!')
self.processbutton = QtWidgets.QPushButton('Encrypt/Decrypt')
self.selectbutton = QtWidgets.QPushButton('Select file(s)/folder(s)')
self.hbox.addWidget(self.setbutton)
self.cryptgroup = QtWidgets.QButtonGroup(self)
self.askgroup = QtWidgets.QButtonGroup(self)
self.enc, self.dec = QtWidgets.QRadioButton('Encrypt'), QtWidgets.QRadioButton('Decrypt')
self.aes, self.fernet = QtWidgets.QRadioButton('AES-256-CBC'), QtWidgets.QRadioButton('MultiFernet')
self.askgroup.addButton(self.enc)
self.askgroup.addButton(self.dec)
self.cryptgroup.addButton(self.aes)
self.cryptgroup.addButton(self.fernet)
self.aes.setChecked(True)
self.enc.setChecked(True)
self.vbox = QtWidgets.QVBoxLayout()
self.h_box.addWidget(self.enc)
self.h_box.addWidget(self.dec)
self.hh.addWidget(self.aes)
self.hh.addWidget(self.fernet)
self.vbox.addLayout(self.hbox)
self.vbox.addLayout(self.h_box)
self.vbox.addLayout(self.hh)
self.vbox.addStretch()
self.vbox.addWidget(self.selectbutton)
self.vbox.addWidget(self.processbutton)
self.window.setLayout(self.vbox)
self.setbutton.clicked.connect(self.setkey)
self.processbutton.clicked.connect(lambda : self.process(self.enc.isChecked(), self.dec.isChecked(), self.aes.isChecked()))
self.selectbutton.clicked.connect(self.select)
self.window.show()
def setkey(self):
key = self.keyinput.text()
if not len(key) > 32 and key != "":
crypt_key['key'] = key
self.show_popup("Key Info", "Key is setted!", QMessageBox.Information)
print(key, "|", crypt_key['key'])
elif len(key) > 32:
self.show_popup("Key Error", f"Max key length is 32, your key length is {len(key)}", QMessageBox.Critical)
elif key == "":
self.show_popup("Key Error", "Please set a key", QMessageBox.Critical)
def show_popup(self, title, text, icon, buttons=None, defaultbutton=None, details=None, informative=None):
msg = QMessageBox()
msg.setWindowTitle(title)
msg.setText(text)
msg.setIcon(icon)
if not buttons == None:
msg.setStandardButtons(buttons)
if not defaultbutton == None:
msg.setDefaultButton(defaultbutton)
if not details == None:
msg.setDetailedText(details)
if not informative == None:
msg.setInformativeText(informative)
msg.buttonClicked.connect(self.popup_button)
x = msg.exec_()
def select(self):
filesfolders = getOpenFilesAndDirs(self, "Select file(s)/folder(s)", defdir)
files = []
folders = []
for x in filesfolders:
if os.path.isfile(x):
files.append(x)
else:
folders.append(x)
files.sort()
folders.sort()
self.data = {"files": files, "folders": folders}
self.show_popup("Crypt Info", "Selected file(s)/folder(s)!", QMessageBox.Information, buttons=QMessageBox.Ignore|QMessageBox.Ok, defaultbutton=QMessageBox.Ok)
def popup_button(self, button):
self.popupbutton = str(button.text()).lower().lstrip("&")
print(self.popupbutton)
def process(self, enc, dec, aes):
if self.keyinput.text() == "":
self.show_popup("Key Error", "Please set a key.", QMessageBox.Critical)
else:
crypter = Fcrypter(key=crypt_key['key'])
files, folders = self.data['files'], self.data['folders']
file_num = len(files)
folder_num = 0
total = file_num
all_paths = ""
if file_num > 0:
for x in files:
all_paths += x + "\n"
if len(folders) > 0:
for folder in folders:
for root, dirs, files in os.walk(folder):
for file in files:
all_paths += os.path.join(root, file) + "\n"
total += len(files)
folder_num += len(dirs)
if enc:
"Encrypt process"
self.popuptext = f"{total} file(s) will be encrypted."
self.show_popup("Cryption Info", self.popuptext, QMessageBox.Information,
buttons=QMessageBox.No | QMessageBox.Yes, defaultbutton=QMessageBox.No,
details=all_paths, informative="Do you accept?")
if self.popupbutton == 'yes':
_all = all_paths.split('\n')
for x in _all:
if os.path.isfile(x):
crypter.encryptFile(x, aes=aes)
else:
crypter.encryptFolder(x, aes=aes)
self.show_popup("Cryption Info", "Encryption was successfully!", QMessageBox.Information)
else:
self.show_popup("Cryption Info", "Encryption is canceled!", QMessageBox.Information)
elif dec:
"Decrypt process"
self.popuptext = f"{total} file(s) will be decrypted."
self.show_popup("Decrypt Info", self.popuptext, QMessageBox.Information,
buttons=QMessageBox.No | QMessageBox.Yes, defaultbutton=QMessageBox.No,
details=all_paths, informative="Do you accept?")
if self.popupbutton == 'yes':
_all = all_paths.split('\n')
for x in _all:
if os.path.isfile(x):
crypter.decryptFile(x, aes=aes)
else:
crypter.decryptFolder(x, aes=aes)
self.show_popup("Cryption Info", "Decryption was successfully!", QMessageBox.Information)
else:
self.show_popup("Decrypt Info", "Decryption is canceled!", QMessageBox.Information)
else:
self.show_popup(title="Process Error", text="Please select an option (Encrypt or Decrypt)",
icon=QMessageBox.Warning)
app = QtWidgets.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())