日期:
来源:Linux公社收集编辑:聆听世界的鱼
点击上方蓝字 ● 关注Linux公社
Tkinter 和 Base64 模块
linuxmi@linuxmi /home/linuxmi/www.linuxmi.comsudo apt install python3-tk
如何在 Python 中对消息编码和解码
from tkinter import *import base64root = Tk()root.geometry('800x500')root.configure(background='aqua')root.title("使用 Python 对消息进行编码和解码")
Label(root, text='Python消息编码器和解码器', font='arial 25 bold', fg='white', bg="purple").pack()Text = StringVar()key = StringVar()mode = StringVar()Result = StringVar()
def Encode(key, message):enc = []for i in range(len(message)):key_c = key[i % len(key)]enc.append(chr((ord(message[i]) + ord(key_c)) % 256))return base64.urlsafe_b64encode("".join(enc).encode()).decode()
def Mode():if (mode.get() == 'E'):Result.set(Encode(key.get(), Text.get()))elif (mode.get() == 'D'):Result.set(Decode(key.get(), Text.get()))else:Result.set('Invalid Mode')
def Exit():root.destroy()def Reset():Text.set("")key.set("")mode.set("")Result.set("")
Label(root, font='arial 17 bold', text='消息', fg='black', bg="aqua").place(x=60, y=100)Entry(root, font='arial 15', textvariable=Text, bg='white').place(x=450, y=100)Label(root, font='arial 17 bold', text='密钥', fg='black', bg="aqua").place(x=60, y=130)Entry(root, font='arial 15', textvariable=key, bg='white').place(x=450, y=130)Label(root, font='arial 17 bold', text='模式(E-Encode, D-Decode)', fg='black', bg="aqua").place(x=60, y=160)Entry(root, font='arial 15', textvariable=mode, bg='white').place(x=450, y=160)Label(root, font='arial 17 bold', text='文本', fg='black', bg="aqua").place(x=60, y=190)Entry(root, font='arial 15 bold', textvariable=Result, bg='white').place(x=450, y=190)Label(root, text='Linux迷 www.linuxmi.com', font='arial 25 bold', fg='white', bg="purple").pack()
Button(root, font='arial 15 bold', text='结果', padx=2, bg='Light Gray', command=Mode).place(x=100, y=240)Button(root, font='arial 15 bold', text='重置', width=6, command=Reset, bg='Green', padx=2).place(x=300, y=240)Button(root, font='arial 15 bold', text='停止', width=6, command=Exit, bg='Red', padx=2, pady=2).place(x=500, y=240)
mainloop ()函数告诉 Python 运行 Tkinter 事件循环并监听事件(例如按钮按下),直到您关闭窗口。
root.mainloop()
消息编码器/解码器的实际应用
使用 Python 检查网络安全
如果需要完整代码,请关注 Linux迷 公众号。
后台回复 pym
链接:https://www.linuxmi.com/python-base64-encode-decode.html
关注我们 长按或扫描下面二维码关注 Linux公社
关注 Linux公社,添加“ 星标 ”
每天 获取 技术干货,让我们一起成长
合作联系:root@linuxidc.net