|  |  |  | @ -7,14 +7,23 @@ | 
			
		
	
		
			
				
					|  |  |  |  | # WARNING: Any manual changes made to this file will be lost when pyuic5 is | 
			
		
	
		
			
				
					|  |  |  |  | # run again.  Do not edit this file unless you know what you are doing. | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import base64 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | from PyQt5.QtWidgets import QFileDialog,QMainWindow | 
			
		
	
		
			
				
					|  |  |  |  | import socket | 
			
		
	
		
			
				
					|  |  |  |  | from Crypto.Cipher import AES | 
			
		
	
		
			
				
					|  |  |  |  | from binascii import b2a_hex, a2b_hex | 
			
		
	
		
			
				
					|  |  |  |  | from Crypto.PublicKey import RSA | 
			
		
	
		
			
				
					|  |  |  |  | from Crypto.Cipher import PKCS1_OAEP | 
			
		
	
		
			
				
					|  |  |  |  | from Crypto.Signature import PKCS1_v1_5 | 
			
		
	
		
			
				
					|  |  |  |  | import hashlib | 
			
		
	
		
			
				
					|  |  |  |  | from Crypto.Hash import SHA512 | 
			
		
	
		
			
				
					|  |  |  |  | from PyQt5 import QtCore, QtGui, QtWidgets | 
			
		
	
		
			
				
					|  |  |  |  | import sys | 
			
		
	
		
			
				
					|  |  |  |  | import json | 
			
		
	
		
			
				
					|  |  |  |  | from gmssl import sm4, func,sm2,sm3 | 
			
		
	
		
			
				
					|  |  |  |  | from PyQt5.QtWidgets import QMessageBox,QFileDialog | 
			
		
	
		
			
				
					|  |  |  |  | import socket | 
			
		
	
		
			
				
					|  |  |  |  | import sm2random | 
			
		
	
		
			
				
					|  |  |  |  | #import sm2random | 
			
		
	
		
			
				
					|  |  |  |  | from OpenSSL import crypto | 
			
		
	
		
			
				
					|  |  |  |  | class Ui_MainWindow(object): | 
			
		
	
		
			
				
					|  |  |  |  |     def setupUi(self, MainWindow,client): | 
			
		
	
	
		
			
				
					|  |  |  | @ -60,6 +69,7 @@ class Ui_MainWindow(object): | 
			
		
	
		
			
				
					|  |  |  |  |         self.mode = "" | 
			
		
	
		
			
				
					|  |  |  |  |         self.algorithm = "" | 
			
		
	
		
			
				
					|  |  |  |  |         self.enc_key = "" | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         #打开文件选择对话框 | 
			
		
	
		
			
				
					|  |  |  |  |         QMessageBox.information(MainWindow, "提示", "请选择你的私钥文件") | 
			
		
	
		
			
				
					|  |  |  |  |         path=QFileDialog.getOpenFileName()[0] | 
			
		
	
	
		
			
				
					|  |  |  | @ -110,22 +120,59 @@ class Ui_MainWindow(object): | 
			
		
	
		
			
				
					|  |  |  |  |         self.decrypt_key() | 
			
		
	
		
			
				
					|  |  |  |  |         self.decrypt() | 
			
		
	
		
			
				
					|  |  |  |  |         self.client.close() | 
			
		
	
		
			
				
					|  |  |  |  |     def receive(self): | 
			
		
	
		
			
				
					|  |  |  |  |         #接收信封并解封保存,加密后的对称密钥,密文,签名 | 
			
		
	
		
			
				
					|  |  |  |  |         data = b"" | 
			
		
	
		
			
				
					|  |  |  |  |         while True: | 
			
		
	
		
			
				
					|  |  |  |  |             envelope_dict = self.client.recv(1024) | 
			
		
	
		
			
				
					|  |  |  |  |             if not envelope_dict: | 
			
		
	
		
			
				
					|  |  |  |  |                 break | 
			
		
	
		
			
				
					|  |  |  |  |             data += envelope_dict | 
			
		
	
		
			
				
					|  |  |  |  |             # print(data.decode()) | 
			
		
	
		
			
				
					|  |  |  |  |         envelope_dict = json.loads(data.decode('utf-8')) | 
			
		
	
		
			
				
					|  |  |  |  |         self.signature= envelope_dict['signature'] | 
			
		
	
		
			
				
					|  |  |  |  |         self.encrypted_text = envelope_dict['encrypted_text'] | 
			
		
	
		
			
				
					|  |  |  |  |         self.enc_key = envelope_dict['enc_key'] | 
			
		
	
		
			
				
					|  |  |  |  |         self.mode= envelope_dict['mode'] | 
			
		
	
		
			
				
					|  |  |  |  |         self.algorithm= envelope_dict['algorithm'] | 
			
		
	
		
			
				
					|  |  |  |  |         if self.mode=="CBC": | 
			
		
	
		
			
				
					|  |  |  |  |             self.iv= envelope_dict['iv'] | 
			
		
	
		
			
				
					|  |  |  |  |         self.decrypt_key() | 
			
		
	
		
			
				
					|  |  |  |  |         self.decrypt() | 
			
		
	
		
			
				
					|  |  |  |  |     def decrypt_key(self): | 
			
		
	
		
			
				
					|  |  |  |  |         #使用sm2私钥解密对称密钥self.enc_key,赋给self.key | 
			
		
	
		
			
				
					|  |  |  |  |         sm2_crypt = sm2.CryptSM2(private_key=self.private_key,public_key=self.public_key,) | 
			
		
	
		
			
				
					|  |  |  |  |         try: | 
			
		
	
		
			
				
					|  |  |  |  |             self.key = sm2_crypt.decrypt(self.enc_key) | 
			
		
	
		
			
				
					|  |  |  |  |             print("key:",self.key) | 
			
		
	
		
			
				
					|  |  |  |  |         except Exception as e: | 
			
		
	
		
			
				
					|  |  |  |  |             print(f"[!] Failed to decrypt SM4 key: {e}") | 
			
		
	
		
			
				
					|  |  |  |  |             raise | 
			
		
	
		
			
				
					|  |  |  |  |         #使用rsa私钥解密对称密钥self.enc_key,赋给self.key | 
			
		
	
		
			
				
					|  |  |  |  |         cipher_rsa = PKCS1_OAEP.new(self.private_key) | 
			
		
	
		
			
				
					|  |  |  |  |         self.key = cipher_rsa.decrypt(f.read()) | 
			
		
	
		
			
				
					|  |  |  |  |     def decrypt(self): | 
			
		
	
		
			
				
					|  |  |  |  |         #根据模式和加密算法解密 | 
			
		
	
		
			
				
					|  |  |  |  |         # self.algorithm = self.algorithm_box.currentText() | 
			
		
	
		
			
				
					|  |  |  |  |         # self.mode = self.mode_box.currentText() | 
			
		
	
		
			
				
					|  |  |  |  |         self.algorithm = "AES" | 
			
		
	
		
			
				
					|  |  |  |  |         self.mode = "CBC" | 
			
		
	
		
			
				
					|  |  |  |  |         with open("aeskey.txt",'rb') as f: | 
			
		
	
		
			
				
					|  |  |  |  |             self.key = "1234567890abcdef".encode("utf-8") | 
			
		
	
		
			
				
					|  |  |  |  |         if self.algorithm=="AES": | 
			
		
	
		
			
				
					|  |  |  |  |             if self.mode=="CBC": | 
			
		
	
		
			
				
					|  |  |  |  |                 pass | 
			
		
	
		
			
				
					|  |  |  |  |                 self.mode = AES.MODE_CBC | 
			
		
	
		
			
				
					|  |  |  |  |                 with open("ciphertext.bin", "rb") as f: | 
			
		
	
		
			
				
					|  |  |  |  |                     self.encrypted_text = f.read() | 
			
		
	
		
			
				
					|  |  |  |  |                      | 
			
		
	
		
			
				
					|  |  |  |  |                 cryptor = AES.new(self.key, self.mode, self.key) | 
			
		
	
		
			
				
					|  |  |  |  |                 plain_text = cryptor.decrypt(a2b_hex(self.encrypted_text)) | 
			
		
	
		
			
				
					|  |  |  |  |                 #print(plain_text) | 
			
		
	
		
			
				
					|  |  |  |  |                 plaintext = b2a_hex(plain_text.rstrip(b'\0')).decode("utf-8") | 
			
		
	
		
			
				
					|  |  |  |  |                 self.textBrowser.append(plaintext) | 
			
		
	
		
			
				
					|  |  |  |  |                  | 
			
		
	
		
			
				
					|  |  |  |  |             elif self.mode=="ECB": | 
			
		
	
		
			
				
					|  |  |  |  |                 pass | 
			
		
	
		
			
				
					|  |  |  |  |                 self.mode = AES.MODE_ECB | 
			
		
	
		
			
				
					|  |  |  |  |                 with open("ciphertext.bin", "rb") as f: | 
			
		
	
		
			
				
					|  |  |  |  |                     self.encrypted_text = f.read() | 
			
		
	
		
			
				
					|  |  |  |  |                 cryptor = AES.new(self.key, self.mode) | 
			
		
	
		
			
				
					|  |  |  |  |                 plain_text = cryptor.decrypt(a2b_hex(self.encrypted_text)) | 
			
		
	
		
			
				
					|  |  |  |  |                 #print(plain_text) | 
			
		
	
		
			
				
					|  |  |  |  |                 plaintext = b2a_hex(plain_text.rstrip(b'\0')).decode("utf-8") | 
			
		
	
		
			
				
					|  |  |  |  |                 self.textBrowser.append(plaintext) | 
			
		
	
		
			
				
					|  |  |  |  |                  | 
			
		
	
		
			
				
					|  |  |  |  |             else: | 
			
		
	
		
			
				
					|  |  |  |  |                 print("error") | 
			
		
	
		
			
				
					|  |  |  |  |         elif self.algorithm=="SM4": | 
			
		
	
	
		
			
				
					|  |  |  | @ -150,8 +197,23 @@ class Ui_MainWindow(object): | 
			
		
	
		
			
				
					|  |  |  |  |         else: | 
			
		
	
		
			
				
					|  |  |  |  |             print("error") | 
			
		
	
		
			
				
					|  |  |  |  |     def verify(self): | 
			
		
	
		
			
				
					|  |  |  |  |         pass | 
			
		
	
		
			
				
					|  |  |  |  |         #使用对方公钥验证签名 | 
			
		
	
		
			
				
					|  |  |  |  |         plaintext = """zhangruixiazhangruixiazhangruixia | 
			
		
	
		
			
				
					|  |  |  |  |         helloguet | 
			
		
	
		
			
				
					|  |  |  |  |         hellopython | 
			
		
	
		
			
				
					|  |  |  |  |         helloworld""" | 
			
		
	
		
			
				
					|  |  |  |  |         key = RSA.importKey(self.private_key) | 
			
		
	
		
			
				
					|  |  |  |  |         data = hashlib.sha512(plaintext.encode("utf-8")).hexdigest() | 
			
		
	
		
			
				
					|  |  |  |  |         h = SHA512.new(data.encode("utf-8")) | 
			
		
	
		
			
				
					|  |  |  |  |         #print(h) | 
			
		
	
		
			
				
					|  |  |  |  |         verifier = PKCS1_v1_5.new(key) | 
			
		
	
		
			
				
					|  |  |  |  |         with open("digitalsign.bin", "rb") as f: | 
			
		
	
		
			
				
					|  |  |  |  |             signature = f.read() | 
			
		
	
		
			
				
					|  |  |  |  |         signature = signature.decode("utf-8") | 
			
		
	
		
			
				
					|  |  |  |  |         if verifier.verify(h, base64.b64decode(signature)): | 
			
		
	
		
			
				
					|  |  |  |  |             self.textBrowser.append("验证成功") | 
			
		
	
		
			
				
					|  |  |  |  |         else: | 
			
		
	
		
			
				
					|  |  |  |  |             self.textBrowser.append("验证失败") | 
			
		
	
		
			
				
					|  |  |  |  | if  __name__ == "__main__": | 
			
		
	
		
			
				
					|  |  |  |  |     app = QtWidgets.QApplication(sys.argv) | 
			
		
	
		
			
				
					|  |  |  |  |     window = QtWidgets.QMainWindow() | 
			
		
	
	
		
			
				
					|  |  |  | 
 |