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