猫史档案馆


【Python作品分享】加密与解密【作品秀】

用户:随风6ar_随风6ar_查看:0 回复:1 评论:0 创建时间:2022-01-29T10:40:14


【作品展示】

center_image

 

【作品介绍】

1

 

【作品源代码】

import nbclient as nb
import easygui
import tkinter as tk
import tkinter
from tkinter import *
import sys
import easygui as e
import tkinter.messagebox
from PIL import Image, ImageTk
import pygame

# 导入pygame资源包
# 开始播放音乐流


def get_image(filename, width, height):
    im = Image.open(filename).resize((width, height))
    return ImageTk.PhotoImage(im)


root = Tk()
root.maxsize(490, 490)  # 左右高
root.title('加密与解密の个性化版')
root.resizable(False, False)
canvas_root = tk.Canvas(root, width=495, height=495)
im_root = get_image(
    '01d1a75b3484d4a80120b959e7c899.jpg@1280w_1l_2o_100sh.jpg', 490, 495)
canvas_root.create_image(245, 247, image=im_root)
canvas_root.pack()


def cut(event=None):
    e.msgbox('@蒸汽创客张明灿,电话:喵', '关于')


def copy(event=None):
    e.msgbox('您已完成个性化升级', '个性化')


def paste(event=None):
    file = r'李雨桐 - 我和我的祖国.mp3'		# 音乐的路径
    pygame.mixer.init()						# 初始化
    track = pygame.mixer.music.load(file)  # 加载音乐文件
    pygame.mixer.music.play()


def paste1(event=None):
    file = r'李雨桐 - 我和我的祖国.mp3'		# 音乐的路径
    pygame.mixer.init()						# 初始化
    track = pygame.mixer.music.load(file)  # 加载音乐文件
    pygame.mixer.music.pause()


texteditor = Text(root)
menubar = Menu(root)
filemenu = Menu(menubar)
root.config(menu=menubar)
filemenu.add_command(label="关于", command=cut)
filemenu.add_command(label="个性化", command=copy)
filemenu.add_command(label="播放音乐", command=paste)
filemenu.add_command(label="停止播放音乐", command=paste1)

menubar.add_cascade(label="集合", menu=filemenu)


isencry = False  # 为了防止点击多次加密出现问题
# 加密算法,参数:秘钥,文本


def encryption():
    key = inputkey.get()
    global isencry
    if len(key) > 2 and len(key) < 10 and isencry:
        text = inputtext.get(1.0, 'end')
        text2 = ''
    # 将key转化成ascii码列表
        newkey = []
        for i in key:
            newkey.append(ord(i))
        ii = 0
        for i in text:
            if ord(i) != 10:
                text2 = text2+chr(ord(i)+newkey[ii])  # 关键部分,加密公式
            else:
                text2 = text2+chr(10)
            ii = ii+1
            if ii >= len(newkey):
                ii = 0
        inputtext.delete(1.0, 'end')
        inputtext.insert('insert', text2)
    isencry = False
# 修改文本触发


def i喵odifytext(*o):
    global isencry
    isencry = True


tkinter.Label(
    root, text='[-----------------------------------加 密-----------------------------------]', bg='Moccasin').place(x=10, y=0)
# 设置秘钥
tkinter.Label(root, text="请您输入9位秘钥", bg='Moccasin').place(x=10, y=30)
ik = tkinter.StringVar()
inputkey = tkinter.Entry(root, textvariable=ik, width=36, bg='Moccasin')
inputkey.place(x=150, y=30)

# 输入要加密的文本
tkinter.Label(root, text="请您输入要加密的文本", bg='Moccasin').place(x=10, y=60)
inputtext = tkinter.Text(root, width=36, height=10, bg='Moccasin')
inputtext.place(x=150, y=60)
inputtext.bind('', i喵odifytext)
# 点击加密
tkinter.Button(root, text='进行加密', bg='Moccasin', width=14,
               command=encryption).place(x=10, y=198)

# 解密
isdecry = False  # 为了防止点击多次解密出现bug
# 解密算法,参数:秘钥,文本


def decrypt():
    key = inputkey.get()
    global isdecry
    if len(key) > 2 and len(key) < 10 and isdecry:
        text = inputtext2.get(1.0, 'end')
        text2 = ''
        #将key转化成ascii码列表
        newkey = []
        for i in key:
            newkey.append(ord(i))
        ii = 0
        for i in text:
            if ord(i) != 10:
                text2 = text2+chr(ord(i)-newkey[ii])  # 关键部分,解密公式
            else:
                text2 = text2+chr(10)
            ii = ii+1
            if ii >= len(newkey):
                ii = 0
        inputtext2.delete(1.0, 'end')
        inputtext2.insert('insert', text2)
    isdecry = False
#修改文本触发


def i喵odify2(*o):
    global isdecry
    isdecry = True


tkinter.Label(
    root, text='[-----------------------------------解 密-----------------------------------]', bg='Moccasin').place(x=10, y=250)
# 设置秘钥
tkinter.Label(root, text="请您输入9位秘钥", bg='Moccasin').place(x=10, y=280)
ik2 = tkinter.StringVar()
inpurootey2 = tkinter.Entry(root, textvariable=ik2, width=36, bg='Moccasin')
inpurootey2.place(x=150, y=280)
# 输入要解密的文本
tkinter.Label(root, text="请您输入要解密的文本", bg='Moccasin').place(x=10, y=310)
inputtext2 = tkinter.Text(root, width=36, height=10, bg='Moccasin')
inputtext2.place(x=150, y=310)
inputtext2.bind('', i喵odify2)
# 点击解密
tkinter.Button(root, text='进行解密', bg='Moccasin', width=14,
               command=decrypt).place(x=10, y=448)


root.mainloop()  # 显示窗口

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
EUWEUW

不如我的 https://shequ.codemao.cn/community/1626931

点赞0


评论