先看源码:(其他在文章最后面讲)(Python3+)
print("正在初始化:")
print(" 初始化模块中... 0%")
import subprocess
sj = {
"版本号":"v.0.2.0",
"语言":"中文",
"启动次数":0,
}
import time
from tkinter import *
from tkinter.messagebox import *
from tkinter.ttk import *
from tkinter.simpledialog import *
import pyautogui # 用于关闭窗口
pyautogui.PAUSE = 0.1
pyautogui.FAILSAFE = False
print(" 初始化模块中... 100%")
print(" 初始化数据中... 0%")
lj = [
["0","0000"],
["1","0001"],
["2","0010"],
["3","0011"],
["4","0100"],
["5","0101"],
["6","0110"],
["7","0111"],
["8","1000"],
["9","1001"],
]# 1010 换数字
# 1100 换字符
help = [
["本工具是一款"]
]
print(" 初始化数据中... 100%")
print(" 初始化函数中... 0%")
def am_a(y,key):
mw = ""
mw_lj = []
mw_ljj = []
# 转换一维列表
for n in range(len(y)):
mw_lj.append(y[n])
# 转换为数字
for n in range(len(mw_lj)):
mw_ljj.append(str(ord(str(mw_lj[int(n)])) + key))
# 转换为字符串
for n in range(len(mw_ljj)):
mw += mw_ljj[n]
mw += ","
return mw
def em_a(m,key):
mw = ""
mw_lj = []
mw_ljj = []
# 转换一维列表
x = ""
for n in range(len(m)):
if m[n] == ',':
mw_lj.append(x)
x = ""
else :
x += m[n]
# 转换为字符
for n in range(len(mw_lj)):
mw_ljj.append(str(chr(int(mw_lj[int(n)]) - key)))
# 转换为字符串
for n in range(len(mw_ljj)):
mw += mw_ljj[n]
return mw
def am_b(y,key):
mw = ""
mw_lj = []
mw_ljj = []
# 转换一维列表
for n in range(len(y)):
mw_lj.append(y[n])
# 转换为数字
for n in range(len(mw_lj)):
sz = str(ord(str(mw_lj[int(n)])) + key)
f = ""
for i in range(len(sz)):
for x in lj:
if x[0] == sz[i]:
f += x[1]
f += "1010"
break
mw_ljj.append(f)
# 转换为字符串
for n in range(len(mw_ljj)):
mw += mw_ljj[n]
mw += "1100"
return mw
# 1010 换数字
# 1100 换字符
def em_b(m,key):
mw = ""
mw_lj = []
mw_ljj = []
# 转换一维列表
x = []
for n in range(len(m)//4):
if n == 0:
if m[n:n+4] == '1010': # 如果换数字
x.append(m[n:n+4]) # 单个字符的列表修改
elif m[n:n+4] == '1100': # 如果换字符
mw_lj.append(x)
x = []
else :
for i in lj:
if i[1] == m[n:n+4]:
x += i[0]
else:
if m[n*4:n*4+4] == '1010': # 如果换数字
x.append(m[n*4:n*4+4]) # 单个字符的列表修改
elif m[n*4:n*4+4] == '1100': # 如果换字符
mw_lj.append(x)
x = []
else :
for i in lj:
if i[1] == m[n*4:n*4+4]:
x += i[0]
# 转换为数字
e = []
for n in range(len(mw_lj)):
for i in range(len(mw_lj[n])):
if len(mw_lj[n][i]) == 1:
e.append(mw_lj[n][i])
mw_ljj.append(e)
e = []
# 单个数字组合一个数字
mw_sz = []
for n in range(len(mw_ljj)):
t = ""
for w in mw_ljj[n]:
t += w
mw_sz.append(int(t))
# 转换为字符
mw_ljjj = []
for n in range(len(mw_sz)):
mw_ljjj.append(chr(mw_sz[n] - key))
# 转换为字符串
for n in range(len(mw_ljjj)):
mw += mw_ljjj[n]
return mw
ti = False
def sr(w,h):
text = askstring("Plaintext ciphertext converter",f"请输入{w}(字符串):")
if text == None:
showinfo("已取消执行。")
return
k = askinteger("Plaintext ciphertext converter","请输入密钥以加密/解密(正整数):")
if k == None:
showinfo("Plaintext ciphertext converter","已取消执行。")
return
if h == "am_a":
showinfo("Plaintext ciphertext converter",am_a(text,k))
print("密文(可手动复制):" + am_a(text,k))
elif h == "em_a":
showinfo("Plaintext ciphertext converter",em_a(text,k))
print("密文(可手动复制):" + em_a(text,k))
elif h == "am_b":
showinfo("Plaintext ciphertext converter",am_b(text,k))
print("原文(可手动复制):" + am_b(text,k))
elif h == "em_b":
showinfo("Plaintext ciphertext converter",em_b(text,k))
print("原文(可手动复制):" + em_b(text,k))
else :
a = showerror("Plaintext ciphertext converter","Plaintext ciphertext converter 出现了错误!请根据错误联系开发者。\n错误:错误代码:0001")
return
print(" 初始化函数中... 100%")
print("创建窗口:")
print("创建主窗口中... 0%")
# 窗口部分
root = Tk()
root.geometry('1000x600')
root.title(f'Plaintext ciphertext converter {sj["版本号"]}')
lf1 = LabelFrame(root,text="字符串相关",width=550,height=100)
lf1.pack(padx=10,pady=10)
btn1 = Button(lf1,text="字符串加密A型",command=lambda:sr("明文","am_a"))
btn1.pack(side=LEFT,padx=10,pady=10)
btn2 = Button(lf1,text="字符串解密A型",command=lambda:sr("密文","em_a"))
btn2.pack(side=LEFT,padx=10,pady=10)
btn3 = Button(lf1,text="字符串解密B型",command=lambda:sr("明文","am_b"))
btn3.pack(side=LEFT,padx=10,pady=10)
btn4 = Button(lf1,text="字符串解密B型",command=lambda:sr("密文","em_b"))
btn4.pack(side=LEFT,padx=10,pady=10)
print("创建主窗口中... 100%")
root.mainloop()
A型加密简单,但容易攻破。B型加密全是0和1,密文会很长。
B型的原理是:
1、GUI窗口选择加密方式、输入原文
2、将原文的每个字符转换为ANSII编码(ord函数)并给每个字符加上密钥
3、遍历lj数组转换为0和1,它大家可以随意调整
4、把0和1全部放在一起
(还可以增加字符串颠倒、假数据等功能,但一般情况下已经够了)
A型的原理是B型的原理的1、2条,用逗号隔开,推荐使用A型。
不用任何模块也可以实现,但黑窗口体验效果不怎么好。
碍于篇幅限制,大家还可以加上窗口图标、日志(在源代码中都被我删掉了)从而更美观、易维护。