#importing modules
from tkinter import *
import base64
from PIL import Image, ImageTk
#initialize window
root = Tk()
root.geometry('500x400')
root.resizable(0, 0)
root.configure(bg="#FFE873")
root.iconbitmap('shield.ico')
#title of the window
root.title("Message Encode and Decode")
image1 = Image.open('shield.png')
test = ImageTk.PhotoImage(image1)
Label(image = test, bg='#FFE873').pack()
#label
Label(root, text ='ENCODE DECODE', font = 'arial 20 bold', bg="#FFE873").pack()
Label(root, text ='@python', font = 'arial 20 bold', bg="#FFE873").pack(side =BOTTOM)
#define variables
Text = StringVar()
private_key = StringVar()
mode_en = StringVar(root, '1')
Result = StringVar()
#define function
#function to encode
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()
#function to decode
def Decode(key, message):
dec=[]
message = base64.urlsafe_b64decode(message).decode()
for i in range(len(message)):
key_c = key[i % len(key)]
dec.append(chr((256 + ord(message[i])- ord(key_c)) % 256))
return "".join(dec)
#function to set mode
def Mode():
if mode_en.get() =='1':
Result.set(Encode(private_key.get(), Text.get()))
elif mode_en.get() =='2':
Result.set(Decode(private_key.get(), Text.get()))
else:
Result.set('Invalid Mode')
#Function to exit window
def Exit():
root.destroy()
#Function to reset
def Reset():
Text.set("")
private_key.set("")
mode_en.set("")
Result.set("")
# Label and Button
#Message
Label(root, font= 'arial 12 bold', text='MESSAGE', bg = '#FFE873').place(x= 60, y=180)
Entry(root, font = 'arial 10', textvariable = Text, bg = '#D5C6FF').place(x=290, y = 180)
#key
Label(root, font = 'arial 12 bold', text ='KEY', bg = '#FFE873').place(x=60, y = 210)
Entry(root, font = 'arial 10', textvariable = private_key, bg ='#D5C6FF').place(x=290, y = 210)
values = {
'1':'Encode',
'2':'Decode'
}
#mode
Label(root, font = 'arial 12 bold', text ='MODE', bg = '#FFE873').place(x=60, y = 240)
for text, value in values.items():
if text=='1': Radiobutton(root, text=value, variable=mode_en, value=text, font = 'arial 10 bold', bg="#FFE873").place(x=290,y=240)
Radiobutton(root,text=value,variable=mode_en, value=text, font = 'arial 10 bold', bg="#FFE873").place(x=360,y=240)
#result
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='#D5C6FF').place(x=290, y = 270)
#result button
Button(root, font = 'arial 10 bold', text = 'RESULT', bg ='white', fg='black', command = Mode).place(x=60, y = 270)
#reset button
Button(root, font = 'arial 10 bold', text ='RESET', width =6, command = Reset, bg = 'LimeGreen', padx=2).place(x=160, y = 310)
#exit button
Button(root, font = 'arial 10 bold', text= 'EXIT', width = 6, command = Exit, bg = 'OrangeRed', padx=2, pady=2).place(x=260, y = 310)
root.mainloop()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Base64是一种编码方法,可以将二进制数据转换为可打印的ASCII字符,以便在文本环境中传输和存储。Python的base64库提供了编码和解码函数。使用base64库进行编码的步骤如下:将需要编码的数据转换为bytes类型。调用b64encode()函数,将数据进行Base64编码,返回编码后的bytes对象。使用base64库进行解码的步骤如下:将需要解码的Base64编码字符串转换为bytes类型。调用b64decode()函数,将Base64编码的数据进行解码,返回解码后的bytes对象。Pillow库是Python中常用的图像处理库,它的Image模块提供了图像的编码和解码功能。使用Pillow库进行图像编码的步骤如下:打开需要编码的图像文件,得到一个Image对象。调用tobytes()方法将图像数据转换为bytes类型。使用base64库的b64encode()函数对图像数据进行Base64编码,返回编码后的bytes对象。使用Pillow库进行图像解码的步骤:将Base64编码的图像数据转换为bytes类型。使用base64库的b64decode()函数对图像数据解码
资源推荐
资源详情
资源评论
收起资源包目录
python的base64和pillow库实现的编码解码程序.zip (4个子文件)
python的base64和pillow库实现的编码解码程序
encode-decode.py 3KB
shield.ico 66KB
encode-decode_output.jpg 63KB
shield.png 5KB
共 4 条
- 1
资源评论
传奇开心果编程
- 粉丝: 7902
- 资源: 336
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功