本次实验实现了DH原理的双机通信。DH_Client和DH_Server的main部分与call_DH部分大同小异,主要是用于本地调试显示,可以忽略。
p,q为公有信息。
1.Alice 选择一个范围在[1, p-1]的随机数,为rand_a
2.Alice 计算cal_a = q^rand_a mod p
3.Bob选择一个范围在[1, p-1]的随机数,为rand_b = 6
4.Bob计算cal_b = q^rand_b mod p
5.Alice和Bob交换cal_a和cal_b
6.Alice计算共享密钥S = cal_b ^rand_a mod p
7.Bob计算共享密钥S = cal_a ^rand_b mod p
# -*- coding:utf-8 -*-
import rsa
import binascii
import os
#快速模幂运算,模重复平方
def powmod(b,n,m):
ans=1
while n > 0:
if (n & 1) !=0:
ans = (ans*b)%m
b = (b*b) % m
n >>= 1
return ans
def bytes_to_int(s):
return int.from_bytes(s,byteorder='big',signed=False)
#rsa类
class rsa_crypto():
#生成RSA密钥对同时返回公钥
def generate_key(self,dir):
list=rsa.newkeys(1024)
list=str(list)
with open(dir, 'w') as f:
f.write(list)
list=list.split(',')
n=str(list[0][11:])
pubkey = str(list[1][:-1])
return n,pubkey
#私钥加密 C=M^d mod n
def rsa_encrypt(self,s,dir):
with open(dir, 'r') as f:
temp = f.read()
ne = temp.split(',')
# print("ne",ne)
n = int(ne[0][11:])
d = int(ne[4])
string =binascii.b2a_hex(s.encode())
intstr = bytes_to_int(string)
cryptot = ("%x"%powmod(intstr,d,n))
if len(cryptot)%2 == 1:
cryptot = '0'+cryptot
return binascii.a2b_hex(cryptot)
#解密 M=C^d mod n
def rsa_decrypt(self,string,n,e):
intstr = bytes_to_int(string)
content = ("%x"%powmod(intstr,e,n))
if len(content)%2 == 1:
content = '0' + content
return binascii.a2b_hex(content)
# -*- coding: UTF-8 -*-
from socket import *
#保存client与server通信时的公有量
class connect():
#发送连接请求
def client(self,C_ip,C_port):
# 绑定对象服务器
HOST = C_ip#192.168.43.198
PORT=C_port
ADDR = (HOST, PORT)
# 面向网络的TCP数据报流
global tcpCliSock, tcpSerSock
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
print("client connects successfully")
#服务端等待连接
def server(self,S_ip,S_port):
HOST = S_ip#192.168.43.100
PORT = S_port
ADDR = (HOST,PORT)
global tcpCliSock,tcpSerSock
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
tcpCliSock, addr =tcpSerSock.accept()
print("server is connected successfully")
#发送,报文需提前写好且格式为bytes
def send_data(self,pack):
tcpCliSock.send(pack)
print("send successfully")
#接受报文
def getdata(self):
while 1:
data = tcpCliSock.recv(1024)
if data!=b'':
print("RECEIEVE", data)
#print("flag_get3\n",tcp.flag)
return data
# -*- coding: UTF-8 -*-
import rsa
import random
import connection
import HASH,DES
import RSA_sign
import binascii
class DH_C:
def __init__(self,length=1024):
self.length=length
return
#得到一个大素数p,从rsa方法内选择,且不与下加密签名内相同
def get_prime(self,length):
prime=rsa.newkeys(length)[1]
string= str(prime).split(',')
p = int(string[3][1:])
print("p")
print(p)
return p
#生成本原元,但是由于基数过大,所以本实验默认为2
def get_generator(self,p):
if p == 0:
return
a = 2
while 1:
if a ** (p - 1)% p== 1:
n = 2
mark = 0
while n < p - 1:
if a ** n % p == 1:
mark = 1
n += 1
if mark == 0:
print(a)
return a
a += 1
#得到随机数rand_A
def get_random_a(self,p):
rand_A = random.randint(0, p - 1)
print("rand_A\n",rand_A)
return rand_A
#计算Client的cal_A
def cal_A(self,g,random_A,p):
cal_A=pow(g,random_A,p)
print("cal_A\n",cal_A)
return cal_A
#进行公有信息打包,利用space分割,便于传输
def pre_pack(self, p, g,C_n, C_pubkey):
pack = bytes(str(p) + 'space' + str(g) + 'space' + str(C_n) + 'space' +str(C_pubkey) + 'space' + 'hello' + 'space', 'utf-8')
return pack
#进行加密信息打包,source_C为附加的HASH验证
def pack(self, A, source_C):
pack = bytes(str(A) + 'space', 'utf-8') + source_C
return pack
#解析Server发来的B,S_Key与对应的两个签名
def pre_prase(self, data):
string = data.split(b'space')
S_pubkey = int(string[0])
S_n=int(string[1])
message = string[2]
print("S_pubkey\n", S_pubkey)
print("S_n\n", S_n)
print("message\n", message)
return S_pubkey,S_n, message
def data_parse(self,data):
string = data.split(b'space')
B = int(string[0])
source_B=string[1]
print("B\n", B)
print("source_B\n",source_B)
return B,source_B
#计算共享密钥
def C_get_Key(self,g,A,p):
C_Key= pow(g, A, p);
print("C_key\n",C_Key)
with open('./key/DH_C_key.txt', 'w') as f:
f.write(str(C_Key))
return C_Key
if __name__ == '__main__':
dh_c = DH_C(256)
share=connection.connect()
hash=HASH.HASH()
C=RSA_sign.rsa_crypto()
share.client('127.0.0.1',21567)
#发送公有的数据
p = dh_c.get_prime(dh_c.length)
g = 2
C_n,C_pubkey=C.generate_key('./key/DH_C_signkey.txt')
pack=dh_c.pre_pack(p,g,C_n,C_pubkey)
share.send_data(pack)
#解析传递的公有数据
data=share.getdata()
(S_pubkey,S_n,message)=dh_c.pre_prase(data)
rand_A = dh_c.get_random_a(p)
A = dh_c.cal_A(g, rand_A, p)
complete=hash.SHA_1(str(A))
source_C=C.rsa_encrypt(complete,'./key/DH_C_signkey.txt')
#传输加密后的数据
pack=dh_c.pack(A,source_C)
share.send_data(pack)
#接受并解析加密后的数据
data = share.getdata()
(B,source_B)=dh_c.data_parse(data)
#解密签名并计算获得值的HASH,比对正确说明无丢失来源正确,可计算得到共享密钥
complete_B=hash.SHA_1(str(B))
cal_source_B=C.rsa_decrypt(source_B,S_n,S_pubkey)
cal_source_B= binascii.a2b_hex(cal_source_B).decode()
if (complete_B==cal_source_B):
C_Key = dh_c.C_get_Key(B, rand_A, p)
with open('./key/DH_C_key.txt', 'w') as f:
f.write(str(C_Key))
#使用商量好的密钥对称加密
des=DES.des()
des_key=str(C_Key)[0:8]
with open('./key/DH_Sec_C.txt', 'w') as f2:
f2.write(des_key)
des.encryptDes('./input/send_message.txt', './key/DH_Sec_C.txt', './output/des_encrypt_message.txt')
# -*- coding: UTF-8 -*-
import random
import connection
import HASH,DES
import RSA_sign
import binascii
class DH_S:
def __init__(self):
return
#得到随机数rand_b
def get_random_b(self,p):
rand_B = random.randint(0, p-1)
print("rand_B",rand_B)
return rand_B
#解析公有的信息内容
def pre_parse(self, data):
string = data.split(b'space')
p = int(string[0])
g = int(string[1])
C_n=int(string[2])
C_pubkey=int(string[3])
message=string[4]
print("p\n", p)
print("g\n", g)
print("C_n\n",type(C_n),C_n)
print("C_pubkey\n", C_pubkey)
print("message\n", message)
return p, g, C_n,C_pubkey, message
#解析含hash的加密信息
def data_parse(self, data):
string = data.split(b'space')
A = int(string[0])
signature = string[1]
print("A\n", A)
print("signature\n", signature)
return A, signature
#计算B与S_Key
def cal_B(self, g, random_B, p):
cal_B = pow(g, random_B, p);
print("cal_B", cal_B)
return cal_B
def S_get_Key(self, A, rand_B, p):
S_Key = pow(int(A), rand_B, p)
print("S_key", S_Key)
with open('./key/DH_S_key.txt', 'w') as f:
f.write(str(S_Key))
return S_Key
#打包公有信息
def pre_pack(self, S_pubkey,S_n, message):
pack = bytes(str(S_pubkey) + 'space'+ str(S_n)+ 'space', 'utf-8') + message
return pack
#打包加密信息
def pack(self,B,source_B):
pack =bytes(str(B)+'space','utf-8')+source_B
return pack
if __name__ == '__main__':
share = connection.connect()
dh_s = DH_S()
hash = HASH.HASH()
S = RSA_sign.rsa_crypto()
#循环等待实现多次认证
while 1:
share.server('127.0.0.1',21567)
#接受公有信息
data=share.getdata()
(p, g, C_n,C_pubkey,message) = dh_s.pre_parse(data)
S_n,S_pubkey = S.generate_key('./key/DH_S_signkey.txt')
pack=dh_s.pre_pack(S_pubkey,S_n,message)
share.send_data(pack)
#接受加密的信息
data = share.getdata()
(A,signature)=dh_s.data_parse(data)
orgin_source = hash.SHA_1(str(A))
complete =S.rsa_decrypt(signature,C_n,C_pubkey)
print(complete)
cal_source=(binascii.a2b_hex(complete).decode())
print("orgin_source\n",orgin_source)
#若验证了得到的共享内容正确
if cal_source==orgin_source:
#发送加密信息
print("it is a right pack")
rand_B = dh_s.get_random_b(p)
B = dh_s.cal_B(g, rand_B, p)
S_Key = dh_s.S_get_Key(A, rand_B, p)
complete_B = hash.SHA_1(str(B))
source_B= S.rsa_encrypt(complete_B,'./key/DH_S_signkey.txt')
pack=dh_s.pack(B,source_B)
share.send_data(pack)
with open('./key/DH_S_key.txt', 'w') as f:
f.write(str(S_Key))
#利用得到的对称密钥加密
des=DES.des()
des_key=str(S_Key)[0:8]
with open('./key/DH_Sec_S.txt','w') as f2:
f2.write(des_key)
des.decryptDes('./output/des_encrypt_message.txt','./key/DH_Sec_S.txt', './output/des_decrypt_message.txt')
# -*- coding: utf-8 -*-
import sys,binascii
import DES,RSA_sign,DH_Client,DH_Server,connection,HASH
from PyQt5.QtWidgets import QApplication, QMainWindow
#导入designer工具生成的login模块
from DH import Ui_MainWindow
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.button_begin.clicked.connect(self.run_begin)
self.button_client.clicked.connect(self.run_button_client)
self.button_server.clicked.connect(self.run_button_server)
self.button_client_connect.clicked.connect(self.run_button_client_connect)
self.button_server_connect.clicked.connect(self.run_button_server_connect)
self.button_pre_C_send.clicked.connect(self.run_button_pre_C_send)
self.button_pre_S_send.clicked.connect(self.run_button_pre_S_send)
self.button_C_send.clicked.connect(self.run_button_C_send)
self.button_S_send.clicked.connect(self.run_button_S_send)
self.button_S_getkey.clicked.connect(self.run_button_S_getkey)
self.button_C_getkey.clicked.connect(self.run_button_C_getkey)
self.button_sec_C.clicked.connect(self.run_button_sec_C)
self.button_sec_S.clicked.connect(self.run_button_sec_S)
def run_begin(self):
global share, dh_c, dh_s, hash, C, S, des
share = connection.connect()
dh_c = DH_Client.DH_C(48)#控制DH生成密钥的长度,但是有一定误差
dh_s = DH_Server.DH_S()
hash = HASH.HASH()
C = RSA_sign.rsa_crypto()
S = RSA_sign.rsa_crypto()
des=DES.des()
self.show_config.setText("设置成功")
def run_button_client(self):
self.button_server.setEnabled(False)
self.button_S_send.setEnabled(False)
self.button_S_getkey.setEnabled(False)
self.button_server_connect.setEnabled(False)
self.show_S_send.setEnabled(False)
self.output_S_key.setEnabled(False)
self.output_pre_S_send.setEnabled(False)
self.output_sec_S.setEnabled(False)
self.button_pre_S_send.setEnabled(False)
self.button_sec_S.setEnabled(False)
self.label_ip.setText("请输入通信对象IP:")
def run_button_server(self):
self.button_client.setEnabled(False)
self.button_C_send.setEnabled(False)
self.button_C_getkey.setEnabled(False)
self.button_client_connect.setEnabled(False)
self.show_C_send.setEnabled(False)
self.output_C_key.setEnabled(False)
self.output_pre_C_send.setEnabled(False)
self.output_sec_C.setEnabled(False)
self.button_pre_C_send.setEnabled(False)
self.button_sec_C.setEnabled(False)
self.label_ip.setText("请输入本机IP:")
def run_button_server_connect(self):
ip = self.input_ip.text()
port = int(self.input_port.text())
share.server(ip, port)
self.show_connect.setText("连接成功,为Server端")
def run_button_client_connect(self):
ip = self.input_ip.text()
port = int(self.input_port.text())
share.client(ip, port)
self.show_connect.setText("连接成功,为Client端")
def run_button_pre_C_send(self):
global p_A,g_A,C_pubkey
p_A = dh_c.get_prime(512)
self.output_pre_C_send.setText("p生成完毕:\n")
self.output_pre_C_send.append(str(p_A))
g_A = 2
self.output_pre_C_send.append("g生成完毕:\n")
self.output_pre_C_send.append(str(g_A))
C_n, C_pubkey = C.generate_key('./key/DH_C_signkey.txt')
self.output_pre_C_send.append("Client端RSA公私钥对生成完毕,输出C_n:\n")
self.output_pre_C_send.append(str(C_n))
self.output_pre_C_send.append("输出C_pubkey:\n")
self.output_pre_C_send.append(str(C_pubkey))
pack = dh_c.pre_pack(p_A, g_A, C_n, C_pubkey)
self.output_pre_C_send.setText("打包p,g,C_n,C_pubkey完成并发送hello:\n")
self.output_pre_C_send.append(str(pack))
share.send_data(pack)
self.output_pre_C_send.append("发送包成功")
def run_button_pre_S_send(self):
global C_n,C_pubkey,p_B,g_B
data = share.getdata()
self.output_pre_S_send.setText("接收到包:\n")
(p_B, g_B, C_n, C_pubkey, message) = dh_s.pre_parse(data)
self.output_pre_S_send.append("获得p:\n")
self.output_pre_S_send.append(str(p_B))
self.output_pre_S_send.append("获得g:\n")
self.output_pre_S_send.append(str(g_B))
self.output_pre_S_send.append("获得C_n:\n")
self.output_pre_S_send.append(str(C_n))
self.output_pre_S_send.append("获得C_pubkey:\n")
self.output_pre_S_send.append(str(C_pubkey))
S_n, S_pubkey = S.generate_key('./key/DH_S_signkey.txt')
self.output_pre_S_send.append("Server端RSA公私钥对生成完毕,输出S_n:\n")
self.output_pre_S_send.append(str(S_n))
self.output_pre_S_send.append("输出S_pubkey:\n")
self.output_pre_S_send.append(str(S_pubkey))
pack = dh_s.pre_pack(S_pubkey, S_n, message)
self.output_pre_S_send.append("打包S_n,S_pubkey完毕并回复原消息:\n")
self.output_pre_S_send.append(str(pack))
share.send_data(pack)
self.output_pre_S_send.append("包发送完毕\n")
def run_button_C_send(self):
global rand_A,p_A,S_pubkey,S_n
data = share.getdata()
self.show_C_send.setText("接受到包:\n")
(S_pubkey, S_n, message) = dh_c.pre_prase(data)
self.show_C_send.append("获得S_n:\n")
self.show_C_send.append(str(S_n))
self.show_C_send.append("获得S_pubkey:\n")
self.show_C_send.append(str(S_pubkey))
rand_A = dh_c.get_random_a(p_A)
self.show_C_send.append("生成随机元rand_A:\n")
self.show_C_send.append(str(rand_A))
A = dh_c.cal_A(g_A, rand_A, p_A)
self.show_C_send.append("计算获得A:\n")
self.show_C_send.append(str(A))
complete = hash.SHA_1(str(A))
self.show_C_send.append("对A生成HASH摘要:\n")
self.show_C_send.append(str(complete))
source_C = C.rsa_encrypt(complete, './key/DH_C_signkey.txt')
self.show_C_send.append("对A的摘要使用Client的私钥进行签名:\n")
self.show_C_send.append(str(source_C))
pack = dh_c.pack(A, source_C)
self.show_C_send.append("打包A与A的数字签名完毕\n")
self.show_C_send.append(str(pack))
share.send_data(pack)
self.show_C_send.append("发送包成功\n")
def run_button_S_send(self):
global A,rand_B
data = share.getdata()
self.show_S_send.setText("收到包\n")
(A, signature_A) = dh_s.data_parse(data)
self.show_S_send.append("A:\n")
self.show_S_send.append(str(A))
self.show_S_send.append("A的数字签名:\n")
self.show_S_send.append(str(signature_A))
complete_A = hash.SHA_1(str(A))
self.show_S_send.append("本地计算A的HASH值为\n")
self.show_S_send.append(str(complete_A))
source_A = S.rsa_decrypt(signature_A, C_n, C_pubkey)
source_A = binascii.a2b_hex(source_A).decode()
self.show_S_send.append("使用Server的私钥解签A的HASH值为:\n")
self.show_S_send.append(str(source_A))
# 若验证了得到的共享内容正确
if complete_A == source_A:
self.show_S_send.append("A的完整性与来源性验证正确\n")
rand_B = dh_s.get_random_b(p_B)
self.show_S_send.append("生成随机元rand_B:\n")
self.show_S_send.append(str(rand_B))
B = dh_s.cal_B(g_B, rand_B, p_B)
self.show_S_send.append("根据g,rand_B,p计算得到B\n")
self.show_S_send.append(str(B))
complete_B = hash.SHA_1(str(B))
self.show_S_send.append("对B生成HASH摘要\n")
self.show_S_send.append(str(complete_B))
source_B = S.rsa_encrypt(complete_B, './key/DH_S_signkey.txt')
self.show_S_send.append("对B的摘要使用Server的私钥进行签名\n")
self.show_S_send.append(str(source_B))
pack = dh_s.pack(B,source_B)
self.show_S_send.append("打包B与B的数字签名\n")
self.show_S_send.append(str(pack))
share.send_data(pack)
self.show_S_send.append("发送包成功\n")
def run_button_C_getkey(self):
data = share.getdata()
self.output_C_key.setText("接收到包\n")
(B,signature_B) = dh_c.data_parse(data)
self.output_C_key.append("B:\n")
self.output_C_key.append(str(B))
self.output_C_key.append("B的数字签名:\n")
self.output_C_key.append(str(signature_B))
complete_B = hash.SHA_1(str(B))
self.output_C_key.append("B的本地计算HASH为:\n")
self.output_C_key.append(str(complete_B))
source_B = C.rsa_decrypt(signature_B, S_n, S_pubkey)
source_B = binascii.a2b_hex(source_B).decode()
self.output_C_key.append("使用Client的公钥解签得B的HASH为:\n")
self.output_C_key.append(str(source_B))
if (complete_B == source_B):
self.output_C_key.append("B的来源性与完整性验证成功:\n")
C_Key = dh_c.C_get_Key(B, rand_A, p_A)
self.output_C_key.append("根据B,rand_A,p_A计算共享密钥C_Key:\n")
self.output_C_key.append(str(C_Key))
with open('./key/DH_C_key.txt','w') as f:
f.write(str(C_Key))
def run_button_S_getkey(self):
S_Key = dh_s.S_get_Key(A, rand_B, p_B)
self.output_S_key.setText("根据A,rand_B,p_B计算共享密钥S_Key\n")
self.output_S_key.append(str(S_Key))
with open('./key/DH_S_key.txt', 'w') as f:
f.write(str(S_Key))
def run_button_sec_C(self):
with open('./key/DH_C_key.txt', 'r') as f:
C_des_key=f.read()[0:8]
with open('./key/DH_Sec_C.txt', 'w') as f2:
f2.write(C_des_key)
self.output_sec_C.setText("取共享密钥前64位作为DES密钥加密消息")
des.encryptDes('./input/send_message.txt', './key/DH_Sec_C.txt', './output/des_encrypt_message.txt')
with open('./input/send_message.txt', 'r',encoding='utf-8') as f3:
message=f3.read()
with open('./output/des_encrypt_message.txt',encoding='utf-8') as f4:
encrypto=f4.read()
self.output_sec_C.append("明文为;")
self.output_sec_C.append(str(message))
self.output_sec_C.append("加密成功,密文为;")
self.output_sec_C.append(str(encrypto))
def run_button_sec_S(self):
with open('./key/DH_S_key.txt', 'r') as f:
S_des_key=f.read()[0:8]
with open('./key/DH_Sec_S.txt', 'w') as f2:
f2.write(S_des_key)
self.output_sec_S.setText("取共享密钥前64位作为DES密钥解密消息")
des.decryptDes('./output/des_encrypt_message.txt', './key/DH_Sec_S.txt', './output/des_decrypt_message.txt')
self.output_sec_S.append("解密完成,请查看./output/des_decrypt_message.txt获取解密后信息")
with open('./output/des_encrypt_message.txt', 'r',encoding='utf-8') as f3:
encrypto=f3.read()
with open('./output/des_decrypt_message.txt', 'r',encoding='utf-8') as f4:
decrypto=f4.read()
self.output_sec_S.append("密文为;")
self.output_sec_S.append(str(encrypto))
self.output_sec_S.append("解密后消息为;")
self.output_sec_S.append(str(decrypto))
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = MyMainForm()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DH.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 1000)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(11, 51, 646, 843))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.button_pre_C_send = QtWidgets.QPushButton(self.layoutWidget)
self.button_pre_C_send.setObjectName("button_pre_C_send")
self.gridLayout.addWidget(self.button_pre_C_send, 3, 0, 1, 1)
self.button_S_send = QtWidgets.QPushButton(self.layoutWidget)
self.button_S_send.setObjectName("button_S_send")
self.gridLayout.addWidget(self.button_S_send, 4, 2, 1, 1)
self.label_port = QtWidgets.QLabel(self.layoutWidget)
self.label_port.setAlignment(QtCore.Qt.AlignCenter)
self.label_port.setObjectName("label_port")
self.gridLayout.addWidget(self.label_port, 2, 2, 1, 1)
self.output_pre_C_send = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_pre_C_send.setObjectName("output_pre_C_send")
self.gridLayout.addWidget(self.output_pre_C_send, 3, 1, 1, 1)
self.button_server = QtWidgets.QPushButton(self.layoutWidget)
self.button_server.setObjectName("button_server")
self.gridLayout.addWidget(self.button_server, 1, 2, 1, 1)
self.show_C_send = QtWidgets.QTextBrowser(self.layoutWidget)
self.show_C_send.setObjectName("show_C_send")
self.gridLayout.addWidget(self.show_C_send, 4, 1, 1, 1)
self.button_sec_C = QtWidgets.QPushButton(self.layoutWidget)
self.button_sec_C.setObjectName("button_sec_C")
self.gridLayout.addWidget(self.button_sec_C, 6, 0, 1, 1)
self.button_pre_S_send = QtWidgets.QPushButton(self.layoutWidget)
self.button_pre_S_send.setObjectName("button_pre_S_send")
self.gridLayout.addWidget(self.button_pre_S_send, 3, 2, 1, 1)
self.show_config = QtWidgets.QLabel(self.layoutWidget)
self.show_config.setAlignment(QtCore.Qt.AlignCenter)
self.show_config.setObjectName("show_config")
self.gridLayout.addWidget(self.show_config, 0, 1, 1, 1)
self.button_sec_S = QtWidgets.QPushButton(self.layoutWidget)
self.button_sec_S.setObjectName("button_sec_S")
self.gridLayout.addWidget(self.button_sec_S, 6, 2, 1, 1)
self.button_begin = QtWidgets.QPushButton(self.layoutWidget)
self.button_begin.setObjectName("button_begin")
self.gridLayout.addWidget(self.button_begin, 0, 0, 1, 1)
self.button_client = QtWidgets.QPushButton(self.layoutWidget)
self.button_client.setObjectName("button_client")
self.gridLayout.addWidget(self.button_client, 1, 0, 1, 1)
self.output_pre_S_send = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_pre_S_send.setObjectName("output_pre_S_send")
self.gridLayout.addWidget(self.output_pre_S_send, 3, 4, 1, 1)
self.label_connect = QtWidgets.QLabel(self.layoutWidget)
self.label_connect.setAlignment(QtCore.Qt.AlignCenter)
self.label_connect.setObjectName("label_connect")
self.gridLayout.addWidget(self.label_connect, 0, 2, 1, 1)
self.label_ip = QtWidgets.QLabel(self.layoutWidget)
self.label_ip.setAlignment(QtCore.Qt.AlignCenter)
self.label_ip.setObjectName("label_ip")
self.gridLayout.addWidget(self.label_ip, 2, 0, 1, 1)
self.input_port = QtWidgets.QLineEdit(self.layoutWidget)
self.input_port.setObjectName("input_port")
self.gridLayout.addWidget(self.input_port, 2, 4, 1, 1)
self.output_sec_S = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_sec_S.setObjectName("output_sec_S")
self.gridLayout.addWidget(self.output_sec_S, 6, 4, 1, 1)
self.output_sec_C = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_sec_C.setObjectName("output_sec_C")
self.gridLayout.addWidget(self.output_sec_C, 6, 1, 1, 1)
self.output_S_key = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_S_key.setObjectName("output_S_key")
self.gridLayout.addWidget(self.output_S_key, 5, 4, 1, 1)
self.button_client_connect = QtWidgets.QPushButton(self.layoutWidget)
self.button_client_connect.setObjectName("button_client_connect")
self.gridLayout.addWidget(self.button_client_connect, 1, 1, 1, 1)
self.output_C_key = QtWidgets.QTextBrowser(self.layoutWidget)
self.output_C_key.setObjectName("output_C_key")
self.gridLayout.addWidget(self.output_C_key, 5, 1, 1, 1)
self.button_C_getkey = QtWidgets.QPushButton(self.layoutWidget)
self.button_C_getkey.setObjectName("button_C_getkey")
self.gridLayout.addWidget(self.button_C_getkey, 5, 0, 1, 1)
self.show_S_send = QtWidgets.QTextBrowser(self.layoutWidget)
self.show_S_send.setObjectName("show_S_send")
self.gridLayout.addWidget(self.show_S_send, 4, 4, 1, 1)
self.input_ip = QtWidgets.QLineEdit(self.layoutWidget)
self.input_ip.setObjectName("input_ip")
self.gridLayout.addWidget(self.input_ip, 2, 1, 1, 1)
self.button_C_send = QtWidgets.QPushButton(self.layoutWidget)
self.button_C_send.setObjectName("button_C_send")
self.gridLayout.addWidget(self.button_C_send, 4, 0, 1, 1)
self.button_S_getkey = QtWidgets.QPushButton(self.layoutWidget)
self.button_S_getkey.setObjectName("button_S_getkey")
self.gridLayout.addWidget(self.button_S_getkey, 5, 2, 1, 1)
self.show_connect = QtWidgets.QLabel(self.layoutWidget)
self.show_connect.setAlignment(QtCore.Qt.AlignCenter)
self.show_connect.setObjectName("show_connect")
self.gridLayout.addWidget(self.show_connect, 0, 4, 1, 1)
self.button_server_connect = QtWidgets.QPushButton(self.layoutWidget)
self.button_server_connect.setObjectName("button_server_connect")
self.gridLayout.addWidget(self.button_server_connect, 1, 4, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1000, 18))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button_pre_C_send.setText(_translate("MainWindow", "通信验证"))
self.button_S_send.setText(_translate("MainWindow", "数据发送"))
self.label_port.setText(_translate("MainWindow", "Port"))
self.button_server.setText(_translate("MainWindow", "Server"))
self.button_sec_C.setText(_translate("MainWindow", "保密通信"))
self.button_pre_S_send.setText(_translate("MainWindow", "通信验证"))
self.show_config.setText(_translate("MainWindow", "statue"))
self.button_sec_S.setText(_translate("MainWindow", "保密通信"))
self.button_begin.setText(_translate("MainWindow", "初始化配置"))
self.button_client.setText(_translate("MainWindow", "Client"))
self.label_connect.setText(_translate("MainWindow", "连接状态"))
self.label_ip.setText(_translate("MainWindow", "IP:"))
self.button_client_connect.setText(_translate("MainWindow", "client_connect"))
self.button_C_getkey.setText(_translate("MainWindow", "交换密钥生成"))
self.button_C_send.setText(_translate("MainWindow", "数据发送"))
self.button_S_getkey.setText(_translate("MainWindow", "交换密钥生成"))
self.show_connect.setText(_translate("MainWindow", "out_of_connection"))
self.button_server_connect.setText(_translate("MainWindow", "server_connect"))
可以发现共享密钥生成的部分产生的内容是一样的。下图演示为本地演示,实际可在一个局域网下的不同主机进行结果实现。每次通信都可以重新生成一个新的对称密钥进行通信。