diff --git a/数据查询方_new.py b/数据查询方_new.py index 3ffc677..80edbb0 100644 --- a/数据查询方_new.py +++ b/数据查询方_new.py @@ -7,6 +7,7 @@ from io import StringIO import CoreAlgorithm import DataSearch + # 审批回执类消息head:01001001 # sql密文类消息head:01001010 # 元数据类消息head:01001100 @@ -16,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 @@ -29,12 +31,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 @@ -53,7 +56,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 @@ -78,7 +81,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() # 获取数字证书 @@ -101,16 +107,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) @@ -124,15 +132,17 @@ class MyClient: # 编写sql def create_sql(self): self.window = tk.Toplevel() # 建立窗口 - self.window.title('编写sql语句') # 为窗口命名 - self.window.geometry('580x270') # 设定窗口大小 + self.window.title("编写sql语句") # 为窗口命名 + self.window.geometry("580x270") # 设定窗口大小 # 初始化信息输入框 self.sql_text = tk.StringVar() - tk.Label(self.window, bg='green', text='请输入您需要的sql查询:').place(x=70, y=50) + tk.Label(self.window, bg="green", text="请输入您需要的sql查询:").place(x=70, y=50) input_message = tk.Entry(self.window, textvariable=self.sql_text, width=60) input_message.place(x=70, y=100) # 初始化确认按钮 - bt_confirm = tk.Button(self.window, bg='green', text='确认', width=60, command=self.get_sql) + bt_confirm = tk.Button( + self.window, bg="green", text="确认", width=60, command=self.get_sql + ) bt_confirm.place(x=70, y=150) # self.sql = input("请编写你的sql:") @@ -149,7 +159,7 @@ class MyClient: def safe_connect(self): if self.certificate != {}: # 与服务器端地址建立通信, 8080为服务端程序的端口号 - self.client.connect(("localhost",1111)) + self.client.connect(("localhost", 1111)) # 为接收消息函数添加线程 threading.Thread(target=self.get_message).start() time.sleep(3) @@ -178,7 +188,7 @@ class MyClient: # 加密封装sql语句 def deal_sql(self, context): - message = (str(self.client.getsockname()) + "||" + context) + message = str(self.client.getsockname()) + "||" + context global server_public_key_data enc_message = str_to_encrypt(message, server_public_key_data) # 用平台的公钥进行加密 return "01001010" + str(enc_message.ciphertext()) # int 型 @@ -198,8 +208,14 @@ class MyClient: print("本地公钥信息为:") print(self.public_key_data) print("打印公钥如下:") - 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) # show_list() messagebox.showinfo("本地公钥", pack_public_key) # GUI界面显示 @@ -208,8 +224,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("打印私钥如下:") print(pack_private_key) messagebox.showinfo("本地私钥", pack_private_key) # GUI界面显示 @@ -248,8 +270,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) @@ -267,14 +292,20 @@ class MyClient: messagebox.showinfo("平台发来的消息", "与平台的双向认证成功!") 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) @@ -285,28 +316,36 @@ class MyClient: if 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) print("分析结果:", dec_int_message) messagebox.showinfo("分析结果", dec_int_message) else: messagebox.showinfo("提示", message) -# --------------------------接收sql消息的窗口设计函数------------------------- + # --------------------------接收sql消息的窗口设计函数------------------------- # 接受调用数据的请求 def send_accept_reply(self): - self.client.send(("01001001" + str(self.client.getsockname()) + "||安全多方平台同意了你的数据申请。").encode()) + self.client.send( + ( + "01001001" + str(self.client.getsockname()) + "||安全多方平台同意了你的数据申请。" + ).encode() + ) 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) @@ -325,7 +364,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() @@ -338,24 +381,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): output = StringIO() # 用于保存打印信息 @@ -377,31 +428,71 @@ class MyClient: # 界面按键 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=360) - 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=398) - 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=433) - 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=468) - button5 = ttk.Button(self.root, text="调用SQL查询", bootstyle="info-outline", - command=self.create_sql, width=30) + button5 = ttk.Button( + self.root, + text="调用SQL查询", + bootstyle="info-outline", + command=self.create_sql, + width=30, + ) button5.place(x=104, y=503) - button6 = ttk.Button(self.root, text="发送SQL请求", bootstyle="info-outline", - command=self.send_sql, width=30) + button6 = ttk.Button( + self.root, + text="发送SQL请求", + bootstyle="info-outline", + command=self.send_sql, + width=30, + ) button6.place(x=104, y=538) - button7 = ttk.Button(self.root, text="处理请求信息", bootstyle="info-outline", - command=self.design_window, width=30) + button7 = ttk.Button( + self.root, + text="处理请求信息", + bootstyle="info-outline", + command=self.design_window, + width=30, + ) button7.place(x=104, y=573) - button8 = ttk.Button(self.root, text="查看医疗记录", bootstyle="info-outline", - command=self.search_data, width=30) + button8 = ttk.Button( + self.root, + text="查看医疗记录", + bootstyle="info-outline", + command=self.search_data, + width=30, + ) button8.place(x=104, y=608) # GUI界面