|
|
|
@ -31,6 +31,8 @@ from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM, FILETYPE_ASN1
|
|
|
|
|
from OpenSSL.crypto import dump_privatekey, dump_publickey
|
|
|
|
|
import hashlib
|
|
|
|
|
from Crypto.Hash import SHA512
|
|
|
|
|
import rsa
|
|
|
|
|
import base64
|
|
|
|
|
#导入加密算法的类
|
|
|
|
|
class Ui_MainWindow(object):
|
|
|
|
|
def setupUi(self, MainWindow,sender):
|
|
|
|
@ -89,15 +91,18 @@ class Ui_MainWindow(object):
|
|
|
|
|
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.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_other=crypto.load_publickey(crypto.FILETYPE_PEM,f.read())
|
|
|
|
|
self.public_key_other=rsa.PublicKey.load_pkcs1_openssl_pem(f.read())
|
|
|
|
|
print("self.public_key",self.public_key)
|
|
|
|
|
print("self.private_key",self.private_key)
|
|
|
|
|
print("self.public_key_other",self.public_key_other)
|
|
|
|
|
self.key=""
|
|
|
|
|
self.envelope={}
|
|
|
|
|
#等待接收方连接
|
|
|
|
@ -124,28 +129,28 @@ class Ui_MainWindow(object):
|
|
|
|
|
# 获取用户选择的加密算法
|
|
|
|
|
self.algorithm = self.algorithm_box.currentText()
|
|
|
|
|
self.mode = self.mode_box.currentText()
|
|
|
|
|
with open(self.file_path, 'r') as f:
|
|
|
|
|
with open(self.file_path, 'rb') as f:
|
|
|
|
|
plaintext = f.read()
|
|
|
|
|
# #A随机生成16字节的数字和字母组合的字符串
|
|
|
|
|
with open("aeskey.txt",'wb') as f:
|
|
|
|
|
self.key = ''.join(random.choices(string.ascii_letters + string.digits, k=16)).encode()
|
|
|
|
|
f.write(self.key)
|
|
|
|
|
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.encode("utf-8")
|
|
|
|
|
text = plaintext
|
|
|
|
|
length = 16
|
|
|
|
|
count = len(text)
|
|
|
|
|
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.textBrowser.append(self.encrypted_text)
|
|
|
|
|
elif self.mode=="CBC":
|
|
|
|
|
#A随机生成16字节的数字和字母组合的字符串
|
|
|
|
|
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.encode("utf-8")
|
|
|
|
|
text = plaintext
|
|
|
|
|
length = 16
|
|
|
|
|
count = len(text)
|
|
|
|
|
add = length - (count % length)
|
|
|
|
@ -181,17 +186,16 @@ class Ui_MainWindow(object):
|
|
|
|
|
else:
|
|
|
|
|
self.textBrowser.append("algorithm error")
|
|
|
|
|
#对称密钥加密
|
|
|
|
|
recipient_key = RSA.importKey(self.public_key_other)
|
|
|
|
|
cipher_rsa = PKCS1_OAEP.new(recipient_key)
|
|
|
|
|
enc_key = cipher_rsa.encrypt(self.key)
|
|
|
|
|
enc_key = b2a_hex(enc_key).decode("utf-8")
|
|
|
|
|
self.textBrowser.append(enc_key+"\n")
|
|
|
|
|
enc_key=rsa.encrypt(self.key.encode(),self.public_key_other)
|
|
|
|
|
self.textBrowser.append(base64.b64encode(enc_key).decode())
|
|
|
|
|
#签名
|
|
|
|
|
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")
|
|
|
|
|
digest=hashlib.sha256(plaintext)
|
|
|
|
|
signature=rsa.sign(digest,self.private_key,'sha256')
|
|
|
|
|
# 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("pack successfully")
|
|
|
|
|