首页 > 编程 > Python > 正文

Python基于DES算法加密解密实例

2020-01-04 18:07:44
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Python基于DES算法加密解密实现方法,以实例形式分析了DES算法实现加密解密的相关技巧,需要的朋友可以参考下

本文实例讲述了Python基于DES算法加密解密实现方法。分享给大家供大家参考。具体实现方法如下:

  1. #coding=utf-8  
  2. from functools import partial  
  3. import base64  
  4. class DES(object):  
  5. """  
  6. DES加密算法  
  7. interface: input_key(s, base=10), encode(s), decode(s)  
  8. """ 
  9. __ip = [  
  10. 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,  
  11. 62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,  
  12. 57,49,41,33,25,179,1,59,51,43,35,27,19,11,3,  
  13. 61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,  
  14. ]  
  15. __ip1 = [  
  16. 40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,  
  17. 38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,  
  18. 36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,  
  19. 34,2,42,10,50,18,58,26,33,1,419,49,17,57,25,  
  20. ]  
  21. __e = [  
  22. 3212345,  
  23. 4 , 56789,  
  24. 8 , 9,10,11,12,13,  
  25. 12,13,14,15,16,17,  
  26. 16,17,18,19,20,21,  
  27. 20,21,22,23,24,25,  
  28. 24,25,26,27,28,29,  
  29. 28,29,30,31,321,  
  30. ]  
  31. __p = [  
  32. 167,20,21,29,12,28,17,  
  33. 1 ,15,23,265,18,31,10,  
  34. 2 ,8 ,24,14,32,2739,  
  35. 19,13,306,22,114,25,  
  36. ]  
  37. __s = [  
  38. [  
  39. 0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7
  40. 0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8
  41. 0x4,0x1,0xe,0x8,0xd,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x1,0xe,0x7,0x4,0xa,0x8,0xd,0xf,0xc,0x9,0x0,0x3,0x5,0x6,0xb
  85. ],  
  86. ]  
  87. __k1 = [  
  88. 57,49,41,33,25,179,  
  89. 1 ,58,50,42,34,26,18,  
  90. 102,59,51,43,35,27,  
  91. 19,113,60,52,44,36,  
  92. 63,55,47,39,31,23,15,  
  93. 7 ,62,54,46,38,30,22,  
  94. 146,61,53,45,37,29,  
  95. 21,135,28,20,124,  
  96. ]  
  97. __k2 = [  
  98. 14,17,11,24153,28,  
  99. 156,21,10,23,19,124,  
  100. 268,167,27,20,132,  
  101. 41,52,31,37,47,55,30,40,  
  102. 51,45,33,48,44,49,39,56,  
  103. 34,53,46,42,50,36,29,32,  
  104. ]  
  105. __k0 = [  
  106. 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,  
  107. ]  
  108. __hex_bin = {  
  109. '0':'0000','1':'0001','2':'0010','3':'0011',  
  110. '4':'0100','5':'0101','6':'0110','7':'0111',  
  111. '8':'1000','9':'1001','a':'1010','b':'1011',  
  112. 'c':'1100','d':'1101','e':'1110','f':'1111',  
  113. ' ':'0000' 
  114. }  
  115. __re = lambda t, s: ''.join(s[i-1for i in t)  
  116. __IP = partial(__re, __ip)  
  117. __IP1 = partial(__re, __ip1)  
  118. __E = partial(__re, __e)  
  119. __P = partial(__re, __p)  
  120. __K1 = partial(__re, __k1)  
  121. __K2 = partial(__re, __k2)  
  122. __B = partial( lambda hex_bin, s: ''.join(hex_bin[w]  
  123. for w in ''.join('%2x' % ord(w) for w in s))  
  124. , __hex_bin)  
  125. __DB = partial( lambda s: ''.join(chr(int(s[i:i+8], 2)) for i in range(0, len(s), 8)))  
  126. __S = partial( lambda hex_bin, __s, s: ''.join(hex_bin['%x' % __s[i][  
  127. int(s[i*6]+s[i*6+5], 2)*16 + int(s[i*6+1:i*6+5], 2)]] for i in range(8))  
  128. , __hex_bin, __s)  
  129. __F = partial( lambda s, k: ''.join('0' if s[i]==k[i] else '1' for i in range(len(s))))  
  130. __K0 = partial( lambda k0, K2, k: map(K2,  
  131. (k[k0[i]:28]+k[0:k0[i]] + k[k0[i]+28:56]+k[28:k0[i]+28for i in range(16)))  
  132. , __k0, __K2)  
  133. __K = partial( lambda K1, K0, k: K0(K1(k))  
  134. , __K1, __K0)  
  135. def __init__(self):  
  136. pass 
  137. def input_key(self, key, base=10):  
  138. if base == 2:  
  139. pass 
  140. elif base == 16:  
  141. key = ''.join(self.__class__.__hex_bin[w] for w in key)  
  142. else:  
  143. key = self.__class__.__B(key)  
  144. self.__k = self.__class__.__K(key)  
  145. def __code(self, s, k):  
  146. s = self.__IP(s)  
  147. l, r = s[0:32], s[32:64]  
  148. for i in range(16):  
  149. r_t = r  
  150. r = self.__E(r)  
  151. r = self.__F(r, k[i])  
  152. r = self.__S(r)  
  153. r = self.__P(r)  
  154. r = self.__F(r, l)  
  155. l = r_t  
  156. return self.__class__.__IP1(r+l)  
  157. def encode(self, s):  
  158. a = ''  
  159. s += ' ' * ((8-len(s)%8)%8)  
  160. for i in range(0, len(s), 8):  
  161. before = self.__class__.__B(s[i:i+8])  
  162. after = self.__code(before, self.__k)  
  163. a += '%16x' % int(after, 2)  
  164. return ''.join(w if w!=' ' else '0' for w in a)  
  165. def decode(self, s):  
  166. a = ''  
  167. s.lower()  
  168. for i in range(0, len(s), 16):  
  169. before = ''.join(self.__class__.__hex_bin[s[j]] for j in range(i, i+16))  
  170. after = self.__code(before, self.__k[::-1])  
  171. a += self.__class__.__DB(after)  
  172. return a.rstrip().decode('utf-8')  
  173. if __name__ == '__main__':  
  174. d = DES()  
  175. d.input_key('123456789')  
  176. s = '再来一段中文' 
  177. a = d.encode(s)  
  178. print a  
  179. b = d.decode(a)  
  180. print b 


希望本文所述对大家的Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表