python+pyqt5+Crypto

一、工具

  python3.6、pyqt5、Cryptodomex、pyinstaller

二、运行效果图

  

三、代码

  1 import sys
  2 from PyQt5 import QtCore, QtWidgets
  3 from PyQt5.QtWidgets import QApplication, QWidget
  4 from Cryptodome.Cipher import AES
  5 import hashlib
  6 import base64
  7 import binascii
  8 from PyQt5.QtWidgets import QMessageBox 
  9 
 10 class Ui_Form(QWidget):
 11     def __init__(self):
 12         super().__init__()
 13         self.setupUi()
 14         self.block_size=AES.block_size
 15         self.encodeType=''
 16         
 17     def setupUi(self):
 18         self.setObjectName("Form")
 19         self.resize(614, 300)
 20         self.ENCODE_BTN = QtWidgets.QPushButton(self)
 21         self.ENCODE_BTN.setGeometry(QtCore.QRect(270, 230, 75, 23))
 22         self.ENCODE_BTN.setObjectName("ENCODE_BTN")
 23         self.DECODE_BTN = QtWidgets.QPushButton(self)
 24         self.DECODE_BTN.setGeometry(QtCore.QRect(380, 230, 75, 23))
 25         self.DECODE_BTN.setObjectName("DECODE_BTN")
 26         self.SRC_TXT_UI = QtWidgets.QTextEdit(self)
 27         self.SRC_TXT_UI.setGeometry(QtCore.QRect(40, 20, 531, 71))
 28         self.SRC_TXT_UI.setObjectName("SRC_TXT_UI")
 29         self.DST_TXT_UI = QtWidgets.QTextEdit(self)
 30         self.DST_TXT_UI.setGeometry(QtCore.QRect(40, 110, 531, 71))
 31         self.DST_TXT_UI.setObjectName("DST_TXT_UI")
 32         self.PRI_KEY_UI = QtWidgets.QLineEdit(self)
 33         self.PRI_KEY_UI.setGeometry(QtCore.QRect(90, 220, 113, 20))
 34         self.PRI_KEY_UI.setObjectName("PRI_KEY_UI")
 35         self.label = QtWidgets.QLabel(self)
 36         self.label.setGeometry(QtCore.QRect(30, 220, 54, 12))
 37         self.label.setObjectName("label")
 38         self.ENCODE_TYPE_SELECT = QtWidgets.QComboBox(self)
 39         self.ENCODE_TYPE_SELECT.setGeometry(QtCore.QRect(88, 250, 111, 22))
 40         self.ENCODE_TYPE_SELECT.setObjectName("ENCODE_TYPE_SELECT")
 41         
 42         self.ENCODE_TYPE_SELECT.addItem("请选择");
 43         self.ENCODE_TYPE_SELECT.addItem("AES");
 44         self.ENCODE_TYPE_SELECT.addItem("MD5");
 45         self.ENCODE_TYPE_SELECT.addItem("BASE64");
 46         self.ENCODE_TYPE_SELECT.activated[str].connect(self.onActivated)
 47         
 48         self.label_2 = QtWidgets.QLabel(self)
 49         self.label_2.setGeometry(QtCore.QRect(30, 260, 54, 12))
 50         self.label_2.setObjectName("label_2")
 51 
 52         self.retranslateUi()
 53         self.ENCODE_BTN.clicked.connect(self.selectConnectEncode)
 54         self.DECODE_BTN.clicked.connect(self.selectConnectDecode)
 55         QtCore.QMetaObject.connectSlotsByName(self)
 56         
 57     def selectConnectDecode(self):
 58         if len(self.encodeType)==0:
 59             QMessageBox.information(self, "警告", "请选择加密类型", QMessageBox.Yes)
 60             return False
 61         if self.encodeType=='AES':
 62             self.continueDecode()
 63         elif self.encodeType=='BASE64':
 64             self.contentBase64Decode()
 65         
 66     def selectConnectEncode(self):
 67         if len(self.encodeType)==0:
 68             QMessageBox.information(self, "警告", "请选择加密类型", QMessageBox.Yes)
 69             return False
 70         if self.encodeType=='AES':
 71             self.contentEncode()
 72         elif self.encodeType=='MD5':
 73             self.contentMD5()
 74         elif self.encodeType=='BASE64':
 75             self.contentBase64()
 76             
 77     def onActivated(self,val):
 78         self.encodeType=val
 79         if val=='MD5':
 80             self.DECODE_BTN.setDisabled(True)
 81         else:
 82             self.DECODE_BTN.setDisabled(False)
 83         
 84     def retranslateUi(self):
 85         _translate = QtCore.QCoreApplication.translate
 86         self.setWindowTitle(_translate("Form", "Form"))
 87         self.ENCODE_BTN.setText(_translate("Form", "加密"))
 88         self.DECODE_BTN.setText(_translate("Form", "解密"))
 89         self.label.setText(_translate("Form", "输入秘钥:"))
 90         self.label_2.setText(_translate("Form", "加密类型:"))
 91     
 92     def contentMD5(self):
 93         srcString=self.SRC_TXT_UI.toPlainText()
 94         if len(srcString)==0:
 95             QMessageBox.information(self, "警告", "请输入源字符", QMessageBox.Yes)
 96             return False
 97         md=hashlib.md5(srcString.encode(encoding='utf_8', errors='strict'))
 98         keyencode=md.hexdigest()
 99         self.DST_TXT_UI.setText(keyencode)
