-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (116 loc) · 4.34 KB
/
Copy pathmain.py
File metadata and controls
152 lines (116 loc) · 4.34 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
import os
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog
from cryptography.fernet import InvalidToken
from ui.mainWindow import Ui_MainWindow
from ui.decryptedTextWindow import Ui_DecryptedWindow
from crypto import basic_encryption, basic_decryption, \
encryption_with_psw, decryption_with_psw
class UIFunctionality(Ui_MainWindow):
def __init__(self) -> None:
super().__init__()
self.setupUi(MainWindow)
self.setupInteraction()
self.readonly_check.setChecked(True)
def setupInteraction(self):
# Buttons
self.psw_encrypt_btn.clicked.connect(self.psw_encrypt_clicker)
self.psw_decrypt_btn.clicked.connect(self.psw_decrypt_clicker)
self.b_encrypt_btn.clicked.connect(self.b_encrypt_clicker)
self.b_decrypt_btn.clicked.connect(self.b_decrypt_clicker)
# File operations
self.file_btn.clicked.connect(self.file_dialog)
# Encryption/Decryption operations
def psw_encrypt_clicker(self):
try:
encryption_with_psw(
self.fname,
self.psw_edit.toPlainText(),
self.overwrite_check.isChecked()
)
self.output_text("Succeed Encryption")
self.reset_state()
except AttributeError:
self.output_text("ERROR: File or password not selected")
def psw_decrypt_clicker(self):
try:
readonly = self.readonly_check.isChecked()
decrypted_content = decryption_with_psw(
self.fname,
self.psw_edit.toPlainText(),
readonly
)
# Show in the other window
if readonly:
decrypted_text_window.showText(decrypted_content.decode('utf-8'))
self.output_text("Succeed Decryption")
self.reset_state()
except AttributeError:
self.output_text("ERROR: File or password not selected")
except InvalidToken:
self.output_text("ERROR: Invalid or Wrong password")
def b_encrypt_clicker(self):
try:
basic_encryption(
self.fname,
self.overwrite_check.isChecked()
)
self.output_text("Succeed Basic Encryption")
self.reset_state()
except AttributeError:
self.output_text("ERROR: File not selected")
def b_decrypt_clicker(self):
try:
readonly = self.readonly_check.isChecked()
decrypted_content = basic_decryption(
self.fname,
readonly
)
if readonly:
decrypted_text_window.showText(decrypted_content.decode('utf-8'))
self.output_text("Succeed Basic Decryption")
self.reset_state()
except AttributeError:
self.output_text("ERROR: File not selected")
# File Operations
def file_dialog(self):
# Create File Dialog.
fname = QFileDialog.getOpenFileName(
MainWindow,
"Select File",
"",
"All Files (*)"
)
# Check if there is a return value.
if os.path.isfile(fname[0]):
self.fname = fname[0]
self.filepath_label.setText(fname[0])
self.filepath_label.adjustSize()
# Various Actions
def output_text(self, text: str):
"""Outputs the text into the output label."""
self.output_label.setText(' >' + text)
def reset_state(self):
"""Resets the general state of the program."""
del self.fname
self.filepath_label.setText("[Selected File]")
self.filepath_label.adjustSize()
self.overwrite_check.setChecked(False)
self.readonly_check.setChecked(False)
class OutputWindow(Ui_DecryptedWindow):
def __init__(self) -> None:
super().__init__()
self.setupUi(DecryptedWindow)
def showText(self, text: str):
"""Shows a text."""
self.textBrowser.setPlainText(text)
DecryptedWindow.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
DecryptedWindow = QtWidgets.QWidget()
ui = UIFunctionality()
decrypted_text_window = OutputWindow()
MainWindow.show()
sys.exit(app.exec_())