基于GUI界面的凯撒密码实现

基于GUI界面的凯撒密码实现涉及到2个模块,分别为凯撒密码加密,解密逻辑实现,和GUI展示模块.想对以前写的凯撒密码做一个简单扩展,练习练习TKINT,故写了此篇.

凯撒密码

涉及到对密码的加密与解密,加密过程比较简单,通过移位操作就可以完成,解密需要知道移动多少位,这里笔者使用基于统计实现,感兴趣可以看我第一篇博客. 后面有GUI代码,完整代码见github

GUI设计

分别包括用户输入,3个按钮, 选择移动key,如下图所示:

j加密步骤如下,

第一步 选择输入文本为(随便在2020AAAI官网copy的): "The Thirty-Fifth AAAI Conference on Artificial Intelligence (AAAI-21) will be held virtually February 2-9, 2021. (Note that AAAI will go to Vancouver, Canada in 2022!). The general chair will be Qiang Yang (Hong Kong University of Science and Technology, Hong Kong) and the program chairs will be Kevin Leyton-Brown (University of British Columbia, Canada) and Mausam (Indian Institute of Technology Delhi, India). The purpose of the AAAI conference is to promote research in artificial intelligence (AI) and scientific exchange among AI researchers, practitioners, scientists, and engineers in affiliated disciplines. AAAI-21 will have a diverse technical track, student abstracts, poster sessions, invited speakers, tutorials, workshops, and exhibit and competition programs, all selected according to the highest reviewing standards. AAAI-21 welcomes submissions on mainstream AI topics as well as novel crosscutting work in related areas."

第二步 选择key

第三步 选择加密

没有选择key时会警告!!!

解密步骤同上:

改为点击解密即可,不需要输入key, 为了方便,我使用了上面加密结果,由于有特殊字符,解密结果有部分不是很理想(测试用例比较短的原因),我试其他纯字母长文本时,效果基本上解密成功!

其他测试结果:


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 31 18:43:45 2020

@author: 1002
"""
import tkinter as tk

from tkinter import ttk

from CaesarCiper import CaesarCiper


window=tk.Tk()
window.title('Caesar Cipher')  #窗口名字  
window.geometry('800x600')   #窗口size
'''
# Entry的第一个参数是父窗口,即这里的window
# *表示输入的文本变为星号,在Entry不可见内容,若为None则表示为输入文本以原形式可见
e=tk.Entry(window,show='*')
e.pack()
'''

#文本输入
inputTxt=tk.Text(window,height=10)  
inputTxt.pack(side = 'top')

optCode = CaesarCiper()


def encode():
    var = inputTxt.get('0.0','end')
    k = process_combobox()
    codeText = optCode.encoder(var,k)
    var = "out>>"+codeText
    resultTxt.insert('insert',var)

    
def decode():
    var = inputTxt.get('0.0','end')
    decodeText = optCode.decoder(var)
    var = "out>>"+decodeText
    resultTxt.insert('end',var)
    
def clear_input():
    inputTxt.delete('0.0','end')

def process_combobox():
    varCom  = com.get()
    if varCom == "下拉选择移动key值":
        key = 0
        resultTxt.insert('end',"(warning,没有选择key,默认key为0)")
        return 0
    else:
        key = int(varCom)
        return key
    
        
#这里的end表示插入在结尾,可以换为1.2,则插入在第一行第二位后面
b1=tk.Button(window,text='凯撒加密',width=15,height=2,command=encode)
b1.pack()

b1=tk.Button(window,text='凯撒解密',width=15,height=2,command=decode)
b1.pack()

b2=tk.Button(window,text='清空',width=15,height=2,command=clear_input)
b2.pack()

#文本输出
resultTxt=tk.Text(window,height=30)     #这里设置文本框高,可以容纳两行
resultTxt.pack(side = 'left')

#下拉框
cv= tk.StringVar()
com=ttk.Combobox(window,text='key',textvariable=cv)
com.pack()
#设置下拉数据
com["value"]=("下拉选择移动key值","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26")
#设置默认值
com.current(0)

key =0

window.mainloop()

猜你喜欢

转载自blog.csdn.net/qq_39463175/article/details/107850079