码迷,mamicode.com
首页 > 其他好文 > 详细

DES加密,Tk写的简单的GUI

时间:2016-08-22 10:39:10      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

  1 # -*- coding: UTF-8 -*-
  2 from Tkinter import *
  3 from tkMessageBox import *
  4  
  5 ip = [  
  6         58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,  
  7         62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,  
  8         57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,  
  9         61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,  
 10      ]  
 11 ip_1 = [  
 12         40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,  
 13         38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,  
 14         36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,  
 15         34,2,42,10,50,18,58,26,33,1,41, 9,49,17,57,25,  
 16        ]
 17 e = [  
 18         32, 1, 2, 3, 4, 5,  
 19         4 , 5, 6, 7, 8, 9,  
 20         8 , 9,10,11,12,13,  
 21         12,13,14,15,16,17,  
 22         16,17,18,19,20,21,  
 23         20,21,22,23,24,25,  
 24         24,25,26,27,28,29,  
 25         28,29,30,31,32, 1,  
 26     ]
 27 p = [  
 28         16, 7,20,21,29,12,28,17,  
 29         1 ,15,23,26, 5,18,31,10,  
 30         2 ,8 ,24,14,32,27, 3, 9,  
 31         19,13,30, 6,22,11, 4,25,  
 32     ]  
 33 sbox = [  
 34         [  
 35         [0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7], 
 36         [0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8],  
 37         [0x4,0x1,0xe,0x8,0xd,0x6,0x2,0xb,0xf,0xc,0x9,0x7,0x3,0xa,0x5,0x0],  
 38         [0xf,0xc,0x8,0x2,0x4,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x6,0xd]  
 39         ],  
 40         [  
 41         [0xf,0x1,0x8,0xe,0x6,0xb,0x3,0x4,0x9,0x7,0x2,0xd,0xc,0x0,0x5,0xa],  
 42         [0x3,0xd,0x4,0x7,0xf,0x2,0x8,0xe,0xc,0x0,0x1,0xa,0x6,0x9,0xb,0x5],  
 43         [0x0,0xe,0x7,0xb,0xa,0x4,0xd,0x1,0x5,0x8,0xc,0x6,0x9,0x3,0x2,0xf],  
 44         [0xd,0x8,0xa,0x1,0x3,0xf,0x4,0x2,0xb,0x6,0x7,0xc,0x0,0x5,0xe,0x9]  
 45         ],  
 46         [  
 47         [0xa,0x0,0x9,0xe,0x6,0x3,0xf,0x5,0x1,0xd,0xc,0x7,0xb,0x4,0x2,0x8],  
 48         [0xd,0x7,0x0,0x9,0x3,0x4,0x6,0xa,0x2,0x8,0x5,0xe,0xc,0xb,0xf,0x1],  
 49         [0xd,0x6,0x4,0x9,0x8,0xf,0x3,0x0,0xb,0x1,0x2,0xc,0x5,0xa,0xe,0x7],  
 50         [0x1,0xa,0xd,0x0,0x6,0x9,0x8,0x7,0x4,0xf,0xe,0x3,0xb,0x5,0x2,0xc]  
 51         ],  
 52         [  
 53         [0x7,0xd,0xe,0x3,0x0,0x6,0x9,0xa,0x1,0x2,0x8,0x5,0xb,0xc,0x4,0xf],  
 54         [0xd,0x8,0xb,0x5,0x6,0xf,0x0,0x3,0x4,0x7,0x2,0xc,0x1,0xa,0xe,0x9],  
 55         [0xa,0x6,0x9,0x0,0xc,0xb,0x7,0xd,0xf,0x1,0x3,0xe,0x5,0x2,0x8,0x4],  
 56         [0x3,0xf,0x0,0x6,0xa,0x1,0xd,0x8,0x9,0x4,0x5,0xb,0xc,0x7,0x2,0xe]  
 57         ],  
 58         [  
 59         [0x2,0xc,0x4,0x1,0x7,0xa,0xb,0x6,0x8,0x5,0x3,0xf,0xd,0x0,0xe,0x9],  
 60         [0xe,0xb,0x2,0xc,0x4,0x7,0xd,0x1,0x5,0x0,0xf,0xa,0x3,0x9,0x8,0x6],  
 61         [0x4,0x2,0x1,0xb,0xa,0xd,0x7,0x8,0xf,0x9,0xc,0x5,0x6,0x3,0x0,0xe],  
 62         [0xb,0x8,0xc,0x7,0x1,0xe,0x2,0xd,0x6,0xf,0x0,0x9,0xa,0x4,0x5,0x3]  
 63         ],  
 64         [  
 65         [0xc,0x1,0xa,0xf,0x9,0x2,0x6,0x8,0x0,0xd,0x3,0x4,0xe,0x7,0x5,0xb],  
 66         [0xa,0xf,0x4,0x2,0x7,0xc,0x9,0x5,0x6,0x1,0xd,0xe,0x0,0xb,0x3,0x8],  
 67         [0x9,0xe,0xf,0x5,0x2,0x8,0xc,0x3,0x7,0x0,0x4,0xa,0x1,0xd,0xb,0x6],  
 68         [0x4,0x3,0x2,0xc,0x9,0x5,0xf,0xa,0xb,0xe,0x1,0x7,0x6,0x0,0x8,0xd]  
 69         ],  
 70         [  
 71         [0x4,0xb,0x2,0xe,0xf,0x0,0x8,0xd,0x3,0xc,0x9,0x7,0x5,0xa,0x6,0x1],  
 72         [0xd,0x0,0xb,0x7,0x4,0x9,0x1,0xa,0xe,0x3,0x5,0xc,0x2,0xf,0x8,0x6],  
 73         [0x1,0x4,0xb,0xd,0xc,0x3,0x7,0xe,0xa,0xf,0x6,0x8,0x0,0x5,0x9,0x2],  
 74         [0x6,0xb,0xd,0x8,0x1,0x4,0xa,0x7,0x9,0x5,0x0,0xf,0xe,0x2,0x3,0xc]  
 75         ],  
 76         [  
 77         [0xd,0x2,0x8,0x4,0x6,0xf,0xb,0x1,0xa,0x9,0x3,0xe,0x5,0x0,0xc,0x7],  
 78         [0x1,0xf,0xd,0x8,0xa,0x3,0x7,0x4,0xc,0x5,0x6,0xb,0x0,0xe,0x9,0x2],  
 79         [0x7,0xb,0x4,0x1,0x9,0xc,0xe,0x2,0x0,0x6,0xa,0xd,0xf,0x3,0x5,0x8],  
 80         [0x2,0x1,0xe,0x7,0x4,0xa,0x8,0xd,0xf,0xc,0x9,0x0,0x3,0x5,0x6,0xb]  
 81         ] 
 82     ]
 83 hex_bin = {  
 84         0x0:0000,0x1:0001,0x2:0010,0x3:0011,  
 85         0x4:0100,0x5:0101,0x6:0110,0x7:0111,  
 86         0x8:1000,0x9:1001,0xa:1010,0xb:1011,  
 87         0xc:1100,0xd:1101,0xe:1110,0xf:1111 
 88           }
 89 bin_hex = {
 90     0000:0,0001:1,0010:2,0011:3,
 91     0100:4,0101:5,0110:6,0111:7,
 92     1000:8,1001:9,1010:A,1011:B,
 93     1100:C,1101:D,1110:E,1111:F
 94           }
 95 hex_s =   {
 96     0:0000,1:0001,2:0010,3:0011,  
 97         4:0100,5:0101,6:0110,7:0111,  
 98         8:1000,9:1001,A:1010,B:1011,  
 99         C:1100,D:1101,E:1110,F:1111
100       }
101 #生成子密钥
102 def Rkey(cipherkey):
103     pc_1 = [  
104         57,49,41,33,25,17, 9,  
105         1 ,58,50,42,34,26,18,  
106         10, 2,59,51,43,35,27,  
107         19,11, 3,60,52,44,36,  
108         63,55,47,39,31,23,15,  
109         7 ,62,54,46,38,30,22,  
110         14, 6,61,53,45,37,29,  
111         21,13, 5,28,20,12, 4 
112        ]  
113     pc_2 = [
114         14,17,11,24, 1, 5, 3,28,  
115         15, 6,21,10,23,19,12, 4,  
116         26, 8,16, 7,27,20,13, 2,  
117         41,52,31,37,47,55,30,40,  
118         51,45,33,48,44,49,39,56,  
119         34,53,46,42,50,36,29,32 
120        ]  
121     pc = [
122         1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
123      ] 
124  
125     k0 = []
126     s = cipherkey
127     for i in range(8):
128         k0.append(s[i])
129         for i in range(len(k0)):
130             k0[i] = bin(int(hex(ord(k0[i])),16))[2:]
131         k = ‘‘
132     for i in range(len(k0)):
133         if len(k0[i]) != 8:
134             k0[i] = (8-len(k0[i]))*0 + k0[i]
135             k = k+k0[i]
136         k1=‘‘
137         for i in range(56):
138             k1 = k1 +k[pc_1[i]-1]
139     c0 = k1[0:28]
140     d0 = k1[28:]
141     c = []
142     d = []
143     for i in range(16):
144         if i == 0:
145             c.append(c0[pc[i]:]+c0[i])
146             d.append(d0[pc[i]:]+d0[i])
147         else:
148             c.append(c[i-1][pc[i]:]+c[i-1][0:pc[i]])
149             d.append(d[i-1][pc[i]:]+d[i-1][0:pc[i]])
150     K_0 = [] #进行pc_2置换并存到列表K_1中
151     K_1 = []
152     for i in range(16):
153         K_0.append(c[i]+d[i])
154     for i in range(16):
155         s = ‘‘
156         for j in range(48):
157             s = s + K_0[i][pc_2[j]-1]
158         K_1.append(s)
159     return K_1
160 #F函数
161 def des_F(R,Ki):
162     R1 = ‘‘
163     for i in range(48):
164         R1 = R1 + R[e[i]-1]
165     #print R1
166     R2 = ‘‘
167     for i in range(48):
168         if R1[i] == Ki[i]:
169             s = 0
170         else:
171             s = 1
172         R2 = R2 + s
173     R3 = []
174     S = [0,6,12,18,24,30,36,42]
175     for i in S:
176         R3.append(R2[i:(i+6)])
177     S1 = ‘‘
178     for i in range(8):
179         row = int((R3[i][0]+R3[i][5]),2)  #hang
180         col = int((R3[i][1:5]),2)         #lie
181         l = sbox[i][row][col]
182                  
183         S1 = S1 + hex_bin[l]
184     S2 = ‘‘
185     for i in range(32):
186         S2 = S2 +S1[p[i]-1]
187     return S2
188  
189 #加密 
190  
191 def des_Encode(cleartext,cipherkey):
192     K = Rkey(cipherkey)
193     s = str(cleartext)
194     s0 = []
195     for i in range(len(s)):
196         s0.append(bin(int(hex(ord(s[i])),16))[2:])
197     if len(s0) != 8:
198         for i in range((8 - len(s0))):
199             s0.append(00000000)
200     s1 = ‘‘
201     for i in range(8):
202         if len(s0[i]) != 8:
203             s0[i] = (8-len(s0[i]))*0 + s0[i]
204         s1 = s1 + s0[i]
205         #初始置换
206     s2 = ‘‘
207     for i in range(64):
208         s2 = s2 + s1[ip[i]-1]
209          
210     L0 = s2[0:32]
211     R0 = s2[32:]
212     L = []
213     R = []
214     for i in range(16):
215         if i == 0:
216             L.append(L0)
217             R.append(R0)
218         else:
219             Li = R[i-1]
220             ans = des_F(R[i-1],K[i-1])
221             Ri = ‘‘
222             for j in range(32):
223                 if L[i-1][j] == ans[j]:
224                     ss = 0
225                 else:
226                     ss = 1
227                 Ri = Ri + ss
228             L.append(Li)
229             R.append(Ri)
230     ans1 = des_F(R[15],K[15])
231     L_15 = R[14]
232     L_16 = ‘‘
233     #异或
234     for i in range(32):
235         if L_15[i] == ans1[i]:
236             tt = 0
237         else:
238             tt = 1
239         L_16 = L_16 + tt
240     R_16 = R[15]
241     C = ‘‘
242     xx =L_16+R_16
243     #逆初始置换
244     for i in range(64):
245         C = C +(xx)[ip_1[i]-1]
246     #转换为16进制
247     binhex = [0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]
248     binbox = []
249     for i in binhex:
250         binbox.append(C[i:(i+4)])
251     C_hex = ‘‘
252     for i in range(16):
253         C_hex = C_hex + str(bin_hex[binbox[i]])
254      
255     return C_hex
256 #解密
257 def des_Decode(ciphertext,cipherkey):
258     text = ciphertext
259     K_l = Rkey(cipherkey)
260     K_list =[]
261     for i in range(16):
262         K_list.append(K_l[15-i])
263  
264     text1 = ‘‘
265     for i in text:
266         text1 = text1 + hex_s[i]
267     text2 = ‘‘
268     for i in range(64):
269         text2 = text2 + text1[ip[i]-1]
270     L0 = text2[0:32]
271     R0 = text2[32:]
272     L = []
273     R = []
274     for i in range(16):
275         if i == 0:
276             L.append(L0)
277             R.append(R0)
278         else:
279             L_next = R[i-1]
280             R_1 = des_F(R[i-1],K_list[i-1]) 
281             R_next = ‘‘
282             for j in range(32):
283                 if L[i-1][j] == R_1[j]:
284                     s = 0
285                 else:
286                     s = 1
287                 R_next = R_next +s
288             L.append(L_next)
289             R.append(R_next)
290     L16 = ‘‘
291     ans2 = des_F(R[15],K_list[15])
292     for i in range(32):
293         if L[15][i] == ans2[i]:
294             ss = 0
295         else:
296             ss = 1
297         L16 = L16 + ss
298     R16 = R[15]
299     L_R = L16 + R16
300     m_bin = ‘‘
301     for i in range (64):
302         m_bin = m_bin + L_R[ip_1[i]-1]
303     bin_list = [0,8,16,24,32,40,48,56]
304     asc_list = []
305     for i in bin_list:
306         asc_list.append(int(m_bin[i:(i+8)],2))
307     m_string = ‘‘
308     for i in asc_list:
309         m_string = m_string + chr(int(i))
310     return m_string
311  
312 class window():
313     def __init__(self,top):
314         top.title(Des  v1.0)
315         top.geometry(560x380+500+200)
316         #top.iconbitmap(‘G:/1.ico‘)
317         top.resizable(0,0)
318         label1 = Label(top,text = (明      文:).decode(utf-8))
319         label1.place(x=0,y=4)
320         self.t = StringVar()
321         self.entry1 = Entry(top,width = 80,textvariable = self.t)
322         self.entry1.place(x=70,y=5)
323         label2 = Label(top,text = (密      钥:).decode(utf-8))
324         label2.place(x=0,y=32)
325         self.entry2 = Entry(top,width = 80)
326         self.entry2.place(x=70,y=33)
327         self.button1 = Button(top,text = (加 密).decode(utf-8),width = 10,height = 1,command = self.Encode_button)
328         self.button1.place(x=70,y=63)
329         self.button2 = Button(top,text = (解 密).decode(utf-8),width = 10,height = 1,command = self.Decode_button)
330         self.button2.place(x=160,y=63)
331         self.button3 = Button(top,text = (显示子密钥).decode(utf-8),width = 10,height = 1,command = self.Show_key)
332         self.button3.place(x=250,y=63)
333         self.button4 = Button(top,text = (清空密钥).decode(utf-8),width = 10,height = 1,command = self.Clear_text)
334         self.button4.place(x=340,y=63)
335         label3 = Label(top,text = (密      文:).decode(utf-8))
336         label3.place(x=0,y=93)
337         self.e = StringVar()
338         self.entry3 = Entry(top,width = 80,textvariable=self.e)
339         self.entry3.place(x=70,y=93)
340         label4 = Label(top,text = (子 密 钥:).decode(utf-8))
341         label4.place(x=0,y=123)
342         self.text2=Text(top,selectbackground = gray,selectforeground = red,width = 60)
343         self.text2.place(x=70,y=123)
344     def Encode_button(self):
345         try:
346             clear_text = self.entry1.get()
347             cipher_key = self.entry2.get()
348             text_list = []
349             if (len(clear_text)/8)*8 != len(clear_text): 
350                 for i in range((len(clear_text)/8)):
351                     text_list.append(clear_text[i*8:(i+1)*8])
352                 text_list.append(clear_text[(len(clear_text)/8)*8:])
353             else:
354                 for i in range((len(clear_text)/8)):
355                     text_list.append(clear_text[i*8:(i+1)*8])
356             ans =‘‘
357             for i in text_list:
358                 ans = ans + des_Encode(i,cipher_key)
359             self.e.set(ans)
360  
361         except:
362             showwarning(Warning,Error!)
363     def Show_key(self):
364         try:
365             cipher_key = self.entry2.get()
366             key = Rkey(cipher_key)
367             for i in range(16):
368                 self.text2.insert(END,K+str(i+1)+: +key[i]+\n)
369  
370         except:
371             showwarning(Warning,Error!)
372     def Clear_text(self):
373         self.text2.delete(1.0,END)
374     def Decode_button(self):
375         try:
376             cipher_text = self.entry3.get()
377             cipher_key = self.entry2.get()
378             mingwen = des_Decode(cipher_text,cipher_key)
379             self.t.set(mingwen)
380         except:
381             showwarning(Warning,Error!)
382  
383 top = Tk()
384 window = window(top)
385 top.mainloop()

 

DES加密,Tk写的简单的GUI

标签:

原文地址:http://www.cnblogs.com/chenjingyi/p/5794615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!