-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (149 loc) · 7.4 KB
/
main.py
File metadata and controls
173 lines (149 loc) · 7.4 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
# from PyQt5 import uic
from PyQt5.QtWidgets import *
from qfluentwidgets import FluentIcon
from CBC import CBC
from cracker import Cracker
from two_fold_S_AES import two_fold_S_AES, treble_S_AES
from ui_main import Ui_Form
from S_AES import S_AES
from utils import *
# class Main():
# noinspection PyArgumentList
class Main(QWidget):
def __init__(self):
# 从UI定义中动态加载窗口对象
# self.ui = uic.loadUi("Forms/main.ui")
# 从文件中加载UI定义
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.cracker = None
self.resize(400, 600)
self.ui.rBtn_Bin.setChecked(True)
self.ui.lineEdit_Key.setClearButtonEnabled(True)
self.ui.pBtn_Github.setIcon(FluentIcon.GITHUB)
self.ui.pBtn_Github.clicked.connect(lambda: openWebsite("https://github.com/Loche2/S-AES_Python"))
self.ui.tBtn_Issue.setIcon(FluentIcon.FEEDBACK)
self.ui.tBtn_Issue.setToolTip('提供反馈')
self.ui.tBtn_Issue.setToolTipDuration(-1)
self.ui.tBtn_Issue.clicked.connect(lambda: openWebsite("https://github.com/Loche2/S-AES_Python/issues"))
self.ui.comboBox_Multi_Encrypt.addItems(['一重加密', '双重加密', '三重加密'])
self.ui.comboBox_Multi_Encrypt.setCurrentIndex(0)
self.ui.pBtn_Encrypt.clicked.connect(self.encrypt)
self.ui.pBtn_Decrypt.clicked.connect(self.decrypt)
self.ui.pBtn_Crack.clicked.connect(self.openCracker)
self.ui.sBtn_CBC.checkedChanged.connect(self.off_multi)
def encrypt(self):
self.ui.cypherTextEdit.clear()
key = self.ui.lineEdit_Key.text()
plain_txt = self.ui.plainTextEdit.toPlainText()
if self.ui.rBtn_Str.isChecked():
plain_txt = str2asc(plain_txt)
# 异常处理:
if not plain_txt.strip(): # 使用strip()方法移除前后的空白字符并检查文本是否为空
showErrorInfoBar(self, '明文为空,请输入')
return
if len(plain_txt) % 16 != 0 or not is_bin(plain_txt):
showErrorInfoBar(self, '明文不是16-bit,请查证后再输入')
return
if self.ui.comboBox_Multi_Encrypt.currentText() == '一重加密':
if len(self.ui.lineEdit_Key.text()) != 16 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '一重加密时密钥仅能为16位比特串')
return
if self.ui.sBtn_CBC.isChecked():
CBC_S_AES = CBC(key=key)
cypher_txt = CBC_S_AES.encrypt(plain_txt)
cypher_txt = ''.join(cypher_txt)
self.ui.cypherTextEdit.insertPlainText(cypher_txt)
else:
m_S_AES = S_AES(key=key)
for two_byte in to_16bits(plain_txt):
cypher_txt = m_S_AES.encrypt(two_byte)
self.ui.cypherTextEdit.insertPlainText(cypher_txt)
elif self.ui.comboBox_Multi_Encrypt.currentText() == '双重加密':
if len(self.ui.lineEdit_Key.text()) != 32 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '双重加密时密钥仅能为32位比特串')
return
double_S_AES = two_fold_S_AES(key=key)
for two_byte in to_16bits(plain_txt):
cypher_txt = double_S_AES.two_fold_encrypt(two_byte)
self.ui.cypherTextEdit.insertPlainText(cypher_txt)
elif self.ui.comboBox_Multi_Encrypt.currentText() == '三重加密':
if len(self.ui.lineEdit_Key.text()) != 48 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '三重加密时密钥仅能为48位比特串')
return
triple_S_AES = treble_S_AES(key=key)
for two_byte in to_16bits(plain_txt):
cypher_txt = triple_S_AES.treble_encrypt(two_byte)
self.ui.cypherTextEdit.insertPlainText(cypher_txt)
if self.ui.rBtn_Str.isChecked():
self.ui.cypherTextEdit.setPlainText(
asc2str(self.ui.cypherTextEdit.toPlainText())
)
def decrypt(self):
self.ui.plainTextEdit.clear()
key = self.ui.lineEdit_Key.text()
cypher_txt = self.ui.cypherTextEdit.toPlainText()
if self.ui.rBtn_Str.isChecked():
cypher_txt = str2asc(cypher_txt)
# 异常处理:
if not cypher_txt.strip(): # 使用strip()方法移除前后的空白字符并检查文本是否为空
showErrorInfoBar(self, '密文为空,请输入')
return
if len(cypher_txt) % 16 != 0 or not is_bin(cypher_txt):
print(cypher_txt)
showErrorInfoBar(self, '密文不是16-bit,请查证后再输入')
return
if self.ui.comboBox_Multi_Encrypt.currentText() == '一重加密':
if len(self.ui.lineEdit_Key.text()) != 16 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '一重加密时密钥仅能为16位比特串')
return
if self.ui.sBtn_CBC.isChecked():
CBC_S_AES = CBC(key=key)
plain_text = CBC_S_AES.decrypt(cypher_txt)
plain_text = ''.join(plain_text)
self.ui.plainTextEdit.insertPlainText(plain_text)
else:
m_S_AES = S_AES(key=key)
for two_byte in to_16bits(cypher_txt):
plain_text = m_S_AES.decrypt(two_byte)
self.ui.plainTextEdit.insertPlainText(plain_text)
elif self.ui.comboBox_Multi_Encrypt.currentText() == '双重加密':
if len(self.ui.lineEdit_Key.text()) != 32 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '双重加密时密钥仅能为32位比特串')
return
double_S_AES = two_fold_S_AES(key=key)
for two_byte in to_16bits(cypher_txt):
plain_text = double_S_AES.two_fold_decrypt(two_byte)
self.ui.plainTextEdit.insertPlainText(plain_text)
elif self.ui.comboBox_Multi_Encrypt.currentText() == '三重加密':
if len(self.ui.lineEdit_Key.text()) != 48 or not is_bin(self.ui.lineEdit_Key.text()):
showErrorInfoBar(self, '三重加密时密钥仅能为48位比特串')
return
triple_S_AES = treble_S_AES(key=key)
for two_byte in to_16bits(cypher_txt):
plain_text = triple_S_AES.treble_decrypt(two_byte)
self.ui.plainTextEdit.insertPlainText(plain_text)
if self.ui.rBtn_Str.isChecked():
self.ui.plainTextEdit.setPlainText(
asc2str(self.ui.plainTextEdit.toPlainText())
)
def off_multi(self):
if self.ui.sBtn_CBC.isChecked():
self.ui.comboBox_Multi_Encrypt.setCurrentIndex(0)
self.ui.comboBox_Multi_Encrypt.setDisabled(True)
else:
self.ui.comboBox_Multi_Encrypt.setDisabled(False)
def openCracker(self):
if self.cracker is None: # 仅当Cracker窗口不存在时创建
self.cracker = Cracker()
self.cracker.show()
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
app = QApplication([])
main = Main()
# main.ui.show()
main.show()
app.exec_()