From 7fa9ac1018d23b305f572aab288bddceaa938634 Mon Sep 17 00:00:00 2001 From: ps5antq2e <1819276859@qq.com> Date: Tue, 24 Dec 2024 23:02:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=94=9F=E6=88=90=E5=85=AC?= =?UTF-8?q?=E7=A7=81=E9=92=A5=E7=9A=84=E8=84=9A=E6=9C=AC=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E4=B8=8A=E7=94=9F=E6=88=90=E6=96=B0=E6=A0=BC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E5=85=AC=E7=A7=81=E9=92=A5=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- receiver_ui.py | 55 +++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/receiver_ui.py b/receiver_ui.py index 9907e70..5279799 100644 --- a/receiver_ui.py +++ b/receiver_ui.py @@ -23,6 +23,7 @@ import sys import json from gmssl import sm4, func,sm2,sm3 from PyQt5.QtWidgets import QMessageBox,QFileDialog +import rsa #import sm2random from OpenSSL import crypto class Ui_MainWindow(object): @@ -69,6 +70,7 @@ class Ui_MainWindow(object): self.mode = "" self.algorithm = "" self.enc_key = "" + #继承连接实例 self.receiver = receiver #打开文件选择对话框 @@ -76,25 +78,24 @@ class Ui_MainWindow(object): path=QFileDialog.getOpenFileName()[0] #弹出消息框 with open(path, "rb") as f: - self.private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, f.read()) + self.private_key = rsa.PrivateKey.load_pkcs1(f.read(),'DER') QMessageBox.information(MainWindow, "提示", "请选择你的公钥文件") path=QFileDialog.getOpenFileName()[0] with open(path, "rb") as f: - self.public_key = crypto.load_publickey(crypto.FILETYPE_PEM, f.read()) + self.public_key = rsa.PublicKey.load_pkcs1_openssl_pem(f.read()) QMessageBox.information(MainWindow, "提示", "请选择对方的公钥文件") path=QFileDialog.getOpenFileName()[0] with open(path, "rb") as f: - self.public_key_other = crypto.load_publickey(crypto.FILETYPE_PEM, f.read()) + self.public_key_other = rsa.PublicKey.load_pkcs1_openssl_pem(f.read()) self.iv = "" self.encrypted_text = "" self.key="" + self.plaintext='' self.retranslateUi(MainWindow) self.receive_button.clicked.connect(self.receive) # type: ignore self.decrypt_button.clicked.connect(self.decrypt) # type: ignore self.verify_button.clicked.connect(self.verify) # type: ignore QtCore.QMetaObject.connectSlotsByName(MainWindow) - #继承连接实例 - self.client = client def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) @@ -105,7 +106,7 @@ class Ui_MainWindow(object): #接收信封并解封保存,加密后的对称密钥,密文,签名 data = b"" while True: - sender_socket = self.receiver.accept() + sender_socket,addr = self.receiver.accept() envelope_dict = sender_socket.recv(1024) if not envelope_dict: break @@ -124,8 +125,7 @@ class Ui_MainWindow(object): self.client.close() def decrypt_key(self): #使用rsa私钥解密对称密钥self.enc_key,赋给self.key - cipher_rsa = PKCS1_OAEP.new(self.private_key) - self.key = cipher_rsa.decrypt(f.read()) + self.key = rsa.decrypt(self.enc_key, self.private_key) def decrypt(self): #根据模式和加密算法解密 # self.algorithm = self.algorithm_box.currentText() @@ -141,7 +141,7 @@ class Ui_MainWindow(object): self.encrypted_text = f.read() cryptor = AES.new(self.key, self.mode, self.key) - plain_text = cryptor.decrypt(a2b_hex(self.encrypted_text)) + self.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) @@ -151,7 +151,7 @@ class Ui_MainWindow(object): 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)) + self.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) @@ -162,7 +162,7 @@ class Ui_MainWindow(object): if self.mode=="CBC": sm4_crypt = sm4.CryptSM4() sm4_crypt.set_key(self.key, sm4.SM4_DECRYPT) - plaintext=sm4_crypt.crypt_cbc(self.iv,self.encrypted_text) + self.plaintext=sm4_crypt.crypt_cbc(self.iv,self.encrypted_text) save_path= QFileDialog.saveFileContent(MainWindow, "Save File", "default.txt", "All Files (*);;Text Files (*.txt)") with open(save_path, "wb") as f: f.write(plaintext) @@ -170,7 +170,7 @@ class Ui_MainWindow(object): elif self.mode=="ECB": sm4_crypt = sm4.CryptSM4() sm4_crypt.set_key(self.key, sm4.SM4_DECRYPT) - plaintext=sm4_crypt.crypt_ecb(self.encrypted_text) + self.plaintext=sm4_crypt.crypt_ecb(self.encrypted_text) save_path= QFileDialog.saveFileContent(MainWindow, "Save File", "default.txt", "All Files (*);;Text Files (*.txt)") with open(save_path, "wb") as f: f.write(plaintext) @@ -181,19 +181,24 @@ class Ui_MainWindow(object): print("error") def verify(self): #使用对方公钥验证签名 - 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)): + # 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("验证失败") + digest= hashlib.sha256(self.plaintext.encode("utf-8")).digest() + if rsa.verify(self.public_key, digest, self.signature): self.textBrowser.append("验证成功") else: self.textBrowser.append("验证失败")