|
|
|
@ -34,6 +34,7 @@ import hashlib
|
|
|
|
|
from Crypto.Hash import SHA512
|
|
|
|
|
import rsa
|
|
|
|
|
import base64
|
|
|
|
|
import threading
|
|
|
|
|
import pickle
|
|
|
|
|
#导入加密算法的类
|
|
|
|
|
class Ui_MainWindow(object):
|
|
|
|
@ -71,7 +72,7 @@ class Ui_MainWindow(object):
|
|
|
|
|
self.retranslateUi(MainWindow)
|
|
|
|
|
self.choose_button.clicked.connect(self.choose_file) # type: ignore
|
|
|
|
|
self.enbutton.clicked.connect(self.encrypt) # type: ignore
|
|
|
|
|
self.envelope_button.clicked.connect(self.thread_create) # type: ignore
|
|
|
|
|
self.envelope_button.clicked.connect(self.send) # type: ignore
|
|
|
|
|
QtCore.QMetaObject.connectSlotsByName(MainWindow)
|
|
|
|
|
#新增属性
|
|
|
|
|
"""
|
|
|
|
@ -107,6 +108,7 @@ class Ui_MainWindow(object):
|
|
|
|
|
print("self.public_key_other",self.public_key_other)
|
|
|
|
|
self.key=""
|
|
|
|
|
self.envelope={}
|
|
|
|
|
self.iv=""
|
|
|
|
|
#等待接收方连接
|
|
|
|
|
self.sender=sender
|
|
|
|
|
def retranslateUi(self, MainWindow):
|
|
|
|
@ -145,8 +147,8 @@ class Ui_MainWindow(object):
|
|
|
|
|
add = length - (count % length)
|
|
|
|
|
text = text + (b'\0' * add)
|
|
|
|
|
encrypted_text = cryptor.encrypt(text)
|
|
|
|
|
self.encrypted_text = b2a_hex(encrypted_text).decode("utf-8")
|
|
|
|
|
self.textBrowser.append(self.encrypted_text)
|
|
|
|
|
self.encrypted_text = b2a_hex(encrypted_text)
|
|
|
|
|
self.textBrowser.append(self.encrypted_text.decode())
|
|
|
|
|
elif self.mode=="CBC":
|
|
|
|
|
#A随机生成16字节的数字和字母组合的字符串
|
|
|
|
|
self.key = ''.join(random.choices(string.ascii_letters + string.digits, k=16)).encode()
|
|
|
|
@ -158,31 +160,33 @@ class Ui_MainWindow(object):
|
|
|
|
|
add = length - (count % length)
|
|
|
|
|
text = text + (b'\0' * add)
|
|
|
|
|
encrypted_text = cryptor.encrypt(text)
|
|
|
|
|
self.encrypted_text = b2a_hex(encrypted_text).decode("utf-8")
|
|
|
|
|
self.textBrowser.append(encrypted_text.decode())
|
|
|
|
|
self.encrypted_text = b2a_hex(encrypted_text)
|
|
|
|
|
self.envelope['iv']=self.key
|
|
|
|
|
self.textBrowser.append(self.encrypted_text.decode())
|
|
|
|
|
else:
|
|
|
|
|
self.textBrowser.append("mode error")
|
|
|
|
|
elif self.algorithm=="SM4":
|
|
|
|
|
if self.mode=="ECB":
|
|
|
|
|
# 生成sm4的密钥和偏移量
|
|
|
|
|
sm4_key = func.random_hex(16)
|
|
|
|
|
self.key = func.random_hex(16)
|
|
|
|
|
with open(self.file_path, 'rb') as f:
|
|
|
|
|
file_content = f.read()
|
|
|
|
|
print("file_content:",file_content)
|
|
|
|
|
sm4Alg = sm4.CryptSM4()
|
|
|
|
|
sm4Alg.set_key(sm4_key.encode(), sm4.SM4_ENCRYPT)
|
|
|
|
|
sm4Alg.set_key(self.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)
|
|
|
|
|
sm4_iv = func.random_hex(16)
|
|
|
|
|
self.key = func.random_hex(16)
|
|
|
|
|
self.iv = func.random_hex(16)
|
|
|
|
|
print(self.iv)
|
|
|
|
|
with open(self.file_path, 'rb') as f:
|
|
|
|
|
file_content = f.read()
|
|
|
|
|
sm4Alg = sm4.CryptSM4()
|
|
|
|
|
sm4Alg.set_key(sm4_key.encode(), sm4.SM4_ENCRYPT)
|
|
|
|
|
encrypted_data = sm4Alg.crypt_cbc(sm4_iv.encode(),file_content)
|
|
|
|
|
sm4Alg.set_key(self.key.encode(), sm4.SM4_ENCRYPT)
|
|
|
|
|
encrypted_data = sm4Alg.crypt_cbc(self.iv.encode(),file_content)
|
|
|
|
|
self.encrypted_text = encrypted_data.hex()
|
|
|
|
|
self.textBrowser.append(self.encrypted_text)
|
|
|
|
|
else:
|
|
|
|
@ -197,25 +201,23 @@ class Ui_MainWindow(object):
|
|
|
|
|
self.textBrowser.append(base64.b64encode(enc_key).decode())
|
|
|
|
|
#签名
|
|
|
|
|
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.decode())
|
|
|
|
|
self.envelope={"algorithm":self.algorithm,"mode":self.mode,"ciphertext":self.encrypted_text,"key":enc_key,"signature":signature}
|
|
|
|
|
print(self.envelope)
|
|
|
|
|
self.envelope={"algorithm":self.algorithm,"mode":self.mode,"ciphertext":self.encrypted_text,"key":enc_key,"signature":signature,"iv":self.iv}
|
|
|
|
|
print(self.envelope["iv"])
|
|
|
|
|
|
|
|
|
|
self.textBrowser.append("pack successfully")
|
|
|
|
|
def send(self):
|
|
|
|
|
# 发送数据
|
|
|
|
|
# self.sender=sender=sender_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self.socketvalue,addr = self.sender.accept()
|
|
|
|
|
while True:
|
|
|
|
|
envelope = json.dumps(self.envelope)
|
|
|
|
|
self.socketvalue.sendall(envelope.encode('utf-8'))
|
|
|
|
|
|
|
|
|
|
def thread_create(self):
|
|
|
|
|
self.send_data = threading.Thread(target=self.send)
|
|
|
|
|
self.send_data.start()
|
|
|
|
|
print("已连接到",addr)
|
|
|
|
|
#while True:
|
|
|
|
|
# 发送数据
|
|
|
|
|
byte_stream = io.BytesIO()
|
|
|
|
|
pickle.dump(self.envelope, byte_stream)
|
|
|
|
|
print(self.key)
|
|
|
|
|
byte_data = byte_stream.getvalue()
|
|
|
|
|
self.socketvalue.sendall(byte_data)
|
|
|
|
|
# self.socketvalue.close()
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# 发送方建立连接 等待接收方连接
|
|
|
|
|