100         
101     def contentBase64Decode(self):
102         srcString=self.SRC_TXT_UI.toPlainText()
103         if len(srcString)==0:
104             QMessageBox.information(self, "警告", "请输入源字符", QMessageBox.Yes)
105             return False
106         srcb=srcString.encode(encoding='utf_8')
107         self.DST_TXT_UI.setText(base64.b64decode(srcb).decode(encoding='utf_8'))
108         
109     def contentBase64(self):
110         srcString=self.SRC_TXT_UI.toPlainText()
111         if len(srcString)==0:
112             QMessageBox.information(self, "警告", "请输入源字符", QMessageBox.Yes)
113             return False
114         basesrc=base64.b64encode(srcString.encode(encoding='utf_8'))
115         self.DST_TXT_UI.setText(basesrc.decode(encoding='utf_8'))
116         
117     def contentEncode(self):
118         srcString=self.SRC_TXT_UI.toPlainText()
119         if len(srcString)==0:
120             QMessageBox.information(self, "警告", "请输入源字符", QMessageBox.Yes)
121             return False
122         
123         key=self.PRI_KEY_UI.text()
124         if len(key)==0:
125             QMessageBox.information(self, "警告", "请输入秘钥", QMessageBox.Yes)
126             return False
127         
128         md=hashlib.md5(key.encode(encoding='utf_8', errors='strict'))
129         keyencode=md.hexdigest()
130         self.aes = AES.new(keyencode.encode(), AES.MODE_ECB)
131         basesrc=base64.b64encode(srcString.encode(encoding='utf_8'))
132         count=len(basesrc)
133         length=self.block_size
134         if count%self.block_size !=0:
135             add = length - (count % length) 
136         else:
137             add = 0  
138         basesrc = basesrc + (b' ' * add) 
139         encrypted_text = self.aes.encrypt(basesrc)
140         self.DST_TXT_UI.setText(binascii.b2a_hex(encrypted_text).decode())
141         
142     def continueDecode(self):
143         srcString=self.SRC_TXT_UI.toPlainText()
144         key=self.PRI_KEY_UI.text()
145         
146         if len(srcString)==0:
147             QMessageBox.information(self, "警告", "请输入源字符", QMessageBox.Yes)
148             return False
149         
150         if len(key)==0:
151             QMessageBox.information(self, "警告", "请输入秘钥", QMessageBox.Yes)
152             return False
153         try:
154             md=hashlib.md5(key.encode(encoding='utf_8', errors='strict'))
155             keyencode=md.hexdigest()
156             self.aes = AES.new(keyencode.encode(), AES.MODE_ECB)
157             dststrDec=self.aes.decrypt(binascii.a2b_hex(srcString))
158             dst=base64.b64decode(dststrDec).decode(encoding='utf_8')
159             self.DST_TXT_UI.setText(dst)
160         except:
161             QMessageBox.information(self, "错误", "解密出错", QMessageBox.Yes)
162             return False
163 app = QApplication(sys.argv)
164 ex = Ui_Form()
165 ex.show();
166 sys.exit(app.exec_()) 
posted @ 2018-02-10 10:37  550  阅读(467)  评论(0编辑  收藏  举报