-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workers.py
228 lines (206 loc) · 7.43 KB
/
Workers.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
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from block import Block, getModules
import time
import os
import importlib
import sys
class Fetch_Worker(QThread):
def __init__(self):
super().__init__()
def run(self):
...
class Upload_Worker(QThread):
def __init__(self):
super().__init__()
def run(self):
...
class RFID_Worker(QThread):
'''RFID Working Thread Class'''
##### Signal for GUI Slots #####
logsAppendSignal = pyqtSignal(str)
resultSignal = pyqtSignal(str)
finishedSignal = pyqtSignal(int, QThread)
def __init__(self, rfid, tagData=None, op='Read'):
super().__init__()
self.rfid = rfid
self.tagData = tagData
self.op = op
# set the signals for GUI communication
self.rfid.setSignals(self.logsAppendSignal)
@pyqtSlot()
def run(self):
'''The Main Process for the Thread'''
if self.op == 'Read':
stat = self.rfid.readKey()
key = str(self.rfid.getKey())
# print(f"THE KEY #{key}")
# print(type(key))
self.resultSignal.emit(key)
self.finishedSignal.emit(stat, self)
else:
stat = self.rfid.writeKey(self.tagData)
self.finishedSignal.emit(stat, self)
class KeyGen_Worker(QThread):
'''Key Generator Worker Thread Class'''
logsAppendSignal = pyqtSignal(str)
resultSignal = pyqtSignal(str)
finishedSignal = pyqtSignal()
def __init__(self, algo, bit_size):
super().__init__()
self.algo = algo
self.bit_size = bit_size
print("CONSTR")
def run(self) -> str:
# TODO some kind of progress indicator for long
i = 0
while (i != 2):
self.logsAppendSignal.emit(str(i))
time.sleep(0.1)
i += 1
try:
module = getModules()[self.algo]
result = eval(f"module.{self.algo}.generateKey({self.bit_size})")
print(f'Result: {result}')
if eval(f"module.{self.algo}.isAsymmetric()"):
ass_key = str(f'{result[0]}_{result[1]}')
self.resultSignal.emit(ass_key)
# return ass_key
self.finishedSignal.emit()
else:
self.resultSignal.emit(str(result))
self.finishedSignal.emit()
except Exception as e:
print(f'Worker Result {e}')
return result
# self.resultSignal.emit(None)
# self.finishedSignal.emit()
class Cryptor_Worker(QThread):
'''Working Crytor Thread Class'''
logsAppendSignal = pyqtSignal(str)
resultSignal = pyqtSignal(str)
finishedSignal = pyqtSignal()
def __init__(self, size, algo, mode, isEnc, text, key, fpga = None):
'''
Cryptor Worker Constructor
Args:
size (int): bit size of blocks
algo (str): encryption or decryption algorithm
mode (str): mode of block
isEnc (bool): is Encryption, else Decryption
text (str): text to encrypt or decrypt
key (str): algorithm key
"optional"
fpga (FPGA): attach FPGA to algorithm
'''
super().__init__()
self.isEnc = isEnc
self.text = text
print(f'Key {key}')
# print(f'Key type {type(key)}')
print(f'Text {text}')
# print(f'Text type {type(text)}')
module = getModules()[algo]
self.isAss = eval(f"module.{algo}.isAsymmetric()")
if fpga != None:
if self.isAss:
key = key.split('_')
if self.isEnc:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key[0], fpga=fpga)
else:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key[1], fpga=fpga)
else:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key, fpga=fpga)
else: # SORRU SORRY
if self.isAss:
key = key.split('_')
if self.isEnc:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key[0])
else:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key[1])
else:
self.block = Block(blockSize=size,
algo=algo, mode=mode,
isEnc=isEnc, text=text,
key=key)
def run(self):
'''The Main Process for the Thread'''
try:
result = self.block.run()
print(f'Worker Result {result}')
self.resultSignal.emit(result)
# return result
self.finishedSignal.emit()
except Exception as e:
print(f'Worker Result {e}')
# self.resultSignal.emit(None)
# self.finishedSignal.emit()
if __name__ == "__main__":
app = QApplication(sys.argv)
# cryptor = KeyGen_Worker("DES", 64)
# cryptor.start()
# sys.exit(app.exec_())
algo = 'RSA'
# mode = 'ECB'
mode = 'CBC'
size = 16
# key_gen = KeyGen_Worker(algo=algo, bit_size=size)
# key = key_gen.start()
# time.sleep(1)
# key = '744b$ad59_fb$ad59'
# key = '1783$9d37_8b$9d37'
res = getModules()[algo].RSA.generateKey(size) # ascii string
key = str(f'{res[0]}_{res[1]}')
# key = getModules()[algo].RSA.generateKey(size) # ascii string
# key = ('10001$f0c792cb', 'e4f319c1$f0c792cb')
# key = '43123$48443_187$48443'
# key = ('4117$20567', '133$20567')
print(f"\nKEY all {key}")
# message = "RSA ECB"
# message = "E460"
# message = "HI RSA CBC EE46"
message = "RSA CBC"
# fpga = RSA_FPGA()
# message = "Hi"
print("\n-------------(RSA - Enc - ECB)----------------")
block = Cryptor_Worker(size=size, algo=algo, mode=mode,
isEnc=True, text=message, key=key)
# # isEnc=True, text=message, key=key, fpga=fpga)
time.sleep(0.2)
cipher = block.run()
time.sleep(0.2)
print("plainHex is: " + message.encode().hex())
print("key is: " + key.split('_')[0])
print("cipher is: " + cipher)
print("\n-------------(Decryption)----------------")
block2 = Cryptor_Worker(size=size, algo=algo, mode=mode,
isEnc=False, text=cipher, key=key)
# # isEnc=False, text=cipher, key=key, fpga=fpga)
time.sleep(0.2)
orig = block2.run()
time.sleep(0.2)
print("key is: " + key.split('_')[1])
print("originalis: " + orig)
bytesOrig = bytes.fromhex(orig)
plain = bytesOrig.decode("utf-8")
print(plain)
# cryptor
# rsa = __modules__['RSA'].RSA()
# rsa.makeKeyFiles('RSA_demo', 32)
...