style: modify code style

This commit is contained in:
sangge 2023-11-29 16:28:59 +08:00
parent 217c8c1614
commit ea5b6c7b85
5 changed files with 391 additions and 144 deletions

View File

@ -1,19 +1,19 @@
import re
import pymysql
import pymysql # pylint: disable=e0401 # type: ignore
# 拆分sql对象
def extract_tables(sql_statement):
# 使用正则表达式匹配FROM关键字之后的表名以及逗号分隔的表名
pattern = r'FROM\s+([\w\s,]+)'
pattern = r"FROM\s+([\w\s,]+)"
matches = re.findall(pattern, sql_statement, re.IGNORECASE)
# 提取逗号分隔的表名,并按逗号进行分割
tabs = re.split(',', matches[0].strip())
tabs = re.split(",", matches[0].strip())
# 处理每个表名,去除空格和其他无关字符
cleaned_tables = []
for tab in tabs:
cleaned_tab = tab.strip()
if ' ' in cleaned_tab:
if " " in cleaned_tab:
cleaned_tab = cleaned_tab.split()[0]
cleaned_tables.append(cleaned_tab)
return cleaned_tables # 返回列表
@ -25,12 +25,12 @@ def divide_sql(sql):
例如sql = "SELECT a FROM data1, data2, data3 WHERE a = b ORDER BY misc"
拆分原语句
"""
parts = re.split(r'(?i)from\s', sql) # 拆分"from "【无论大小写】
parts = re.split(r"(?i)from\s", sql) # 拆分"from "【无论大小写】
head = parts[0] + "from " # SELECT a FROM
divide_sqls = []
if re.search(r'where', parts[1], flags=re.IGNORECASE):
data = re.split(r'(?i) where', parts[1]) # 拆分" where"【无论大小写】
if re.search(r"where", parts[1], flags=re.IGNORECASE):
data = re.split(r"(?i) where", parts[1]) # 拆分" where"【无论大小写】
tail = " where" + data[1] # WHERE a = b ORDER BY misc
# message_p = "涉及到的数据源有:"
# print(message_p)
@ -64,7 +64,13 @@ class SelectDatabase: # 定义类
def ret_hospital(self): # 定义函数返回字典——医院HOS这个表
# 连接服务器Server的数据库:
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
db = pymysql.connect(
host="localhost",
user="root",
password="111111",
db=f"{self.database}",
charset="utf8",
)
# 使用操作游标:
cursor = db.cursor()
sql = """SELECT * FROM HOS"""
@ -108,7 +114,13 @@ class SelectDatabase: # 定义类
def ret_doctor(self): # 定义函数返回字典——医生DOC这个表
# 连接服务器Server的数据库:
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
db = pymysql.connect(
host="localhost",
user="root",
password="111111",
db=f"{self.database}",
charset="utf8",
)
# 使用操作游标:
cursor = db.cursor()
sql = """SELECT * FROM DOC"""
@ -147,7 +159,13 @@ class SelectDatabase: # 定义类
def ret_patient(self): # 定义函数返回字典——病人PAT这个表
# 连接服务器Server的数据库:
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
db = pymysql.connect(
host="localhost",
user="root",
password="111111",
db=f"{self.database}",
charset="utf8",
)
# 使用操作游标:
cursor = db.cursor()
sql = """SELECT * FROM PAT"""
@ -182,7 +200,13 @@ class SelectDatabase: # 定义类
def ret_diagnosis(self): # 定义函数返回字典——诊断DIA这个表
# 连接服务器Server的数据库:
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
db = pymysql.connect(
host="localhost",
user="root",
password="111111",
db=f"{self.database}",
charset="utf8",
)
# 使用操作游标:
cursor = db.cursor()
sql = """SELECT * FROM DIA"""
@ -228,7 +252,13 @@ class SelectDatabase: # 定义类
def ret_define(self, sql): # 定义函数,返回自定义信息的字典
# 连接服务器Server的数据库:
db = pymysql.connect(host='localhost', user='root', password='111111', db=f'{self.database}', charset='utf8')
db = pymysql.connect(
host="localhost",
user="root",
password="111111",
db=f"{self.database}",
charset="utf8",
)
try:
# 使用操作游标:
cursor = db.cursor()
@ -253,12 +283,12 @@ def main(hospital_name):
PAT.PAT_ID = DIA.PAT_ID
"""
hospital_name = hospital_name.replace("\n", "")
if hospital_name == 'xx大学附属医院':
database = SelectDatabase('Hospital1')
if hospital_name == "xx大学附属医院":
database = SelectDatabase("Hospital1")
database.ret_define(sql)
elif hospital_name == 'xx阳光社区附属医院':
database = SelectDatabase('Hospital2')
elif hospital_name == "xx阳光社区附属医院":
database = SelectDatabase("Hospital2")
database.ret_define(sql)
else:

