1 Star 0 Fork 2

Elysia/认证密钥交换协议实验_1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client_handshake.py 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
from Crypto.PublicKey import RSA
import socket
from getkeys import get_session_key
from info import *
from myException import *
ip_port = ('127.0.0.1', 9999)
s = socket.socket() # 创建套接字
s.connect(ip_port) # 连接服务器
messages=[]
info=Info()
client_hello = info.build_client_hello()
s.sendall(client_hello)
print('发出client_hello')
messages.append(client_hello)
server_reply = s.recv(1024)
server_hello=server_reply
print('收到server_hello')
messages.append(server_hello)
try:
verify_cipherSuite(client_hello[3:],server_hello[3:])
except Exception:
error=info.build_error_message(0x02)
s.sendall(server_hello)
print('服务器端密码算法 cipherSuite 错误')
sys.exit()
server_reply = s.recv(1024)
try:
verify_error(server_reply)
except Exception:
print('收到报错信息')
sys.exit()
server_certificate=server_reply
print('收到server_certificate')
messages.append(server_certificate)
with open('RSA_public_key_client.pem', 'r') as f:
public_key = f.read().encode()
client_certificate = info.build_client_certificate(public_key)
s.sendall(client_certificate)
print('发出client_certificate')
messages.append(client_certificate)
key_client = RSA.import_key(open('RSA_private_key_client.pem').read())
certificate_verify = info.build_certificate_verify(key_client, client_hello, server_hello, server_certificate)
s.sendall(certificate_verify)
print('发出certificate_verify')
messages.append(certificate_verify)
key_server = RSA.import_key(server_certificate[4:])
master_secret,client_key_exchange = info.build_client_key_exchange(key_server)
s.sendall(client_key_exchange)
print('发出client_key_exchange')
messages.append(client_key_exchange)
server_reply = s.recv(1024)
try:
verify_error(server_reply)
except Exception:
print('收到报错信息')
sys.exit()
server_finished=server_reply
print('收到server_finished')
new_server_finished=info.build_server_finished(messages,master_secret)
try:
verify_handshake(server_finished,new_server_finished)
except Exception:
error=info.build_error_message(0x08)
s.sendall(server_hello)
print('服务器端发送的握手认证错误')
sys.exit()
client_finished=info.build_client_finished(messages, master_secret)
s.sendall(client_finished)
print('发出client_finished')
Skey,Mkey=get_session_key(master_secret,client_hello[3:],server_hello[3:])
try:
verify_error(server_reply)
except Exception:
print('收到报错信息')
sys.exit()
print(Skey)
print(Mkey)
print('test')
print('client_hello')
print(client_hello)
print('server_hello')
print(server_hello)
print('server_certificate')
print(server_certificate)
print('client_certificate')
print(client_certificate)
print('certificate_verify')
print(certificate_verify)
print('client_key_exchange')
print(client_key_exchange)
print('server_finished')
print(server_finished)
print('client_finished')
print(client_finished)
s.close() # 关闭连接
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/citrusyuzu/key_exchange_lab_1.git
git@gitee.com:citrusyuzu/key_exchange_lab_1.git
citrusyuzu
key_exchange_lab_1
认证密钥交换协议实验_1
master

搜索帮助