-*- coding: utf-8 -*-
from tkinter import *
import tkinter
import hashlib
import time
import codecs
import codecs
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
LOG_LINE_NUM = 0
class MY_GUI():
def init(self,init_window_name):
self.init_window_name = init_window_name
#设置窗口
def set_init_window(self):
self.init_window_name.title("维吉尼亚密码工具") #窗口名
self.init_window_name.geometry('320x160+10+10') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.init_window_name.geometry('1000x680+10+10')
self.init_window_name["bg"] = "white" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
self.init_window_name.attributes("-alpha",1.0) #虚化,值越小虚化程度越高
self.init_data_label = Label(self.init_window_name, text="待处理数据")
self.init_data_label.grid(row=0, column=0)
self.result_data_label = Label(self.init_window_name, text="输出结果")
self.result_data_label.grid(row=0, column=15)
self.init_key_label = Label(self.init_window_name, text="密钥")
self.init_key_label.grid(row=0, column=10)
self.log_label = Label(self.init_window_name, text="日志")
self.log_label.grid(row=12, column=0)
self.init_data_Text = Text(self.init_window_name, width=50, height=35) # 原始数据录入框
self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
self.result_data_Text = Text(self.init_window_name, width=35, height=49) # 处理结果展示
self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
self.key_Text = Text(self.init_window_name, width=5, height=1) # 密钥框
self.key_Text.grid(row=1, column=6, rowspan=1,columnspan=10)
self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
self.log_data_Text.grid(row=13, column=0, columnspan=10)
#按钮
self.str_transtoencrypt_button = Button(self.init_window_name, text="加密", bg="lightblue", width=10,command=self.str_transtoencrypt) # 调用内部方法 加()为直接调用
self.str_transtoencrypt_button.grid(row=3, column=11)
self.str_transtodecrypt_button = Button(self.init_window_name, text="解密", bg="lightblue", width=10,command=self.str_transtodecrypt) # 调用内部方法 加()为直接调用
self.str_transtodecrypt_button.grid(row=5, column=11)
self.str_count_button = Button(self.init_window_name, text="词频统计", bg="lightblue", width=10,command=self.str_count) # 调用内部方法 加()为直接调用
self.str_count_button.grid(row=7, column=11)
#功能算法的实现
#加密算法
def str_transtoencrypt(self):
letter_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
src = self.init_data_Text.get(1.0,END).strip().replace("\n","") #获得输入的字符
key = self.key_Text.get(1.0, END).strip().replace("\n", "") #获得输入的密钥
a=[]
for ch in key:
a.append(ord(ch.upper())-65) #ord()函数返回ASCii值 upper()将小写转大写Python 列表 append() 方法用于在列表末尾追加新的对象。
get_list = a
ciphertext = '';
i = 0
for ch in src: # 遍历明文
if i % len(get_list) == 0:
i = 0
if ch.isupper(): # isupper函数检测字符串中是否都是大写字母
ciphertext += letter_list[(ord(ch) - 65 + get_list[i]) % 26]
i += 1
else:
ciphertext += letter_list[(ord(ch) - 97 + get_list[i]) % 26].lower()
i += 1
self.result_data_Text.delete(1.0, END)
self.result_data_Text.insert(1.0, ciphertext)
#解密算法
def str_transtodecrypt(self):
letter_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
src = self.init_data_Text.get(1.0, END).strip().replace("\n", "") # 获得输入的字符
key = self.key_Text.get(1.0, END).strip().replace("\n", "") # 获得输入的密钥
a=[]
for ch in key:
a.append(ord(ch.upper())-65) #ord()函数返回ASCii值 upper()将小写转大写Python 列表 append() 方法用于在列表末尾追加新的对象。
get_list = a
ciphertext = '';
i = 0
plaintext='';
i = 0
for ch in src: #遍历密文
if i%len(get_list)==0:
i=0
#判断是否是大写
if ch.isupper():
plaintext+=letter_list[(ord(ch)-65-get_list[i]) % 26]
i+=1
else:
plaintext+=letter_list[(ord(ch)-97-get_list[i]) % 26].lower()
i+=1
self.result_data_Text.delete(1.0, END)
self.result_data_Text.insert(1.0, plaintext)
#词频统计及统计图
def str_count(self):
top = Toplevel()
top.title('词频统计结果')
top.geometry('1000x1000')
fr =self.init_data_Text.get(1.0,END).strip().replace("\n","")
word = []
counter = {}
for line in fr:
line = line.strip()
if len(line) == 0:
continue
for w in line:
if not w in word:
word.append(w)
if not w in counter:
counter[w] = 0
else:
counter[w] += 1
counter_list = sorted(counter.items(), key=lambda x: x[1], reverse=True)
print(counter_list[:50])
label = list(map(lambda x: x[0], counter_list[:50]))
value = list(map(lambda y: y[1], counter_list[:50]))
plt.bar(range(len(value)), value, tick_label=label)
plt.show()
def gui_start():
init_window = Tk() #实例化出一个父窗口
RMC_PORTAL = MY_GUI(init_window)
设置根窗口默认属性
RMC_PORTAL.set_init_window()
init_window.mainloop() #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
gui_start()