DES加密,Tk写的简单的GUI
# -*- coding: UTF-8 -*-
from Tkinter import *
from tkMessageBox import * ip = [
58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,
]
ip_1 = [
40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41, 9,49,17,57,25,
]
e = [
32, 1, 2, 3, 4, 5,
4 , 5, 6, 7, 8, 9,
8 , 9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32, 1,
]
p = [
16, 7,20,21,29,12,28,17,
1 ,15,23,26, 5,18,31,10,
2 ,8 ,24,14,32,27, 3, 9,
19,13,30, 6,22,11, 4,25,
]
sbox = [
[
[0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7],
[0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8],
[0x4,0x1,0xe,0x8,0xd,0x6,0x2,0xb,0xf,0xc,0x9,0x7,0x3,0xa,0x5,0x0],
[0xf,0xc,0x8,0x2,0x4,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x6,0xd]
],
[
[0xf,0x1,0x8,0xe,0x6,0xb,0x3,0x4,0x9,0x7,0x2,0xd,0xc,0x0,0x5,0xa],
[0x3,0xd,0x4,0x7,0xf,0x2,0x8,0xe,0xc,0x0,0x1,0xa,0x6,0x9,0xb,0x5],
[0x0,0xe,0x7,0xb,0xa,0x4,0xd,0x1,0x5,0x8,0xc,0x6,0x9,0x3,0x2,0xf],
[0xd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x8,0xc,0x3,0x7,0x0,0x4,0xa,0x1,0xd,0xb,0x6],
[0x4,0x3,0x2,0xc,0x9,0x5,0xf,0xa,0xb,0xe,0x1,0x7,0x6,0x0,0x8,0xd]
],
[
[0x4,0xb,0x2,0xe,0xf,0x0,0x8,0xd,0x3,0xc,0x9,0x7,0x5,0xa,0x6,0x1],
[0xd,0x0,0xb,0x7,0x4,0x9,0x1,0xa,0xe,0x3,0x5,0xc,0x2,0xf,0x8,0x6],
[0x1,0x4,0xb,0xd,0xc,0x3,0x7,0xe,0xa,0xf,0x6,0x8,0x0,0x5,0x9,0x2],
[0x6,0xb,0xd,0x8,0x1,0x4,0xa,0x7,0x9,0x5,0x0,0xf,0xe,0x2,0x3,0xc]
],
[
[0xd,0x2,0x8,0x4,0x6,0xf,0xb,0x1,0xa,0x9,0x3,0xe,0x5,0x0,0xc,0x7],
[0x1,0xf,0xd,0x8,0xa,0x3,0x7,0x4,0xc,0x5,0x6,0xb,0x0,0xe,0x9,0x2],
[0x7,0xb,0x4,0x1,0x9,0xc,0xe,0x2,0x0,0x6,0xa,0xd,0xf,0x3,0x5,0x8],
[0x2,0x1,0xe,0x7,0x4,0xa,0x8,0xd,0xf,0xc,0x9,0x0,0x3,0x5,0x6,0xb]
]
]
hex_bin = {
0x0:'',0x1:'',0x2:'',0x3:'',
0x4:'',0x5:'',0x6:'',0x7:'',
0x8:'',0x9:'',0xa:'',0xb:'',
0xc:'',0xd:'',0xe:'',0xf:''
}
bin_hex = {
'':'','':'','':'','':'',
'':'','':'','':'','':'',
'':'','':'','':'A','':'B',
'':'C','':'D','':'E','':'F'
}
hex_s = {
'':'','':'','':'','':'',
'':'','':'','':'','':'',
'':'','':'','A':'','B':'',
'C':'','D':'','E':'','F':''
}
#生成子密钥
def Rkey(cipherkey):
pc_1 = [
57,49,41,33,25,17, 9,
1 ,58,50,42,34,26,18,
10, 2,59,51,43,35,27,
19,11, 3,60,52,44,36,
63,55,47,39,31,23,15,
7 ,62,54,46,38,30,22,
14, 6,61,53,45,37,29,
21,13, 5,28,20,12, 4
]
pc_2 = [
14,17,11,24, 1, 5, 3,28,
15, 6,21,10,23,19,12, 4,
26, 8,16, 7,27,20,13, 2,
41,52,31,37,47,55,30,40,
51,45,33,48,44,49,39,56,
34,53,46,42,50,36,29,32
]
pc = [
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
] k0 = []
s = cipherkey
for i in range(8):
k0.append(s[i])
for i in range(len(k0)):
k0[i] = bin(int(hex(ord(k0[i])),16))[2:]
k = ''
for i in range(len(k0)):
if len(k0[i]) != 8:
k0[i] = (8-len(k0[i]))*'' + k0[i]
k = k+k0[i]
k1=''
for i in range(56):
k1 = k1 +k[pc_1[i]-1]
c0 = k1[0:28]
d0 = k1[28:]
c = []
d = []
for i in range(16):
if i == 0:
c.append(c0[pc[i]:]+c0[i])
d.append(d0[pc[i]:]+d0[i])
else:
c.append(c[i-1][pc[i]:]+c[i-1][0:pc[i]])
d.append(d[i-1][pc[i]:]+d[i-1][0:pc[i]])
K_0 = [] #进行pc_2置换并存到列表K_1中
K_1 = []
for i in range(16):
K_0.append(c[i]+d[i])
for i in range(16):
s = ''
for j in range(48):
s = s + K_0[i][pc_2[j]-1]
K_1.append(s)
return K_1
#F函数
def des_F(R,Ki):
R1 = ''
for i in range(48):
R1 = R1 + R[e[i]-1]
#print R1
R2 = ''
for i in range(48):
if R1[i] == Ki[i]:
s = ''
else:
s = ''
R2 = R2 + s
R3 = []
S = [0,6,12,18,24,30,36,42]
for i in S:
R3.append(R2[i:(i+6)])
S1 = ''
for i in range(8):
row = int((R3[i][0]+R3[i][5]),2) #hang
col = int((R3[i][1:5]),2) #lie
l = sbox[i][row][col] S1 = S1 + hex_bin[l]
S2 = ''
for i in range(32):
S2 = S2 +S1[p[i]-1]
return S2 #加密 def des_Encode(cleartext,cipherkey):
K = Rkey(cipherkey)
s = str(cleartext)
s0 = []
for i in range(len(s)):
s0.append(bin(int(hex(ord(s[i])),16))[2:])
if len(s0) != 8:
for i in range((8 - len(s0))):
s0.append('')
s1 = ''
for i in range(8):
if len(s0[i]) != 8:
s0[i] = (8-len(s0[i]))*'' + s0[i]
s1 = s1 + s0[i]
#初始置换
s2 = ''
for i in range(64):
s2 = s2 + s1[ip[i]-1] L0 = s2[0:32]
R0 = s2[32:]
L = []
R = []
for i in range(16):
if i == 0:
L.append(L0)
R.append(R0)
else:
Li = R[i-1]
ans = des_F(R[i-1],K[i-1])
Ri = ''
for j in range(32):
if L[i-1][j] == ans[j]:
ss = ''
else:
ss = ''
Ri = Ri + ss
L.append(Li)
R.append(Ri)
ans1 = des_F(R[15],K[15])
L_15 = R[14]
L_16 = ''
#异或
for i in range(32):
if L_15[i] == ans1[i]:
tt = ''
else:
tt = ''
L_16 = L_16 + tt
R_16 = R[15]
C = ''
xx =L_16+R_16
#逆初始置换
for i in range(64):
C = C +(xx)[ip_1[i]-1]
#转换为16进制
binhex = [0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]
binbox = []
for i in binhex:
binbox.append(C[i:(i+4)])
C_hex = ''
for i in range(16):
C_hex = C_hex + str(bin_hex[binbox[i]]) return C_hex
#解密
def des_Decode(ciphertext,cipherkey):
text = ciphertext
K_l = Rkey(cipherkey)
K_list =[]
for i in range(16):
K_list.append(K_l[15-i]) text1 = ''
for i in text:
text1 = text1 + hex_s[i]
text2 = ''
for i in range(64):
text2 = text2 + text1[ip[i]-1]
L0 = text2[0:32]
R0 = text2[32:]
L = []
R = []
for i in range(16):
if i == 0:
L.append(L0)
R.append(R0)
else:
L_next = R[i-1]
R_1 = des_F(R[i-1],K_list[i-1])
R_next = ''
for j in range(32):
if L[i-1][j] == R_1[j]:
s = ''
else:
s = ''
R_next = R_next +s
L.append(L_next)
R.append(R_next)
L16 = ''
ans2 = des_F(R[15],K_list[15])
for i in range(32):
if L[15][i] == ans2[i]:
ss = ''
else:
ss = ''
L16 = L16 + ss
R16 = R[15]
L_R = L16 + R16
m_bin = ''
for i in range (64):
m_bin = m_bin + L_R[ip_1[i]-1]
bin_list = [0,8,16,24,32,40,48,56]
asc_list = []
for i in bin_list:
asc_list.append(int(m_bin[i:(i+8)],2))
m_string = ''
for i in asc_list:
m_string = m_string + chr(int(i))
return m_string class window():
def __init__(self,top):
top.title('Des v1.0')
top.geometry('560x380+500+200')
#top.iconbitmap('G:/1.ico')
top.resizable(0,0)
label1 = Label(top,text = ('明 文:').decode('utf-8'))
label1.place(x=0,y=4)
self.t = StringVar()
self.entry1 = Entry(top,width = 80,textvariable = self.t)
self.entry1.place(x=70,y=5)
label2 = Label(top,text = ('密 钥:').decode('utf-8'))
label2.place(x=0,y=32)
self.entry2 = Entry(top,width = 80)
self.entry2.place(x=70,y=33)
self.button1 = Button(top,text = ('加 密').decode('utf-8'),width = 10,height = 1,command = self.Encode_button)
self.button1.place(x=70,y=63)
self.button2 = Button(top,text = ('解 密').decode('utf-8'),width = 10,height = 1,command = self.Decode_button)
self.button2.place(x=160,y=63)
self.button3 = Button(top,text = ('显示子密钥').decode('utf-8'),width = 10,height = 1,command = self.Show_key)
self.button3.place(x=250,y=63)
self.button4 = Button(top,text = ('清空密钥').decode('utf-8'),width = 10,height = 1,command = self.Clear_text)
self.button4.place(x=340,y=63)
label3 = Label(top,text = ('密 文:').decode('utf-8'))
label3.place(x=0,y=93)
self.e = StringVar()
self.entry3 = Entry(top,width = 80,textvariable=self.e)
self.entry3.place(x=70,y=93)
label4 = Label(top,text = ('子 密 钥:').decode('utf-8'))
label4.place(x=0,y=123)
self.text2=Text(top,selectbackground = 'gray',selectforeground = 'red',width = 60)
self.text2.place(x=70,y=123)
def Encode_button(self):
try:
clear_text = self.entry1.get()
cipher_key = self.entry2.get()
text_list = []
if (len(clear_text)/8)*8 != len(clear_text):
for i in range((len(clear_text)/8)):
text_list.append(clear_text[i*8:(i+1)*8])
text_list.append(clear_text[(len(clear_text)/8)*8:])
else:
for i in range((len(clear_text)/8)):
text_list.append(clear_text[i*8:(i+1)*8])
ans =''
for i in text_list:
ans = ans + des_Encode(i,cipher_key)
self.e.set(ans) except:
showwarning('Warning','Error!')
def Show_key(self):
try:
cipher_key = self.entry2.get()
key = Rkey(cipher_key)
for i in range(16):
self.text2.insert(END,'K'+str(i+1)+': '+key[i]+'\n') except:
showwarning('Warning','Error!')
def Clear_text(self):
self.text2.delete(1.0,END)
def Decode_button(self):
try:
cipher_text = self.entry3.get()
cipher_key = self.entry2.get()
mingwen = des_Decode(cipher_text,cipher_key)
self.t.set(mingwen)
except:
showwarning('Warning','Error!') top = Tk()
window = window(top)
top.mainloop()
DES加密,Tk写的简单的GUI的更多相关文章
- C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解
之前工作上需要用C++把软件生成的用户序列号用des加密cbc的模式,加密后为二进制,转化为十六进制,然后提供给java写的授权码管理平台. java平台会根据用户序列号,生成一个授权码,授权码是用r ...
- java 简单的des加密示例
1.加密结果 包含 : 对int加密 .对string加密.对byte[]加密. 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/Ciph ...
- 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)
原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...
- RAS、AES、DES加密
---------------------------------------------------------------------------------------------------- ...
- BS结构中,web如何将数据进行DES加密并写道IC卡中
在IC卡应用系统中,一般都要对IC卡数据进行DES加密,以保证数据的安全.友我科技RFID读写器云服务2.0充分考虑了这个需求,只需要软件工程师简单的配置即可实现数据的加解密并且写到数据块中.如下图所 ...
- DES加密模式详解
DES加密模式详解 http://www.cnblogs.com/Lawson/archive/2012/05/20/2510781.html http://www.blogjava.net/wayn ...
- IOS-plist文件DES加密
转载请说明出处:http://www.cnblogs.com/gexun/p/3705207.html,谢谢. 这些天一直在做一个知识问答的项目,因为初赛的项目题目比较少,所以题目就写在本地的plis ...
- Android数据加密之Des加密
前言: 端午节前有个同事咨询我有关Android DES加密的相关实现,简单的实现了一下,今天来总结一下. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes加密 ...
- des加密解密——java加密,php解密
最近在做项目中,遇到des加密解密的问题. 场景是安卓app端用des加密,php这边需要解密.之前没有接触过des这种加密解密算法,但想着肯定会有demo.因此百度,搜了代码来用.网上代码也是鱼龙混 ...
随机推荐
- mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句
http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases; ...
- 数位dp入门 hdu2089 不要62
数位dp入门 hdu2089 不要62 题意: 给定一个区间[n,m] (0< n ≤ m<1000000),找出不含4和'62'的数的个数 (ps:开始以为直接暴力可以..貌似可以,但是 ...
- Codeforces Round #336 (Div. 1) A - Chain Reaction
Chain Reaction 题意:有n(1 ≤ n ≤ 100 000) 个灯泡,每个灯泡有一个位置a以及向左照亮的范围b (0 <= a <= 1e6 ,1<= b <= ...
- 在ios开发中有多少常用的加密解密方式(备用)
最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...
- jquery点击图片选中特效
jquery点击图片选中特效 点击在线预览效果
- 使AspNetPager控件中文显示分页信息
在日常的编程过程中,很多学员对于使AspNetPager控件中文显示分页信息不是很清楚,本文将由达内的老师为各位学员介绍一下使AspNetPager控件中文显示分页信息的内容. AspNetPager ...
- Qt创建窗体的过程
版权声明 本文为原创作品,请尊重作者的劳动成果.转载必须保持文章完整性,并以超链接形式注明原始作者“ tingsking18”和 主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口 ...
- jquery 提示插件 cluetip
jquery的 插件cluetip, 地址下载是:plugins.learningjquery.com/cluetip/demo/ 下面简单讲解下用法: 1 首先当然要放JQUERY的基本JS,和这个 ...
- JMS的作用
JMS就是生产者与消费者模式.消费者负责消费生产者产生的消息.通过JMS可以做后台的异步操作,应用到具体工作中的话,有用它来发内部消息的.发邮件的.发短信的,做大操作时在后台做异步操作的. Java ...
- WCF - net.pipe vs. net.tcp vs. http Bindings
WCF - net.pipe vs. net.tcp vs. http Bindings 问题: I'm new to WCF and would like to know the differenc ...