View File

@ -1,18 +1,25 @@
import wx
import wx.xrc
import wx # pylint: disable=e0401 # type: ignore
import wx.xrc # pylint: disable=e0401 # type: ignore
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"安全多方服务器平台", pos=wx.DefaultPosition,
wx.Frame.__init__(
self,
parent,
id=wx.ID_ANY,
title="安全多方服务器平台",
pos=wx.DefaultPosition,
size=wx.Size(444, 749),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL,
)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetFont(wx.Font(5, 70, 90, 90, False, "宋体"))
self.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT))
self.SetBackgroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
)
fgSizer1 = wx.FlexGridSizer(0, 2, 0, 0)
fgSizer1.SetFlexibleDirection(wx.BOTH)
@ -20,51 +27,81 @@ class MyFrame(wx.Frame):
bSizer11 = wx.BoxSizer(wx.VERTICAL)
self.m_bitmap11 = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 30), 0)
self.m_bitmap11 = wx.StaticBitmap(
self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 30), 0
)
bSizer11.Add(self.m_bitmap11, 0, wx.ALL, 5)
self.m_staticline211 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline211.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline211 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline211.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
self.m_staticline211.SetMaxSize(wx.Size(80, -1))
bSizer11.Add(self.m_staticline211, 0, wx.EXPAND | wx.ALL, 5)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"接收记录", wx.DefaultPosition, wx.Size(-1, 30), 0)
self.m_staticText1 = wx.StaticText(
self, wx.ID_ANY, "接收记录", wx.DefaultPosition, wx.Size(-1, 30), 0
)
self.m_staticText1.Wrap(-1)
self.m_staticText1.SetFont(wx.Font(11, 70, 90, 90, False, "宋体"))
self.m_staticText1.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_staticText1.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer11.Add(self.m_staticText1, 0, wx.ALL, 5)
self.m_bitmap1 = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 111), 0)
self.m_bitmap1 = wx.StaticBitmap(
self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 111), 0
)
bSizer11.Add(self.m_bitmap1, 0, wx.ALL, 5)
self.m_staticline21 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline21.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline21 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline21.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
self.m_staticline21.SetMaxSize(wx.Size(80, -1))
bSizer11.Add(self.m_staticline21, 0, wx.EXPAND | wx.ALL, 5)
self.m_staticText11 = wx.StaticText(self, wx.ID_ANY, u"发送记录", wx.DefaultPosition, wx.Size(-1, 30), 0)
self.m_staticText11 = wx.StaticText(
self, wx.ID_ANY, "发送记录", wx.DefaultPosition, wx.Size(-1, 30), 0
)
self.m_staticText11.Wrap(-1)
self.m_staticText11.SetFont(wx.Font(11, 70, 90, 90, False, "宋体"))
self.m_staticText11.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_staticText11.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer11.Add(self.m_staticText11, 0, wx.ALL, 5)
self.m_bitmap12 = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 111), 0)
self.m_bitmap12 = wx.StaticBitmap(
self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(70, 111), 0
)
bSizer11.Add(self.m_bitmap12, 0, wx.ALL, 5)
self.m_staticline212 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline212.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline212 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline212.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
self.m_staticline212.SetMaxSize(wx.Size(80, -1))
bSizer11.Add(self.m_staticline212, 0, wx.EXPAND | wx.ALL, 5)
self.m_staticText111 = wx.StaticText(self, wx.ID_ANY, u"当前状态", wx.DefaultPosition, wx.Size(-1, 30), 0)
self.m_staticText111 = wx.StaticText(
self, wx.ID_ANY, "当前状态", wx.DefaultPosition, wx.Size(-1, 30), 0
)
self.m_staticText111.Wrap(-1)
self.m_staticText111.SetFont(wx.Font(11, 70, 90, 90, False, "宋体"))
self.m_staticText111.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_staticText111.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer11.Add(self.m_staticText111, 0, wx.ALL, 5)
@ -74,48 +111,80 @@ class MyFrame(wx.Frame):
bSizer2 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText = wx.StaticText(self, wx.ID_ANY, u"安全多方平台服务器", wx.DefaultPosition, wx.Size(-1, 30), 0)
self.m_staticText = wx.StaticText(
self, wx.ID_ANY, "安全多方平台服务器", wx.DefaultPosition, wx.Size(-1, 30), 0
)
self.m_staticText.Wrap(-1)
self.m_staticText.SetFont(wx.Font(16, 70, 90, 92, False, "宋体"))
self.m_staticText.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_staticText.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer2.Add(self.m_staticText, 0, wx.ALL, 5)
self.m_staticline1 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(1, 2), wx.LI_HORIZONTAL)
self.m_staticline1.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline1 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(1, 2), wx.LI_HORIZONTAL
)
self.m_staticline1.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
bSizer2.Add(self.m_staticline1, 0, wx.EXPAND | wx.ALL, 5)
self.m_listCtrl1 = wx.ListCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT)
self.m_listCtrl1 = wx.ListCtrl(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT
)
self.m_listCtrl1.SetFont(wx.Font(9, 70, 90, 90, False, "宋体"))
self.m_listCtrl1.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_listCtrl1.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer2.Add(self.m_listCtrl1, 0, wx.ALL, 5)
self.m_staticline2 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline2.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline2 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline2.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
bSizer2.Add(self.m_staticline2, 0, wx.EXPAND | wx.ALL, 5)
self.m_listCtrl2 = wx.ListCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT)
self.m_listCtrl2 = wx.ListCtrl(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT
)
self.m_listCtrl2.SetFont(wx.Font(9, 70, 90, 90, False, "宋体"))
self.m_listCtrl2.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_listCtrl2.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer2.Add(self.m_listCtrl2, 0, wx.ALL, 5)
self.m_staticline22 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline22.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline22 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline22.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
bSizer2.Add(self.m_staticline22, 0, wx.EXPAND | wx.ALL, 5)
self.m_listCtrl3 = wx.ListCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT)
self.m_listCtrl3 = wx.ListCtrl(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(360, 150), wx.LC_REPORT
)
self.m_listCtrl3.SetFont(wx.Font(9, 70, 90, 90, False, "宋体"))
self.m_listCtrl3.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_listCtrl3.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer2.Add(self.m_listCtrl3, 0, wx.ALL, 5)
self.m_staticline221 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL)
self.m_staticline221.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.m_staticline221 = wx.StaticLine(
self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1, 2), wx.LI_HORIZONTAL
)
self.m_staticline221.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
)
bSizer2.Add(self.m_staticline221, 0, wx.EXPAND | wx.ALL, 5)
@ -123,9 +192,13 @@ class MyFrame(wx.Frame):
bSizer102 = wx.BoxSizer(wx.HORIZONTAL)
self.m_button31 = wx.Button(self, wx.ID_ANY, u"显示本地公钥", wx.DefaultPosition, wx.Size(300, 30), 0)
self.m_button31 = wx.Button(
self, wx.ID_ANY, "显示本地公钥", wx.DefaultPosition, wx.Size(300, 30), 0
)
self.m_button31.SetFont(wx.Font(11, 70, 90, 90, False, wx.EmptyString))
self.m_button31.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button31.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer102.Add(self.m_button31, 0, wx.ALIGN_CENTER | wx.ALL, 5)
@ -133,9 +206,13 @@ class MyFrame(wx.Frame):
bSizer1012 = wx.BoxSizer(wx.HORIZONTAL)
self.m_button41 = wx.Button(self, wx.ID_ANY, u"显示本地私钥", wx.DefaultPosition, wx.Size(300, 30), 0)
self.m_button41 = wx.Button(
self, wx.ID_ANY, "显示本地私钥", wx.DefaultPosition, wx.Size(300, 30), 0
)
self.m_button41.SetFont(wx.Font(11, 70, 90, 90, False, wx.EmptyString))
self.m_button41.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button41.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer1012.Add(self.m_button41, 0, wx.ALIGN_CENTER | wx.ALL, 5)
@ -143,9 +220,13 @@ class MyFrame(wx.Frame):
bSizer10111 = wx.BoxSizer(wx.HORIZONTAL)
self.m_button51 = wx.Button(self, wx.ID_ANY, u"打印本地证书", wx.DefaultPosition, wx.Size(300, 30), 0)
self.m_button51 = wx.Button(
self, wx.ID_ANY, "打印本地证书", wx.DefaultPosition, wx.Size(300, 30), 0
)
self.m_button51.SetFont(wx.Font(11, 70, 90, 90, False, wx.EmptyString))
self.m_button51.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self.m_button51.SetForegroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
)
bSizer10111.Add(self.m_button51, 0, wx.ALIGN_CENTER | wx.ALL, 5)

