1 Star 0 Fork 2

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

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server_handshake.py 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
import sys
from Crypto import Random
from Crypto.PublicKey import RSA
import socket
from info import *
from getkeys import get_session_key
from myException import *
ip_port = ('127.0.0.1', 9999)
sk = socket.socket() # 创建套接字
sk.bind(ip_port) # 绑定服务地址
sk.listen(5) # 监听连接请求
print('启动socket服务,等待客户端连接')
conn, address = sk.accept() # 等待连接,此处自动阻塞
print('连接建立')
messages=[]
info=Info()
client_reply = conn.recv(1024)
client_hello=client_reply
print('收到client_hello')
messages.append(client_hello)
server_hello=info.build_server_hello()
conn.sendall(server_hello)
print('发出server_hello')
messages.append(server_hello)
try:
verify_cipherSuite(client_hello[3:],server_hello[3:])
except Exception:
error=info.build_error_message(0x01)
conn.sendall(server_hello)
print('客户端密码算法 cipherSuite 错误')
sys.exit()
with open('RSA_public_key_server.pem', 'r') as f:
public_key = f.read().encode()
server_certificate = info.build_server_certificate(public_key)
conn.sendall(server_certificate)
print('发出server_certificate')
messages.append(server_certificate)
client_reply = conn.recv(1024)
try:
verify_error(client_reply)
except Exception:
print('收到报错信息')
sys.exit()
client_certificate=client_reply
print('收到client_certificate')
messages.append(client_certificate)
client_reply = conn.recv(1024)
try:
verify_error(client_reply)
except Exception:
print('收到报错信息')
sys.exit()
certificate_verify=client_reply
print('收到certificate_verify')
messages.append(certificate_verify)
client_reply = conn.recv(1024)
try:
verify_error(client_reply)
except Exception:
print('收到报错信息')
sys.exit()
client_key_exchange=client_reply
print('收到client_key_exchange')
messages.append(client_key_exchange)
key_server = RSA.import_key(open('RSA_private_key_server.pem').read())
cipher_rsa = PKCS1_v1_5.new(key_server)
sentinel = Random.new().read(128)
master_secret=cipher_rsa.decrypt(client_key_exchange[4:],sentinel)
server_finished=info.build_server_finished(messages, master_secret)
conn.sendall(server_finished)
print('发出server_finished')
client_reply = conn.recv(1024)
try:
verify_error(client_reply)
except Exception:
print('收到报错信息')
sys.exit()
client_finished=client_reply
print('收到client_finished')
new_client_finished=info.build_client_finished(messages,master_secret)
try:
verify_handshake(client_finished,new_client_finished)
except Exception:
error=info.build_error_message(0x05)
conn.sendall(server_hello)
print('客户端发送的握手认证错误')
sys.exit()
Skey,Mkey=get_session_key(master_secret,client_hello[3:],server_hello[3:])
try:
verify_error(client_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)
conn.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

搜索帮助