发送方数据封装成功

main
ps5antq2e 2 months ago
parent 8cbe3c1084
commit 5844612b05

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import io
# Form implementation generated from reading ui file 'sender_ui.ui'
#
@ -33,6 +34,7 @@ import hashlib
from Crypto.Hash import SHA512
import rsa
import base64
import pickle
#导入加密算法的类
class Ui_MainWindow(object):
def setupUi(self, MainWindow,sender):
@ -130,14 +132,14 @@ class Ui_MainWindow(object):
self.algorithm = self.algorithm_box.currentText()
self.mode = self.mode_box.currentText()
with open(self.file_path, 'rb') as f:
plaintext = f.read()
self.plaintext = f.read()
if self.algorithm=="AES":
if self.mode=="ECB":
# #A随机生成16字节的数字和字母组合的字符串
self.key = ''.join(random.choices(string.ascii_letters + string.digits, k=16)).encode()
mode = AES.MODE_ECB
cryptor = AES.new(self.key, mode)
text = plaintext
text = self.plaintext
length = 16
count = len(text)
add = length - (count % length)
@ -150,7 +152,7 @@ class Ui_MainWindow(object):
self.key = ''.join(random.choices(string.ascii_letters + string.digits, k=16)).encode()
mode = AES.MODE_CBC
cryptor = AES.new(self.key, mode, self.key)#iv = self.key
text = plaintext
text = self.plaintext
length = 16
count = len(text)
add = length - (count % length)
@ -171,6 +173,7 @@ class Ui_MainWindow(object):
sm4Alg.set_key(sm4_key.encode(), sm4.SM4_ENCRYPT)
encrypted_data = sm4Alg.crypt_ecb(file_content)
self.encrypted_text = encrypted_data.hex()
self.textBrowser.append(self.encrypted_text)
elif self.mode=="CBC":
# 生成sm4的密钥和偏移量
sm4_key = func.random_hex(16)
@ -181,28 +184,34 @@ class Ui_MainWindow(object):
sm4Alg.set_key(sm4_key.encode(), sm4.SM4_ENCRYPT)
encrypted_data = sm4Alg.crypt_cbc(sm4_iv.encode(),file_content)
self.encrypted_text = encrypted_data.hex()
self.textBrowser.append(self.encrypted_text)
else:
self.textBrowser.append("mode error")
else:
self.textBrowser.append("algorithm error")
#对称密钥加密
if type(self.key)!=bytes:
enc_key=rsa.encrypt(self.key.encode(),self.public_key_other)
else:
enc_key=rsa.encrypt(self.key,self.public_key_other)
self.textBrowser.append(base64.b64encode(enc_key).decode())
#签名
digest=hashlib.sha256(plaintext)
signature=rsa.sign(digest,self.private_key,'sha256')
signature=rsa.sign(self.plaintext,self.private_key,'SHA-256')
# key = RSA.importKey(self.private_key)
# sha512_hash = hashlib.sha256(plaintext.encode('utf-8')).hexdigest()
# h = SHA512.new(sha512_hash.encode("utf-8"))
# signer = PKCS1_v1_5.new(key)
# signature = b2a_hex(signer.sign(h)).decode("utf-8")
self.textBrowser.append(signature)
self.envelope={"algorithm":self.algorithm,"mode":self.mode,"ciphertext":self.encrypted_text,"key":enc_key,"signature":signature,"publick_key":self.public_key}
# self.textBrowser.append(signature.decode())
self.envelope={"algorithm":self.algorithm,"mode":self.mode,"ciphertext":self.encrypted_text,"key":enc_key,"signature":signature}
print(self.envelope)
self.textBrowser.append("pack successfully")
def send(self):
#发送数据
envelope = json.dumps(self.envelope)
self.sender.send(envelope.encode('utf-8'))
byte_stream=io.BytesIO()
pickle.dump(self.envelope,byte_stream)
byte_data=byte_stream.getvalue()
self.sender.send(b"123")
if __name__ == "__main__":
#连接接收端 准备发送数据

Loading…
Cancel
Save