Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
9af2bd77d3 | 2 years ago |
|
96d9dc736a | 2 years ago |
|
f81d507a61 | 2 years ago |
@ -0,0 +1,105 @@
|
|||||||
|
import os
|
||||||
|
import socket
|
||||||
|
from threading import Thread
|
||||||
|
from tkinter import LEFT, RIGHT, Button, Label, StringVar, ttk
|
||||||
|
from tkinter.tix import Tk # 升级的组合控件包
|
||||||
|
from server_twokey import server_twokey
|
||||||
|
from ttkbootstrap import Style
|
||||||
|
import tqdm
|
||||||
|
|
||||||
|
from server_panduan import panduan
|
||||||
|
|
||||||
|
|
||||||
|
def agree():
|
||||||
|
# 设计IP和端口
|
||||||
|
SERVER_HOST = "0.0.0.0"
|
||||||
|
SERVER_PORT = 9799
|
||||||
|
|
||||||
|
# 文件读写缓冲区
|
||||||
|
BUFFER_SIZE = 4096
|
||||||
|
SEPARATOR = "<SEPARATOR>"
|
||||||
|
|
||||||
|
# 创建Server
|
||||||
|
s = socket.socket()
|
||||||
|
s.bind((SERVER_HOST, SERVER_PORT))
|
||||||
|
|
||||||
|
# 设置连接数
|
||||||
|
s.listen(5)
|
||||||
|
print(f"服务器监听{SERVER_HOST}:{SERVER_PORT}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 接受客户端连接
|
||||||
|
client_socket, address = s.accept()
|
||||||
|
zhaiyao = Label(
|
||||||
|
root, text="接收成功!", bg="red", bd=1, font=("Arial", 12), width=10, height=1
|
||||||
|
)
|
||||||
|
zhaiyao.pack()
|
||||||
|
# 输出客户端IP
|
||||||
|
print(f"客户端{address}连接")
|
||||||
|
# 接受客户端信息
|
||||||
|
received = client_socket.recv(BUFFER_SIZE).decode()
|
||||||
|
filename, file_size = received.split(SEPARATOR)
|
||||||
|
# 获取传输文件名和大小
|
||||||
|
filename = os.path.basename(filename)
|
||||||
|
file_size = int(file_size)
|
||||||
|
|
||||||
|
# 文件接受处理
|
||||||
|
progress = tqdm.tqdm(
|
||||||
|
range(file_size),
|
||||||
|
f"接收{filename}",
|
||||||
|
unit="B",
|
||||||
|
unit_divisor=1024,
|
||||||
|
unit_scale=True,
|
||||||
|
)
|
||||||
|
with open(filename, "wb") as f:
|
||||||
|
for _ in progress:
|
||||||
|
# 读取客户端数据
|
||||||
|
bytes_read = client_socket.recv(BUFFER_SIZE)
|
||||||
|
if not bytes_read:
|
||||||
|
break
|
||||||
|
f.write(bytes_read)
|
||||||
|
progress.update(len(bytes_read))
|
||||||
|
# 关闭
|
||||||
|
client_socket.close()
|
||||||
|
print("客户端关闭")
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
|
def socketStart():
|
||||||
|
print("服务器启动")
|
||||||
|
t = Thread(target=agree)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
|
||||||
|
def init_window():
|
||||||
|
global root
|
||||||
|
root = Tk()
|
||||||
|
style = Style(theme='minty')
|
||||||
|
root.title("服务器端")
|
||||||
|
root.geometry("700x500")
|
||||||
|
root.configure(bg='white')
|
||||||
|
root.resizable(width=False, height=False)
|
||||||
|
root.tk.eval("package require Tix") # 引入升级包,这样才能使用升级的组合控件
|
||||||
|
ttk.Button(root, text="开始接收", command=socketStart,bootstyle="default-outline").place(x=400, y=210, width=200, height=60)
|
||||||
|
model = StringVar()
|
||||||
|
model.set("请选择解密模式")
|
||||||
|
values = ["DES", "AES_ECB", "AES_CBC"]
|
||||||
|
ttk.Combobox(
|
||||||
|
master=root, # 父容器
|
||||||
|
height=10, # 高度,下拉显示的条目数量
|
||||||
|
width=20, # 宽度
|
||||||
|
state="readonly", # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
|
||||||
|
cursor="arrow", # 鼠标移动时样式 arrow, circle, cross, plus... # 字体
|
||||||
|
textvariable=model, # 通过StringVar设置可改变的值
|
||||||
|
values=values,
|
||||||
|
bootstyle="info",# 设置下拉框的选项
|
||||||
|
).place(x=30,y=230,width=180,height=30)
|
||||||
|
ttk.Button(
|
||||||
|
root,text="生成接收端公私钥",command=lambda: server_twokey(),bootstyle="default-outline"
|
||||||
|
).place(x=400, y=50, width=200, height=60)
|
||||||
|
ttk.Button(root, text="解密", command=lambda: panduan(root=root, op=model.get()),bootstyle="default-outline").place(x=400,y=380,width=200, height=60)
|
||||||
|
root.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
if "__main__" == __name__:
|
||||||
|
init_window()
|
@ -0,0 +1,54 @@
|
|||||||
|
import hashlib, base64
|
||||||
|
from tkinter import *
|
||||||
|
from tkinter import ttk
|
||||||
|
from pyDes import des, PAD_PKCS5, ECB
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
from Crypto.Signature import PKCS1_v1_5
|
||||||
|
import rsa
|
||||||
|
from pyDes import des, CBC, PAD_PKCS5
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
import binascii
|
||||||
|
|
||||||
|
|
||||||
|
def panduan(root, op):
|
||||||
|
with open("encryptedkeyfile.bin", "rb") as f:
|
||||||
|
encryptedkeyfile = f.read()
|
||||||
|
with open("./Bprivkey.pem", "rb") as YY:
|
||||||
|
Bprivkey = YY.read()
|
||||||
|
privkey = rsa.PrivateKey.load_pkcs1(Bprivkey)
|
||||||
|
m = rsa.decrypt(encryptedkeyfile, privkey)
|
||||||
|
|
||||||
|
with open("encryptedfile.txt", "rb") as ff:
|
||||||
|
encryptedfile = ff.read()
|
||||||
|
secret_key = m
|
||||||
|
iv = secret_key
|
||||||
|
if op == "DES":
|
||||||
|
des_obj = des(secret_key, ECB, iv, pad=None, padmode=PAD_PKCS5)
|
||||||
|
decrypt_str = des_obj.decrypt(encryptedfile)
|
||||||
|
elif op == "AES_ECB":
|
||||||
|
aes_obj = AES.new(secret_key, mode=AES.MODE_ECB)
|
||||||
|
decrypt_str = aes_obj.decrypt(encryptedfile)
|
||||||
|
elif op == "AES_CBC":
|
||||||
|
aes = AES.new(secret_key, AES.MODE_CBC) # CBC模式下解密需要重新创建一个aes对象
|
||||||
|
decrypt_str = aes.decrypt(encryptedfile)
|
||||||
|
|
||||||
|
decrypt_str1 = decrypt_str.decode().rstrip("\0").encode()
|
||||||
|
|
||||||
|
decrypt_str2 = hashlib.sha256(decrypt_str1).hexdigest().encode("utf-8")
|
||||||
|
Apubkey = open("../client/Apubkey.pem", "rb").read().decode("utf-8")
|
||||||
|
key = RSA.importKey(Apubkey)
|
||||||
|
h = SHA256.new(decrypt_str2)
|
||||||
|
verifier = PKCS1_v1_5.new(key)
|
||||||
|
signature = open("sigfile.bin", "rb").read().decode()
|
||||||
|
if verifier.verify(h, base64.b64decode(signature)):
|
||||||
|
print("正确!")
|
||||||
|
zhaiyao = Label(
|
||||||
|
root, text="成功!", width=10, height=1
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
zhaiyao = Label(
|
||||||
|
root, text="失败", width=10, height=1
|
||||||
|
)
|
||||||
|
zhaiyao.place(x=200,y=380,width=180,height=60)
|
@ -0,0 +1,8 @@
|
|||||||
|
import rsa
|
||||||
|
def server_twokey():
|
||||||
|
pubkey, privkey = rsa.newkeys(1024)
|
||||||
|
pub = pubkey.save_pkcs1()
|
||||||
|
pri = privkey.save_pkcs1("PEM")
|
||||||
|
with open("./Bpubkey.pem", mode="wb") as f, open("./Bprivkey.pem", mode="wb") as f1:
|
||||||
|
f.write(pub)
|
||||||
|
f1.write(pri)
|
Loading…
Reference in new issue