# -*- 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,0x8,0xa,0x1,0x3,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0,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()