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("验证失败")