帮我检查修改一下代码:from CryptoUtilnumber import from CryptoPublicKey import RSAfrom hashlib import sha256import random os signal stringimport signalfrom tkinter import from tkinter import filedialogfrom tkint
以下是修改后的代码:
from Crypto.Util.number import *
from Crypto.PublicKey import RSA
from hashlib import sha256
import random, os, string
import re
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import base64
import pandas as pd
def proof_of_work():
random.seed(os.urandom(8))
proof = ''.join([random.choice(string.ascii_letters+string.digits) for _ in range(20)])
_hexdigest = sha256(proof.encode()).hexdigest()
text.insert("1.0","原理非常简单,就是我这有20位随机数,提供给你ta们的哈希值,和后面16位的值,你写前四位就好了,至于为什么要有这个,假如我做的是网站,防DDOS攻击就很有必要了\n")
text.insert("2.0",f"sha256(XXXX+{proof[4:]}) == {_hexdigest}")
return proof
def shuru(x,proof):
_hexdigest = sha256(proof.encode()).hexdigest()
if len(x) != 4 or sha256(x.encode()+proof[4:].encode()).hexdigest() != _hexdigest:
show_popup()
else:
root.withdraw()
root2.deiconify()
def clear_fields():
# 清除所有输入框和文本框的内容
inp1.delete(0, tk.END)
text.delete("1.0", tk.END)
def show_popup():
# 弹出一个消息框
messagebox.showinfo("你输入的有误", "确定再来一遍?")
clear_fields()
def xiage(old_window,new_window):
old_window.withdraw()
new_window.deiconify()
########################################计算性验证界面#######################################################
root=tk.Tk()
root.title("登录计算性检验")
root.geometry('1024x1024')
master=tk.Tk()
master.title('登陆页面')
master.geometry('500x600')
master.withdraw()
lb1 =tk.Label(root, text='这是为了预防DDOS攻击设立的,我建议你最好算一下',font=("华文行楷",20,"italic"))
lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)
proof= tk.StringVar()
btnlp=tk.Button(root, text='开始验证',font=("华文行楷",20,"italic"),command=lambda:proof.set(proof_of_work()))
btnlp.place(relx=0.2, rely=0.2, relwidth=0.6, relheight=0.1)
text = tk.Text(root)
text.place(relx=0.2, rely=0.3, relwidth=0.4, relheight=0.1)
inp1 = tk.Entry(root)
inp1.place(relx=0.2, rely=0.4, relwidth=0.4, relheight=0.1)
btnlp2=tk.Button(root, text="检验",font=("华文行楷",20,"italic"),command=lambda:shuru(inp1.get(),proof.get()))
btnlp2.place(relx=0.2, rely=0.5, relwidth=0.6, relheight=0.1)
btnlp3=tk.Button(root, text="检验",font=("华文行楷",20,"italic"),command=lambda:xiage(root,master))
btnlp3.place(relx=0.2, rely=0.6, relwidth=0.6, relheight=0.1)
l_username=tk.Label(master,text='用户名:')
l_password=tk.Label(master,text='密码:')
entry_username=tk.Entry(master)
entry_password=tk.Entry(master, show='*')
def login():
username=entry_username.get()
password=entry_password.get()
if username=="admin" and password=="123":
master.destroy()
denglua()
elif username=="user" and password=="456":
master.destroy()
denglub()
else:
messagebox.showerror("Error")
def denglua():
master=tk.Tk()
master.geometry('1024x1024')
master.title("管理员登录")
label=tk.Label(master,text="欢迎管理员使用!")
label.pack()
btn=tk.Button(master, text="查看",font=("华文行楷",20,"italic"),command=jiancea)
btn.place(relx=0.2, rely=0.5, relwidth=0.6, relheight=0.1)
text=tk.Text(master)
text.pack()
df=pd.read_csv(r"C:\Users\57160\fake_data.csv")
#敏感数据检测
def jiancea():
text.delete("1.0", tk.END)
column_pattern = re.compile('.*号.*') # Regular expression pattern to match column names
for column in df.columns:
if re.match(column_pattern, column):
encoded_values =df[column].apply(lambda x: base64.b64encode(str(x).encode('utf-8')).decode('utf-8'))
df[column] = encoded_values
elif column == "住址":
encoded_addresses =df[column].apply(lambda x: str(x).encode('gbk'))
df[column] = encoded_addresses
text.insert("1.0",df)
master.mainloop()
def denglub():
master=tk.Tk()
master.geometry('1024x1024')
master.title("用户登录")
label=tk.Label(master,text="欢迎用户使用!")
label.pack()
btn2=tk.Button(master, text="加密",font=("华文行楷",20,"italic"),command=jiami)
btn2.place(relx=0.2, rely=0.5, relwidth=0.6, relheight=0.1)
btn3=tk.Button(master, text="解密",font=("华文行楷",20,"italic"),command=jiemi)
btn3.place(relx=0.2, rely=0.6, relwidth=0.6, relheight=0.1)
text2=tk.Text(master)
text2.pack()
master.mainloop()
def rsamiyaoshengcheng():
try:
p = 10940206282074483858036773727052823389305030997054406946763014582669310228184596230111265755887655367310175216583281138971104503610629184058848548063825083
q = 12442523493425835794134606490415609544124507346968381571595410902712280527217151303692521128760607432325728467210194053326155586037224803356693169939598853
assert p > 0
assert q > 0
assert p != q
assert p.bit_length() == 512
assert q.bit_length() == 512
assert isPrime(p)
assert isPrime(q)
except:
print("Invalid params")
try:
e = 65537
d = inverse(e,(p-1)*(q-1))
modulus = p * q
public_key = RSA.construct((modulus, e))
private_key = RSA.construct((modulus, e, d, p, q))
return public_key,private_key
except KeyboardInterrupt:
print("")
except ValueError:
print("How could this happen?")
def rsajiami(public_key, data):
cipher = PKCS1_v1_5.new(public_key)
cipher_text = cipher.encrypt(data.encode('utf-8'))
encoded_cipher_text = base64.b64encode(cipher_text).decode('utf-8')
return encoded_cipher_text
def jiami():
public_key,private_key=rsamiyaoshengcheng()
column_pattern = re.compile('.*号.*') # Regular expression pattern to match column names
for column in df.columns:
if re.match(column_pattern, column):
encrypted_values = df[column].apply(lambda x: rsajiami(public_key,str(x)))
df[column] = encrypted_values
text2.insert("1.0","抱歉,您的权限不够,请向管理员申请密钥\n")
text2.insert("2.0",df)
def rsajiemi(encoded_cipher_text):
cipher_text = base64.b64decode(encoded_cipher_text)
decrypt_cipher = PKCS1_v1_5.new(private_key)
decrypt_plain_text = decrypt_cipher.decrypt(cipher_text, None)
return decrypt_plain_text.decode('utf-8')
# 使用私钥解密加密后的列数据
def jiemi():
text2.delete("1.0", tk.END)
for column in df.columns:
if re.match(column_pattern, column):
decrypted_values = df[column].apply(lambda x: rsajiemi(x))
df[column] = decrypted_values
text2.insert("1.0",df)
b_login=tk.Button(master,text="登录",command=login)
b_quit=tk.Button(master,text="退出",command=master.destroy)
l_username.grid(row=0,column=0)
l_password.grid(row=1,column=0)
entry_username.grid(row=0,column=1)
entry_password.grid(row=1,column=1)
b_login.grid(row=2,column=1)
b_quit.grid(row=3,column=1)
root.mainloop()
``
原文地址: https://www.cveoy.top/t/topic/fYpR 著作权归作者所有。请勿转载和采集!