View File

@ -1,7 +1,7 @@
import socket
import time
import pickle
import wx
import wx # pylint: disable=e0401 # type: ignore
import CoreAlgorithm
import threading
@ -25,8 +25,11 @@ def generate_key_pair_data():
# 从公钥证书中提取公钥信息
def get_public_key_data(message):
message = eval(message.replace("\n", ""))
public_key = message["public_key"].replace("-----BEGIN PUBLIC KEY-----", "").replace(
"-----END PUBLIC KEY-----", "") # 分割得到公钥
public_key = (
message["public_key"]
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
) # 分割得到公钥
public_key_bytes = bytes.fromhex(public_key)
public_key_data = pickle.loads(public_key_bytes)
return public_key_data
@ -34,12 +37,13 @@ def get_public_key_data(message):
# 用公钥为本地生成数字证书
def get_certificate(temp_public_key, cert_name):
public_key_str = '\n'.join(temp_public_key[i:i + 60] for i in range(0, len(temp_public_key), 60))
pack_public_key = "-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----\n"
cert = {
'public_key': pack_public_key,
'name': cert_name
}
public_key_str = "\n".join(
temp_public_key[i : i + 60] for i in range(0, len(temp_public_key), 60)
)
pack_public_key = (
"-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----\n"
)
cert = {"public_key": pack_public_key, "name": cert_name}
return cert
@ -49,7 +53,7 @@ def str_to_encrypt(message, public_data):
if message.isdigit():
int_message = int(message)
else:
int_message = int.from_bytes(message.encode(), 'big')
int_message = int.from_bytes(message.encode(), "big")
enc_message = public_data.encrypt(int_message)
print("int_message", int_message)
return enc_message
@ -77,7 +81,7 @@ class MyServer(server_demo.MyFrame):
# 初始化socket
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定IP地址和端口
self.server.bind(('localhost', 1111))
self.server.bind(("localhost", 1111))
# 设置最大监听数
self.server.listen(5)
# 设置一个字典,用来保存每一个客户端的连接和身份信息
@ -127,7 +131,9 @@ class MyServer(server_demo.MyFrame):
time.sleep(sleep_time)
# 创建线程,负责接收客户端信息并转发给其他客户端
threading.Thread(target=self.recv_from_client, args=(client_socket,)).start()
threading.Thread(
target=self.recv_from_client, args=(client_socket,)
).start()
# 产生公私钥字符串
def generate_key_pair(self):
@ -142,8 +148,10 @@ class MyServer(server_demo.MyFrame):
# 接收客户端消息并转发
def recv_from_client(self, client_socket): # client_socket指的是连接到的端口socket
while True:
message = client_socket.recv(self.maxSize).decode('utf-8')
message_p = "接收到来自{0}的message:\n{1}".format(self.socket_mapping[client_socket][0], message)
message = client_socket.recv(self.maxSize).decode("utf-8")
message_p = "接收到来自{0}的message:\n{1}".format(
self.socket_mapping[client_socket][0], message
)
print(message_p)
self.m_listCtrl1.Append([message_p]) # 准备文件传输
if message.startswith("01001001"):
@ -152,7 +160,9 @@ class MyServer(server_demo.MyFrame):
self.m_listCtrl3.Append([message_p])
time.sleep(sleep_time)
# 去掉审批回执的头部
message = message.split("01001001", 1)[1] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
message = message.split("01001001", 1)[
1
] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
# 认证发送者的合法性
sender = message.split("||")[0]
context = message.split("||")[1]
@ -171,7 +181,9 @@ class MyServer(server_demo.MyFrame):
# 认证发送者的合法性
if len(self.socket_mapping[client_socket]) > 1: # 如果发送者的公钥信息已被收集
# 识别明文中一起发送的发送目标 明文应是发送者||发送内容(||时间戳等),对象用socket表示吧...
message = message.split("01001010", 1)[1] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
message = message.split("01001010", 1)[
1
] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
# 用发送目标之前给的公钥加密明文,得到密文。
# 去掉sql密文的头部
# 使用平台私钥解密消息,获得sql明文
@ -181,9 +193,13 @@ class MyServer(server_demo.MyFrame):
time.sleep(sleep_time)
int_message = int(message)
# Ciphertext转EncryptedNumber
Encrpt_message = CoreAlgorithm.EncryptedNumber(self.public_key_data, int_message, exponent=0)
Encrpt_message = CoreAlgorithm.EncryptedNumber(
self.public_key_data, int_message, exponent=0
)
dec_int_message = self.private_key_data.decrypt(Encrpt_message)
dec_message = dec_int_message.to_bytes((dec_int_message.bit_length() + 7) // 8, 'big').decode()
dec_message = dec_int_message.to_bytes(
(dec_int_message.bit_length() + 7) // 8, "big"
).decode()
message_p = "解密后的消息为:" + dec_message
print(message_p)
self.m_listCtrl3.Append([message_p])
@ -231,7 +247,9 @@ class MyServer(server_demo.MyFrame):
self.m_listCtrl3.Append([message_p])
time.sleep(sleep_time)
# 去掉元数据头部
message = message.split("01001100", 1)[1] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
message = message.split("01001100", 1)[
1
] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
# 把元数据存入本地数据库的临时表里格式provider + encode_data
# print("message:", message)
int_message = int(message)
@ -239,8 +257,11 @@ class MyServer(server_demo.MyFrame):
print(message_p)
self.m_listCtrl1.Append([message_p])
# 根据证书找公钥信息
search_public_key = self.search_cert["public_key"].split("-----END PUBLIC KEY-----")[0].split(
"-----BEGIN PUBLIC KEY-----")[1]
search_public_key = (
self.search_cert["public_key"]
.split("-----END PUBLIC KEY-----")[0]
.split("-----BEGIN PUBLIC KEY-----")[1]
)
search_public_key = search_public_key.replace("\n", "")
print("提取到的search_public_key:")
print(search_public_key)
@ -252,7 +273,9 @@ class MyServer(server_demo.MyFrame):
print("对应的search_public_key_data:")
print(search_public_key_data)
# int密 -- EncryptedNumber密
Encrpt_message = CoreAlgorithm.EncryptedNumber(search_public_key_data, int_message, exponent=0)
Encrpt_message = CoreAlgorithm.EncryptedNumber(
search_public_key_data, int_message, exponent=0
)
self.datas.append(Encrpt_message)
# Ciphertext转EncryptedNumber
# print("int_message:", int_message)
@ -283,7 +306,9 @@ class MyServer(server_demo.MyFrame):
print(message)
print(self.socket_mapping)
print(get_public_key_data(message))
self.socket_mapping[client_socket].append(get_public_key_data(message)) # 绑定端口与公钥信息的关系
self.socket_mapping[client_socket].append(
get_public_key_data(message)
) # 绑定端口与公钥信息的关系
print(self.socket_mapping)
cert = eval(message)
print(cert, type(cert)) # 字典型
@ -367,8 +392,14 @@ class MyServer(server_demo.MyFrame):
print(message_p)
self.m_listCtrl3.Append([message_p])
public_key_str = '\n'.join(self.public_key[i:i + 60] for i in range(0, len(self.public_key), 60))
pack_public_key = "-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----\n"
public_key_str = "\n".join(
self.public_key[i : i + 60] for i in range(0, len(self.public_key), 60)
)
pack_public_key = (
"-----BEGIN PUBLIC KEY-----\n"
+ public_key_str
+ "\n-----END PUBLIC KEY-----\n"
)
message_p = pack_public_key
print(message_p)
message = message_p.split("\n") # 设置打印格式,因为显示窗打印不了\n
@ -386,8 +417,14 @@ class MyServer(server_demo.MyFrame):
print(message_p)
self.m_listCtrl3.Append([message_p])
private_key_str = '\n'.join(self.private_key[i:i + 60] for i in range(0, len(self.public_key), 60))
pack_private_key = "-----BEGIN PRIVATE KEY-----\n" + private_key_str + "\n-----END PRIVATE KEY-----\n"
private_key_str = "\n".join(
self.private_key[i : i + 60] for i in range(0, len(self.public_key), 60)
)
pack_private_key = (
"-----BEGIN PRIVATE KEY-----\n"
+ private_key_str
+ "\n-----END PRIVATE KEY-----\n"
)
message_p = "打印私钥如下:"
print(message_p)
@ -423,17 +460,32 @@ class MyServer(server_demo.MyFrame):
def pack_sqls(self):
for key, value in self.socket_mapping.items():
for i in range(len(self.divide_providers)):
if self.divide_providers[i] in value[2]: # eg: value[2] == "<数据提供方>风舱医院"
if (
self.divide_providers[i] in value[2]
): # eg: value[2] == "<数据提供方>风舱医院"
for j in range(len(self.divide_sqls)):
if self.divide_providers[i] in self.divide_sqls[j]: # 如果发送目标和信息匹配)
sql = str(self.source) + "||" + str(key.getsockname()) + "||" + self.divide_sqls[i]
if (
self.divide_providers[i] in self.divide_sqls[j]
): # 如果发送目标和信息匹配)
sql = (
str(self.source)
+ "||"
+ str(key.getsockname())
+ "||"
+ self.divide_sqls[i]
)
print(sql)
int_enc_sql = str_to_encrypt(sql, value[1]).ciphertext() # 用接收者的公钥加密消息
int_enc_sql = str_to_encrypt(
sql, value[1]
).ciphertext() # 用接收者的公钥加密消息
message_p = "01001010" + str(int_enc_sql)
key.send(message_p.encode())
self.m_listCtrl2.Append([message_p])
message_p = "已将消息{0}发送给{1},其地址为{2}".format(
self.divide_sqls[j], self.divide_providers[i], key.getsockname())
self.divide_sqls[j],
self.divide_providers[i],
key.getsockname(),
)
print(message_p)
self.m_listCtrl3.Append([message_p])
@ -446,7 +498,7 @@ class MyServer(server_demo.MyFrame):
self.m_listCtrl3.Append([message_p])
time.sleep(sleep_time)
# #################安全多方计算分析过程##############
if 'select count(*) from xx阳光社区诊所, xx大学附属医院' in self.sql:
if "select count(*) from xx阳光社区诊所, xx大学附属医院" in self.sql:
print(self.datas)
for x in self.datas:
self.result = self.result + x

