-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
198 lines (170 loc) · 6.46 KB
/
interface.py
File metadata and controls
198 lines (170 loc) · 6.46 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
'''
Ran Ju Final Project, CSEG 505
This program is the GUI that can generate key,
decode/encode what I what
'''
import sys
from PyQt5.QtWidgets import *
from Crypto import *
class CryptoTool(QWidget):
def __init__(self, parent = None):
super().__init__(parent)
screen = QApplication.desktop().screenGeometry()
x = (screen.width() - self.size().width()) / 2
y = (screen.height() - self.size().height()) / 2
self.x = x
self.y = y
self.move(x, y)
self.setWindowTitle("Crypto Tool")
label_1 = QLabel('Key file:', self)
label_1.setMaximumWidth(100)
label_1.setMinimumWidth(100)
label_2 = QLabel('Source File:', self)
label_2.setMaximumWidth(100)
label_2.setMinimumWidth(100)
self.text_1 = QLineEdit(self)
self.text_2 = QLineEdit(self)
self.text_1.setReadOnly(True)
self.text_2.setReadOnly(True)
self.button_1 = QPushButton("browse", self)
self.button_1.clicked.connect(self.getfiles1)
self.button_2 = QPushButton("browse", self)
self.button_2.clicked.connect(self.getfiles2)
self.button_3 = QPushButton("encode",self)
self.button_3.clicked.connect(self.encode)
self.button_4 = QPushButton("decode", self)
self.button_4.clicked.connect(self.decode)
self.button_5 = QPushButton("generate key",self)
self.button_5.clicked.connect(self.file_save)
self.text_3 = QLineEdit(self)
self.text_3.setText("Key:")
self.text_3.setReadOnly(True)
self.text_4 = QLineEdit(self)
self.text_4.setText("Encrypted/Decryted source text")
self.text_4.setReadOnly(True)
self.content_1 = QTextEdit(self)
self.content_2 = QTextEdit(self)
self.content_1.setReadOnly(True)
self.content_2.setReadOnly(True)
self.content_2.setFontFamily('courier')
self.content_1.setFontFamily('courier')
self.text_5 = QLineEdit(self)
self.text_5.setText("Decryted/Encrted text")
self.key =""
self.text = ""
self.keyfile = ""
self.code = ""
lyt1 = QHBoxLayout()
lyt1.addWidget(label_1)
lyt1.addWidget(self.text_1)
lyt1.addWidget(self.button_1)
lyt2 = QHBoxLayout()
lyt2.addWidget(label_2)
lyt2.addWidget(self.text_2)
lyt2.addWidget(self.button_2)
lyt4 = QHBoxLayout()
lyt4.addWidget(self.button_3)
lyt4.addWidget(self.button_4)
lyt4.addWidget(self.button_5)
lyt3 = QVBoxLayout()
lyt3.addLayout(lyt1)
lyt3.addLayout(lyt2)
lyt3.addLayout(lyt4)
lyt3.addWidget(self.text_3)
lyt3.addWidget(self.text_4)
lyt3.addWidget(self.content_1)
lyt3.addWidget(self.text_5)
lyt3.addWidget(self.content_2)
self.setLayout(lyt3)
self.setMinimumSize(700,400)
def getfiles1(self):
fileName, _ = QFileDialog.getOpenFileName(self, 'Open a File', '', '*.txt')
if fileName == "":
self.text_1.setText("")
else:
self.text_1.setText(fileName)
file = open(fileName, 'r')
self.keyfile = fileName
text = ""
for line in file:
text += line
self.key = text
self.text_3.setText("Key: "+text)
def getfiles2(self):
fileName, _ = QFileDialog.getOpenFileName(self, 'Open a File', '', '*.enc;;*.txt')
if fileName == "":
self.text_2.setText("")
else:
self.text_2.setText(fileName)
self.content_1.setText(open(fileName,'r').read())
#function on decode button
def decode(self):
if self.text_1.text() == "" or self.text_2.text() == "":
reply = QMessageBox.information(self,"Error","Please select the key file and source file")
else:
self.content_1.clear()
fileName = self.text_2.text()
self.code = fileName
file = open(fileName, 'r')
text = ""
for line in file:
text += line
self.text = text
self.content_1.setText(text)
c = Crypto()
c.SetKey(self.key)
infile = open(self.code,'r')
res = ""
fileName, _ = QFileDialog.getSaveFileName(self, 'Save the content', '', '*.txt')
with open(fileName, 'w') as f:
for i in infile:
line = c.UnEncrypt(i)
res += c.UnEncrypt(i)
f.write(res)
self.content_2.setText(res)
# if fileName != "":
# with open(fileName, 'w') as f:
# f.write(res)
#function on encode function
def encode(self):
if self.text_1.text() == "" or self.text_2.text() == "":
reply = QMessageBox.information(self,"Error","Please select the key file and source file")
else:
c = Crypto()
key = self.text_1.text()
keyFile = open(key,'r')
keyString = ""
for i in keyFile:
keyString += i
c.SetKey(i)
filename = self.text_2.text()
file = open(filename,'r')
res = ""
fileName, _ = QFileDialog.getSaveFileName(self, 'Save the content', '', '*.enc')
for j in file:
res += c.Encrypt(j)+"\r"
if fileName != "":
with open(fileName, 'w') as f:
f.write(res)
self.content_2.setText(open(fileName,'r').read())
#print the content of the text
fileName = self.text_2.text()
self.code = fileName
file = open(fileName, 'r')
text = ""
for line in file:
text += line
self.text = text
self.content_1.setText(text)
def file_save(self):
fileName, _ = QFileDialog.getSaveFileName(self, 'Generate a new key file', '', '*.txt')
if fileName != "":
c = Crypto()
res = c.GenerateKey()
with open(fileName,'w') as f:
f.write(res)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = CryptoTool()
mw.show()
sys.exit(app.exec_())