目录
每周更新一下,(可能还不是那么完善)希望支持一下。
定义一个包括加密和解密的类
import wx#导入wxpython
class encry_commut:#定义encry_commut类
加密函数
用随机数加密后返回信息和密钥
# def __init__(self):
alphabet = [chr(i) for i in range(97, 123)]
alphabet.append(' ')#创建字母表
def enchipher(self, information):#infomation,要加密的信息
import random#导入随机类
encrylist = []
cipher1 = []
encry = []#infomation变成的列表
for i in range(len(information)):
encry.append(information[i])#将字符串替换为列表
if 1 == 1:#凑数
# space=[]
encrylist.append('b')#迷惑性行为
encry_trans = []#将转换后的encry加入
# location=-1
for i in range(len(encry)):
ord1 = ord(encry[i])#将每个字变成ord编码
if len(str(ord1)) < 5:
ord1 = '0' * (5 - len(str(ord1))) + str(ord1)#统一格式
encry_trans.append(ord1)
order = ''.join('%s' % id for id in encry_trans)#字符串化后的encry_trans
for i in range(len(order)):
random1 = random.randint(0, 9)
# order = alphabet.index(encry[i])
order1 = int(order[i]) + random1#每个字符随机加密
cipher1.append(f'{random1}')#加入密钥
if order1 < 27:
encrylist.append(encry_commut.alphabet[order1])#加入转换为字母的信息
else:
order1 -= 26#如超过,重新开始
encrylist.append(encry_commut.alphabet[order1])
chipherstr=''.join('%s' % id for id in cipher1)
encryliststr=''.join('%s' % id for id in encrylist)
return [encryliststr,chipherstr]#返回值
# return ''.join('%s' % id for id in cipher1)
解密函数
输入信息和密钥后返回原本的信息
def decrypt(self,decrypt_information,cipher):
text=[]#解密
if decrypt_information[0]=='b':
section=[]
in1=decrypt_information[1:len(decrypt_information)+1]
chipher_list=[]
for i in range(len(in1)):
chipher_list1=encry_commut.alphabet.index(in1[i])-int(cipher[i])#转换为数字
if chipher_list1<0:
chipher_list1+=27
chipher_list.append(chipher_list1)
for i in range(0,len(chipher_list),5):
section1=(chipher_list[i:i+5])
section.append(''.join('%s' % id for id in section1))#五个分为一组,再转换为中文
for i in range(len(section)):
text.append(chr(int(section[i])))
textnumber = ''.join(text)
# textnumber.replace(' ', '')
return textnumber
定义一个界面类
class MyApp(wx.App):
主界面
可选择是加密还是解密。
有两个按钮,分别是加密和解密。
def OnInit(self):
frame=wx.Frame(parent=None,title='my first interface')
panel = wx.Panel(frame,-1)
label1=wx.StaticText(panel,-1,'需要加密还是解密?',pos=(30,10))#标签
button1=wx.Button(panel,-1,'加密',pos=(80,110))
button2=wx.Button(panel,-1,'解密',pos=(180,110))
self.button1=button1#两个按钮
self.button2=button2
self.Bind(wx.EVT_BUTTON,#绑定事件
self.ench,
self.button1)
self.Bind(wx.EVT_BUTTON,
self.decr,
self.button2)
frame.Show()
return True
加密界面
把解密函数包装一下,并且设置一个完成按钮。
def ench(self,event):#按钮绑定的事件
ench1 = wx.Frame(None, title='加密')
lable1=wx.StaticText(ench1,-1,'需要加密的信息',pos=(10,10))
self.text1=wx.TextCtrl(ench1,pos=(130,10),size=(180,30),style=wx.TE_MULTILINE)#需要加密的信息填入
self.button3=wx.Button(ench1,-1,'确定',pos=(110,170))
self.Bind(wx.EVT_BUTTON,self.ture1,self.button3)
ench1.Show()
self.ench1=ench1
按下完成后显示+设置两个复制按钮
def ench(self,event):#按钮绑定的事件
ench1 = wx.Frame(None, title='加密')
lable1=wx.StaticText(ench1,-1,'需要加密的信息',pos=(10,10))
self.text1=wx.TextCtrl(ench1,pos=(130,10),size=(180,30),style=wx.TE_MULTILINE)#需要加密的信息填入
self.button3=wx.Button(ench1,-1,'确定',pos=(110,170))
self.Bind(wx.EVT_BUTTON,self.ture1,self.button3)
ench1.Show()
self.ench1=ench1
复制按钮:
def login(self, event):#复制按钮绑定的事件
text =self.list1[0]
data_object = wx.TextDataObject(text)
wx.TheClipboard.SetData(data_object)#放入剪贴板
#复制
def login2(self, event):#复制按钮绑定的事件
text1 =self.list1[1]
data_object1 = wx.TextDataObject(text1)
wx.TheClipboard.SetData(data_object1)
#复制
解密界面
只是把解密函数包装了一下
def decr(self,event):#按钮绑定的事件
decr1 = wx.Frame(None, title='解密')
lable2=wx.StaticText(decr1,-1,'需要解密的信息',pos=(10,10))#用定义好的解密函数
self.text2=wx.TextCtrl(decr1,pos=(130,10),size=(180,30),style=wx.TE_MULTILINE)#需要解密的信息填入
lable3=wx.StaticText(decr1,-1,'密钥',pos=(10,60))#标签
self.text3=wx.TextCtrl(decr1,pos=(130,60),size=(180,30),style=wx.TE_MULTILINE)
self.button4=wx.Button(decr1,-1,'确定',pos=(110,110))
self.Bind(wx.EVT_BUTTON,self.ture2,self.button4)
decr1.Show()
self.decr1=decr1
def ture2(self,event):
encry2=encry_commut()
secret2=self.text2.GetValue()#得到文本框里的内容
secret3 = self.text3.GetValue()
str1=encry2.decrypt(secret2,secret3)
lable5=wx.StaticText(self.decr1,-1,f'{str1}',pos=(10,150),style=wx.ALIGN_CENTRE_HORIZONTAL)
执行
app = MyApp()
app.MainLoop()
完整代码:
import wx#导入wxpython
class encry_commut:#定义encry_commut类
# def __init__(self):
alphabet = [chr(i) for i in range(97, 123)]
alphabet.append(' ')#创建字母表
def enchipher(self, information):#infomation,要加密的信息
import random#导入随机类
encrylist = []
cipher1 = []
encry = []#infomation变成的列表
for i in range(len(information)):
encry.append(information[i])#将字符串替换为列表
if 1 == 1:#凑数
# space=[]
encrylist.append('b')#迷惑性行为
encry_trans = []#将转换后的encry加入
# location=-1
for i in range(len(encry)):
ord1 = ord(encry[i])#将每个字变成ord编码
if len(str(ord1)) < 5:
ord1 = '0' * (5 - len(str(ord1))) + str(ord1)#统一格式
encry_trans.append(ord1)
order = ''.join('%s' % id for id in encry_trans)#字符串化后的encry_trans
for i in range(len(order)):
random1 = random.randint(0, 9)
# order = alphabet.index(encry[i])
order1 = int(order[i]) + random1#每个字符随机加密
cipher1.append(f'{random1}')#加入密钥
if order1 < 27:
encrylist.append(encry_commut.alphabet[order1])#加入转换为字母的信息
else:
order1 -= 26#如超过,重新开始
encrylist.append(encry_commut.alphabet[order1])
chipherstr=''.join('%s' % id for id in cipher1)
encryliststr=''.join('%s' % id for id in encrylist)
return [encryliststr,chipherstr]#返回值
# return ''.join('%s' % id for id in cipher1)
def decrypt(self,decrypt_information,cipher):
text=[]#解密
if decrypt_information[0]=='b':
section=[]
in1=decrypt_information[1:len(decrypt_information)+1]
chipher_list=[]
for i in range(len(in1)):
chipher_list1=encry_commut.alphabet.index(in1[i])-int(cipher[i])#转换为数字
if chipher_list1<0:
chipher_list1+=27
chipher_list.append(chipher_list1)
for i in range(0,len(chipher_list),5):
section1=(chipher_list[i:i+5])
section.append(''.join('%s' % id for id in section1))#五个分为一组,再转换为中文
for i in range(len(section)):
text.append(chr(int(section[i])))
textnumber = ''.join(text)
# textnumber.replace(' ', '')
return textnumber
class MyApp(wx.App):
def OnInit(self):
frame=wx.Frame(parent=None,title='my first interface')
panel = wx.Panel(frame,-1)
label1=wx.StaticText(panel,-1,'需要加密还是解密?',pos=(30,10))#标签
button1=wx.Button(panel,-1,'加密',pos=(80,110))
button2=wx.Button(panel,-1,'解密',pos=(180,110))
self.button1=button1#两个按钮
self.button2=button2
self.Bind(wx.EVT_BUTTON,#绑定事件
self.ench,
self.button1)
self.Bind(wx.EVT_BUTTON,
self.decr,
self.button2)
frame.Show()
return True
def ench(self,event):#按钮绑定的事件
ench1 = wx.Frame(None, title='加密')
lable1=wx.StaticText(ench1,-1,'需要加密的信息',pos=(10,10))
self.text1=wx.TextCtrl(ench1,pos=(130,10),size=(180,30),style=wx.TE_MULTILINE)#需要加密的信息填入
self.button3=wx.Button(ench1,-1,'确定',pos=(110,170))
self.Bind(wx.EVT_BUTTON,self.ture1,self.button3)
ench1.Show()
self.ench1=ench1
def ture1(self,event):#按钮绑定的事件
encry1=encry_commut()
secret=self.text1.GetValue()
self.list1=encry1.enchipher(secret)
lable3=wx.StaticText(self.ench1,-1,f'{self.list1[0]}',pos=(10,60))
lable4=wx.StaticText(self.ench1,-1,f'{self.list1[1]}',pos=(10,80))
button1 = wx.Button(self.ench1, -1, '复制信息', pos=(10,100))#复制按钮
button2 = wx.Button(self.ench1, -1, '复制密钥', pos=(100,100))
self.button1 = button1
self.button2=button2
self.Bind(wx.EVT_BUTTON,self.login,self.button1)#绑定事件
self.Bind(wx.EVT_BUTTON, self.login2, self.button2)#绑定事件
def login(self, event):#复制按钮绑定的事件
text =self.list1[0]
data_object = wx.TextDataObject(text)
wx.TheClipboard.SetData(data_object)#放入剪贴板
#复制
def login2(self, event):#复制按钮绑定的事件
text1 =self.list1[1]
data_object1 = wx.TextDataObject(text1)
wx.TheClipboard.SetData(data_object1)
#复制
def decr(self,event):#按钮绑定的事件
decr1 = wx.Frame(None, title='解密')
lable2=wx.StaticText(decr1,-1,'需要解密的信息',pos=(10,10))#用定义好的解密函数
self.text2=wx.TextCtrl(decr1,pos=(130,10),size=(180,30),style=wx.TE_MULTILINE)#需要解密的信息填入
lable3=wx.StaticText(decr1,-1,'密钥',pos=(10,60))#标签
self.text3=wx.TextCtrl(decr1,pos=(130,60),size=(180,30),style=wx.TE_MULTILINE)
self.button4=wx.Button(decr1,-1,'确定',pos=(110,110))
self.Bind(wx.EVT_BUTTON,self.ture2,self.button4)
decr1.Show()
self.decr1=decr1
def ture2(self,event):
encry2=encry_commut()
secret2=self.text2.GetValue()
secret3 = self.text3.GetValue()
str1=encry2.decrypt(secret2,secret3)
lable5=wx.StaticText(self.decr1,-1,f'{str1}',pos=(10,150),style=wx.ALIGN_CENTRE_HORIZONTAL)
app = MyApp()
app.MainLoop()
效果
(其实没有什么加密效果,可以把密钥加在信息后会好一些)