View File

@ -17,8 +17,9 @@ import tkinter as tk
from tkinter import *
from tkinter import messagebox, scrolledtext
from PIL import ImageTk
# 美化
from ttkbootstrap import Style
from ttkbootstrap import Style # pylint: disable=e0401 # type: ignore
from tkinter import ttk
sleep_time = 1
@ -32,12 +33,13 @@ def generate_key_pair_data():
# 用公钥为本地生成数字证书
def get_certificate(temp_public_key, cert_name):
public_key_str = '\n'.join(temp_public_key[i:i + 60] for i in range(0, len(temp_public_key), 60))
pack_public_key = "-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----"
cert = {
'public_key': pack_public_key,
'name': "<数据提供方>" + cert_name
}
public_key_str = "\n".join(
temp_public_key[i : i + 60] for i in range(0, len(temp_public_key), 60)
)
pack_public_key = (
"-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----"
)
cert = {"public_key": pack_public_key, "name": "<数据提供方>" + cert_name}
return cert
@ -56,7 +58,7 @@ def str_to_encrypt(message, public_data):
if message.isdigit():
int_message = int(message)
else:
int_message = int.from_bytes(message.encode(), 'big')
int_message = int.from_bytes(message.encode(), "big")
enc_message = public_data.encrypt(int_message)
print("int_message", int_message)
return enc_message
@ -82,7 +84,10 @@ class MyClient:
self.give_name()
# 生成私钥和公钥信息
self.private_key_data, self.public_key_data = generate_key_pair_data() # PaillierPublicKey 类型
(
self.private_key_data,
self.public_key_data,
) = generate_key_pair_data() # PaillierPublicKey 类型
# 生成私钥和公钥字符串
self.private_key, self.public_key = self.generate_key_pair()
# 初始化数字证书
@ -105,16 +110,18 @@ class MyClient:
# 为本端取名
def give_name(self):
self.window = tk.Toplevel()
self.window.title('命名窗口')
self.window.geometry('300x320')
self.window.title("命名窗口")
self.window.geometry("300x320")
# 初始化信息输入框
self.name_text = tk.StringVar()
tk.Label(self.window, bg='green', text='请为本端取名:').place(x=50, y=100)
tk.Label(self.window, bg="green", text="请为本端取名:").place(x=50, y=100)
input_message = tk.Entry(self.window, textvariable=self.name_text)
input_message.pack()
input_message.place(x=80, y=150)
# 确认按钮及位置
bt_confirm = tk.Button(self.window, bg='green', text='确认', width=10, command=self.get_name)
bt_confirm = tk.Button(
self.window, bg="green", text="确认", width=10, command=self.get_name
)
bt_confirm.pack()
bt_confirm.place(x=125, y=220)
@ -159,8 +166,14 @@ class MyClient:
print("本地公钥信息为:")
print(self.public_key_data)
print("打印公钥如下:", end="")
public_key_str = '\n'.join(self.public_key[i:i + 60] for i in range(0, len(self.public_key), 60))
pack_public_key = "\n-----BEGIN PUBLIC KEY-----\n" + public_key_str + "\n-----END PUBLIC KEY-----\n"
public_key_str = "\n".join(
self.public_key[i : i + 60] for i in range(0, len(self.public_key), 60)
)
pack_public_key = (
"\n-----BEGIN PUBLIC KEY-----\n"
+ public_key_str
+ "\n-----END PUBLIC KEY-----\n"
)
print(pack_public_key)
messagebox.showinfo("本地公钥", pack_public_key) # GUI界面显示
@ -168,8 +181,14 @@ class MyClient:
def print_private_key(self):
print("本地私钥信息为:")
print(self.private_key_data)
private_key_str = '\n'.join(self.private_key[i:i + 60] for i in range(0, len(self.public_key), 60))
pack_private_key = "-----BEGIN PRIVATE KEY-----\n" + private_key_str + "\n-----END PRIVATE KEY-----\n"
private_key_str = "\n".join(
self.private_key[i : i + 60] for i in range(0, len(self.public_key), 60)
)
pack_private_key = (
"-----BEGIN PRIVATE KEY-----\n"
+ private_key_str
+ "\n-----END PRIVATE KEY-----\n"
)
print("打印私钥如下:", end="")
print(pack_private_key)
messagebox.showinfo("本地私钥", pack_private_key) # GUI界面显示
@ -208,8 +227,11 @@ class MyClient:
print(key, ":\n", value)
message += key + ":\n" + value
messagebox.showinfo("平台的公钥证书", message)
server_public_key = cert["public_key"].split("-----END PUBLIC KEY-----")[0].split(
"-----BEGIN PUBLIC KEY-----")[1]
server_public_key = (
cert["public_key"]
.split("-----END PUBLIC KEY-----")[0]
.split("-----BEGIN PUBLIC KEY-----")[1]
)
server_public_key = server_public_key.replace("\n", "")
print("提取到的server_public_key:")
print(server_public_key)
@ -238,8 +260,11 @@ class MyClient:
print(key, ":\n", value)
message += key + ":\n" + value + "\n"
# messagebox.showinfo("数据查询方的公钥证书", message)
search_public_key = cert["public_key"].split("-----END PUBLIC KEY-----")[0].split(
"-----BEGIN PUBLIC KEY-----")[1]
search_public_key = (
cert["public_key"]
.split("-----END PUBLIC KEY-----")[0]
.split("-----BEGIN PUBLIC KEY-----")[1]
)
search_public_key = search_public_key.replace("\n", "")
print("提取到的search_public_key:")
print(search_public_key)
@ -250,14 +275,20 @@ class MyClient:
# messagebox.showinfo("数据查询方的公钥信息", search_public_key_data)
elif message.startswith("01001010"):
message = message.split("01001010", 1)[1] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
message = message.split("01001010", 1)[
1
] # [0]是空字符串,已测试。只切一次是防止密文出现和头部一样的信息被误切。
# 使用私钥解密消息,获得sql明文
print("接收到的sql密文:", message)
int_message = int(message)
# Ciphertext转EncryptedNumber
Encrpt_message = CoreAlgorithm.EncryptedNumber(self.public_key_data, int_message, exponent=0)
Encrpt_message = CoreAlgorithm.EncryptedNumber(
self.public_key_data, int_message, exponent=0
)
dec_int_message = self.private_key_data.decrypt(Encrpt_message)
dec_message = dec_int_message.to_bytes((dec_int_message.bit_length() + 7) // 8, 'big').decode()
dec_message = dec_int_message.to_bytes(
(dec_int_message.bit_length() + 7) // 8, "big"
).decode()
print("解密后的消息为:", dec_message)
self.sql = dec_message.split("||")[2]
print("收到已授权方的sql:", self.sql)
@ -267,19 +298,29 @@ class MyClient:
# 接受调用数据的请求
def send_accept_reply(self):
self.client.send(("01001001" + str(self.client.getsockname()) + "||" + self.name + "同意了你的数据申请。").encode())
self.client.send(
(
"01001001"
+ str(self.client.getsockname())
+ "||"
+ self.name
+ "同意了你的数据申请。"
).encode()
)
self.window.destroy()
self.window = tk.Toplevel()
self.window.title('命名窗口')
self.window.geometry('300x320')
self.window.title("命名窗口")
self.window.geometry("300x320")
# 信息输入框
self.data_text = tk.StringVar()
tk.Label(self.window, bg='green', text='请输入要调用的信息:').place(x=50, y=100)
tk.Label(self.window, bg="green", text="请输入要调用的信息:").place(x=50, y=100)
input_message = tk.Entry(self.window, textvariable=self.data_text)
input_message.pack()
input_message.place(x=80, y=150)
# 确认按钮及位置
bt_confirm = tk.Button(self.window, bg='green', text='确认', width=10, command=self.send_data)
bt_confirm = tk.Button(
self.window, bg="green", text="确认", width=10, command=self.send_data
)
bt_confirm.pack()
bt_confirm.place(x=125, y=220)
@ -300,7 +341,11 @@ class MyClient:
# 拒绝调用数据的请求
def send_decline_reply(self):
self.client.send(("01001001" + str(self.client.getsockname()) + "||" + "想得美哦!不给(*^▽^*)").encode())
self.client.send(
(
"01001001" + str(self.client.getsockname()) + "||" + "想得美哦!不给(*^▽^*)"
).encode()
)
messagebox.showinfo("提示", "已拒绝!")
self.window.destroy()
@ -313,23 +358,32 @@ class MyClient:
if self.sql != "":
self.window = tk.Toplevel()
self.window.title("平台发来的消息")
self.window.geometry('550x320') # 设定窗口大小
self.window.geometry("550x320") # 设定窗口大小
self.recent_message = "收到已授权方的sql:" + self.sql
tk.Label(self.window, bg='green', text=self.recent_message).place(x=50, y=100)
tk.Label(self.window, bg="green", text=self.recent_message).place(
x=50, y=100
)
# 创建接受按钮
accept_button = tk.Button(self.window, text="接受", width=15, command=self.send_accept_reply)
accept_button = tk.Button(
self.window, text="接受", width=15, command=self.send_accept_reply
)
accept_button.place(x=90, y=220)
# 创建拒绝按钮
decline_button = tk.Button(self.window, text="拒绝", width=15, command=self.send_decline_reply)
decline_button = tk.Button(
self.window, text="拒绝", width=15, command=self.send_decline_reply
)
decline_button.place(x=225, y=220)
# 创建稍后处理按钮
decline_button = tk.Button(self.window, text="稍后处理", width=15, command=self.wait_reply)
decline_button = tk.Button(
self.window, text="稍后处理", width=15, command=self.wait_reply
)
decline_button.place(x=360, y=220)
else:
messagebox.showinfo("提示", "暂无待处理的信息。")
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
# 调用本地信息库
def search_data(self):
@ -349,29 +403,59 @@ class MyClient:
roll.insert(tk.END, long_text)
roll.pack()
# --------------------------主界面的按键功能绑定--------------------------------
# --------------------------主界面的按键功能绑定--------------------------------
# 界面按键
def create_widgets(self):
# 创建按钮和标签等部件并使用ttk和Style进行美化
style = Style(theme='darkly') # 指定样式主题
style = Style(theme="darkly") # 指定样式主题
self.root = style.master
button1 = ttk.Button(self.root, text="查看我的公钥", bootstyle="info-outline",
command=self.print_public_key, width=30)
button1 = ttk.Button(
self.root,
text="查看我的公钥",
bootstyle="info-outline",
command=self.print_public_key,
width=30,
)
button1.place(x=104, y=310)
button2 = ttk.Button(self.root, text="查看我的私钥", bootstyle="info-outline",
command=self.print_private_key, width=30)
button2 = ttk.Button(
self.root,
text="查看我的私钥",
bootstyle="info-outline",
command=self.print_private_key,
width=30,
)
button2.place(x=104, y=348)
button3 = ttk.Button(self.root, text="生成我的证书", bootstyle="info-outline",
command=self.print_certificate, width=30)
button3 = ttk.Button(
self.root,
text="生成我的证书",
bootstyle="info-outline",
command=self.print_certificate,
width=30,
)
button3.place(x=104, y=383)
button4 = ttk.Button(self.root, text="获取平台认证", bootstyle="info-outline",
command=self.safe_connect, width=30)
button4 = ttk.Button(
self.root,
text="获取平台认证",
bootstyle="info-outline",
command=self.safe_connect,
width=30,
)
button4.place(x=104, y=418)
button5 = ttk.Button(self.root, text="处理请求信息", bootstyle="info-outline",
command=self.design_window, width=30)
button5 = ttk.Button(
self.root,
text="处理请求信息",
bootstyle="info-outline",
command=self.design_window,
width=30,
)
button5.place(x=104, y=453)
button6 = ttk.Button(self.root, text="查看医疗记录", bootstyle="info-outline",
command=self.search_data, width=30)
button6 = ttk.Button(
self.root,
text="查看医疗记录",
bootstyle="info-outline",
command=self.search_data,
width=30,
)
button6.place(x=104, y=488)
# GUI界面