好久没写过博客啦,最近在gao搞Qt,做出漂亮的UI确实挺难的,做美工也不简单啊,好啦,言归正传,下面是实现DES的python源码,是借鉴了开源中国一个大师的源码写出来的,直接贴啦。

加密部分:

  1. #!/usr/bin/python
  2. #coding:utf-8
  3.  
  4. from DESstruction import *
  5. import re
  6.  
  7. __all__ = ['desencode']
  8.  
  9. class DES():
  10. def __init__(self):
  11. pass
  12. #加密
  13. def code(self, former_code, key, code_len, key_len):
  14. output = ""
  15. trun_len = 0
  16.  
  17. #将密文和密钥转换为二进制
  18. code_string = self.hexTobin(former_code, code_len)
  19. code_key = self.hexTobin(key, key_len)
  20.  
  21. #如果密钥长度不是16的整数倍则以增加0的方式变为16的整数倍
  22. if code_len % 16 != 0:
  23. real_len = (code_len / 16) * 16 + 16
  24. else:
  25. real_len = code_len
  26.  
  27. if key_len % 16 != 0:
  28. key_len = (key_len / 16) * 16 + 16
  29. key_len *= 4
  30.  
  31. #每个16进制占4位
  32. trun_len = 4 * real_len
  33. #对每64位进行一次加密
  34. for i in range(0, trun_len, 64):
  35. run_code = code_string[i : i+64]
  36. l = i % key_len
  37. run_key = code_key[l : l+64]
  38.  
  39. #64位明文、密钥初始置换
  40. run_code = self.code_Firstchange(run_code)
  41. run_key = self.key_Firstchange(run_key)
  42.  
  43. #16次迭代
  44. for j in range(16):
  45.  
  46. #取出明文左右32位
  47. code_r = run_code[32 : 64]
  48. code_l = run_code[0 : 32]
  49.  
  50. #64左右交换
  51. run_code = code_r
  52.  
  53. #右边32位扩展置换
  54. code_r = self.code_Expand(code_r)
  55.  
  56. #获取本轮子密钥
  57. key_l = run_key[0:28]
  58. key_r = run_key[28 : 56]
  59. key_l = key_l[d[j] : 28] + key_l[0 : d[j]]
  60. key_r = key_r[d[j] : 28] + key_r[0 : d[j]]
  61. run_key = key_l + key_r
  62. key_y = self.key_Secondchange(run_key)
  63.  
  64. #异或
  65. code_r = self.code_xor(code_r,key_y)
  66.  
  67. #S盒代替/选择
  68. code_r = self.key_Sbox(code_r)
  69.  
  70. #P转换
  71. code_r = self.key_Pbox(code_r)
  72.  
  73. #异或
  74. code_r = self.code_xor(code_l, code_r)
  75. run_code += code_r
  76. #32互换
  77. code_r = run_code[32 : 64]
  78. code_l = run_code[0 : 32]
  79. run_code = code_r + code_l
  80.  
  81. #将二进制转换为16进制、逆初始置换
  82. output += self.code_Inversepermutation(run_code)
  83. return output
  84.  
  85. #将十六进制转换为二进制字符串
  86. def hexTobin(self, code, lens):
  87. new_code = ''
  88. lens = lens % 16
  89. for key in code:
  90. code_ord = int(key,16)
  91. new_code += self.Tobin(code_ord, 4)
  92. if lens != 0:
  93. new_code += '' * (16 - lens) * 4
  94. return new_code
  95.  
  96. #二进制转换
  97. def Tobin(self, o, lens):
  98. new_code = ''
  99. for i in range(lens):
  100. new_code = str(o >> i & 1) + new_code
  101. return new_code
  102.  
  103. #密文或明文初s始置换
  104. def code_Firstchange(self, code):
  105. changed_code = ''
  106. for i in range(64):
  107. changed_code += code[ip[i] - 1]
  108. return changed_code
  109.  
  110. #密钥初始置换
  111. def key_Firstchange (self, key):
  112. changed_key = ''
  113. for i in range(56):
  114. changed_key += key[pc1[i] - 1]
  115. return changed_key
  116.  
  117. #扩展置换
  118. def code_Expand(self, code):
  119. new_list = ''
  120. for i in range(48):
  121. new_list += code[e[i] - 1]
  122. return new_list
  123.  
  124. #密钥置换选择2
  125. def key_Secondchange(self, key):
  126. new_list = ''
  127. for i in range(48):
  128. new_list += key[pc2[i] - 1]
  129. return new_list
  130.  
  131. #异或
  132. def code_xor(self, code, key):
  133. code_len = len(key)
  134. new_list = ''
  135. for i in range(code_len):
  136. if code[i] == key[i]:
  137. new_list += ''
  138. else:
  139. new_list += ''
  140. return new_list
  141.  
  142. #S盒代替选择置换
  143. def key_Sbox(self, key):
  144. new_list = ''
  145. for i in range(8):
  146. row = int(str(key[i * 6]) + str(key[i * 6 + 5]), 2)
  147. raw = int(str(key[i * 6 + 1]) + str(key[i * 6 + 2]) + str(key[i * 6 + 3]) + str(key[i * 6 + 4]), 2)
  148. new_list += self.Tobin(s[i][row][raw], 4)
  149.  
  150. return new_list
  151.  
  152. #置换P
  153. def key_Pbox(self, code):
  154. new_list = ''
  155. for i in range(32):
  156. new_list += code[p[i] - 1]
  157. return new_list
  158.  
  159. #逆初始置换
  160. def code_Inversepermutation(self, code):
  161. lens = len(code) / 4
  162. new_list = ''
  163. for i in range(lens):
  164. list = ''
  165. for j in range(4):
  166. list += code[ip_1[i * 4 + j] - 1]
  167. new_list += "%x" % int(list, 2)
  168. return new_list
  169.  
  170. def DES_encode(former_code,key):
  171. #转换为16进制
  172. former_code = Tohex(former_code)
  173. key = Tohex(key)
  174.  
  175. des = DES()
  176. key_len = len(key)
  177. code_len = len(former_code)
  178.  
  179. if code_len < 1 or key_len < 1:
  180. print 'error input'
  181. return False
  182.  
  183. key_code = des.code(former_code, key, code_len, key_len)
  184. return key_code
  185.  
  186. #将unicode字符转换为16进制
  187. def Tohex(string):
  188. new_string = ''
  189.  
  190. for i in string:
  191. new_string += "%02x" % ord(i)
  192.  
  193. return new_string
  194.  
  195. def tounicode(string):
  196. return_string = ''
  197. string_len = len(string)
  198. for i in range(0, string_len, 2):
  199. return_string += chr(int(string[i : i + 2], 16))
  200. return return_string
  201.  
  202. if __name__ == '__main__':
  203. print DES_encode('12083612isawonder', '0f1571c947刘')

解密方法:

  1. #!/usr/bin/python
  2. #coding:utf-8
  3.  
  4. from DESstruction import *
  5. import re
  6.  
  7. __all__ = ['desdecode']
  8. class DES():
  9.  
  10. def __init__(self):
  11. pass
  12. #解密
  13. def decode(self, string, key, key_len, string_len):
  14. output = ""
  15. trun_len = 0
  16. num = 0
  17.  
  18. #将密文转换为二进制
  19. code_string = self.hexTobin(string, string_len)
  20. #获取字密钥
  21. code_key = self.getKey(key, key_len)
  22.  
  23. #如果密钥长度不是16的整数倍则以增加0的方式变为16的整数倍
  24. real_len = (key_len / 16) + 1 if key_len % 16 != 0 else key_len / 16
  25. trun_len = string_len * 4
  26. #对每64位进行一次加密
  27. for i in range(0, trun_len, 64):
  28. run_code = code_string[i : i + 64]
  29. run_key = code_key[num % real_len]
  30.  
  31. #64位明文初始置换
  32. run_code = self.code_Firstchange(run_code)
  33.  
  34. #16次迭代
  35. for j in range(16):
  36.  
  37. code_r = run_code[32 : 64]
  38. code_l = run_code[0 : 32]
  39.  
  40. #64左右交换
  41. run_code = code_r
  42.  
  43. #右边32位扩展置换
  44. code_r = self.code_Expand(code_r)
  45.  
  46. #获取本轮子密钥
  47. key_y = run_key[15 - j]
  48.  
  49. #异或
  50. code_r = self.code_xor(code_r, key_y)
  51.  
  52. #S盒代替/选择
  53. code_r = self.code_Sbox(code_r)
  54.  
  55. #P转换
  56. code_r = self.code_Pbox(code_r)
  57.  
  58. #异或
  59. code_r = self.code_xor(code_l,code_r)
  60.  
  61. run_code += code_r
  62. num += 1
  63.  
  64. #32互换
  65. code_r = run_code[32 : 64]
  66. code_l = run_code[0 : 32]
  67. run_code = code_r + code_l
  68.  
  69. #将二进制转换为16进制、逆初始置换
  70. output += self.code_Inversepermutation(run_code)
  71. return output
  72.  
  73. #将十六进制转换为二进制字符串
  74. def hexTobin(self, code, lens):
  75. return_code = ''
  76. lens = lens % 16
  77. for key in code:
  78. code_ord = int(key,16)
  79. return_code += self.Tobin(code_ord, 4)
  80.  
  81. if lens != 0:
  82. return_code += '' * (16 - lens) * 4
  83. return return_code
  84.  
  85. #获取子密钥
  86. def getKey(self, key, key_len):
  87.  
  88. #将密钥转换为二进制
  89. code_key = self.hexTobin(key, key_len)
  90.  
  91. a = [''] * 16
  92. real_len = (key_len / 16) * 16 + 16 if key_len % 16 != 0 else key_len
  93.  
  94. b = [''] * (real_len / 16)
  95. for i in range(real_len / 16):
  96. b[i] = a[ : ]
  97. num = 0
  98. trun_len = 4 * key_len
  99. for i in range(0, trun_len, 64):
  100. run_key = code_key[i : i + 64]
  101. run_key = self.key_Firstchange(run_key)
  102. for j in range(16):
  103. key_l = run_key[0 : 28]
  104. key_r = run_key[28 : 56]
  105. key_l = key_l[d[j] : 28] + key_l[0 : d[j]]
  106. key_r = key_r[d[j] : 28] + key_r[0 : d[j]]
  107. run_key = key_l + key_r
  108. key_y = self.key_Secondchange(run_key)
  109. b[num][j] = key_y[ : ]
  110. num += 1
  111.  
  112. return b
  113.  
  114. #密文或明文初始置换
  115. def code_Firstchange(self, code):
  116. changed_code = ''
  117. for i in range(64):
  118. changed_code += code[ip[i] - 1]
  119. return changed_code
  120.  
  121. #扩展置换
  122. def code_Expand(self, code):
  123. return_list = ''
  124. for i in range(48):
  125. return_list += code[e[i] - 1]
  126. return return_list
  127.  
  128. #异或
  129. def code_xor(self, code, key):
  130. code_len = len(key)
  131. return_list = ''
  132. for i in range(code_len):
  133. if code[i] == key[i]:
  134. return_list += ''
  135. else:
  136. return_list += ''
  137. return return_list
  138.  
  139. #二进制转换
  140. def Tobin(self, o, lens):
  141. return_code = ''
  142. for i in range(lens):
  143. return_code = str(o >> i & 1) + return_code
  144. return return_code
  145.  
  146. #S盒代替选择置换
  147. def code_Sbox(self, key):
  148. return_list=''
  149. for i in range(8):
  150. row = int(str(key[i * 6]) + str(key[i * 6 + 5]), 2)
  151. raw = int(str(key[i*6+1]) + str(key[i * 6 + 2]) + str(key[i * 6 + 3]) + str(key[i * 6 + 4]), 2)
  152. return_list += self.Tobin(s[i][row][raw], 4)
  153. return return_list
  154.  
  155. #置换P
  156. def code_Pbox(self, code):
  157. return_list = ''
  158. for i in range(32):
  159. return_list += code[p[i] - 1]
  160. return return_list
  161.  
  162. #密钥初始置换
  163. def key_Firstchange(self, key):
  164. changed_key = ''
  165. for i in range(56):
  166. changed_key += key[pc1[i] - 1]
  167. return changed_key
  168.  
  169. #逆初始置换
  170. def code_Inversepermutation(self, code):
  171. return_list = ''
  172. for i in range(16):
  173. list = ''
  174. for j in range(4):
  175. list += code[ip_1[i * 4 + j] - 1]
  176. return_list += "%x" % int(list, 2)
  177. return return_list
  178.  
  179. #密钥置换选择2
  180. def key_Secondchange(self, key):
  181. return_list = ''
  182. for i in range(48):
  183. return_list += key[pc2[i] - 1]
  184. return return_list
  185.  
  186. #入口函数
  187. def DES_decode(from_code, key):
  188. key = tohex(key)
  189.  
  190. des = DES()
  191.  
  192. key_len = len(key)
  193. string_len = len(from_code)
  194. if string_len % 16 != 0:
  195. return False
  196. if string_len < 1 or key_len < 1:
  197. return False
  198.  
  199. key_code = des.decode(from_code, key, key_len, string_len)
  200. return tounicode(key_code)
  201.  
  202. #将unicode字符转换为16进制
  203. def tohex(string):
  204. return_string = ''
  205. for i in string:
  206. return_string += "%02x" % ord(i)
  207. return return_string
  208.  
  209. def tounicode(string):
  210. return_string = ''
  211. string_len = len(string)
  212. for i in range(0, string_len, 2):
  213. return_string += chr(int(string[i : i + 2], 16))
  214. return return_string
  215.  
  216. #测试
  217. if __name__ == '__main__':
  218. print DES_decode('3530b23e0d9d0f5c2cb2602ac98fd26382ed6769d3dcf00a', '0f1571c947刘')

DES算法的更多相关文章

  1. DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 1.DES算法简介 DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准. DES是一个分组 ...

  2. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  3. DES 算法的 C++ 与 JAVA 互相加解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. MFC 简单实现 DES 算法

    前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...

  5. 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密

    在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...

  6. Asp.Net 常用工具类之加密——对称加密DES算法(2)

    又到周末,下午博客园看了两篇文章,关于老跳和老赵的程序员生涯,不禁感叹漫漫程序路,何去何从兮! 转眼毕业的第三个年头,去过苏州,跑过上海,从一开始的凌云壮志,去年背起行囊默默回到了长沙准备买房,也想有 ...

  7. AES算法,DES算法,RSA算法JAVA实现

    1     AES算法 1.1    算法描述 1.1.1      设计思想 Rijndael密码的设计力求满足以下3条标准: ① 抵抗所有已知的攻击. ② 在多个平台上速度快,编码紧凑. ③ 设计 ...

  8. 安全体系(一)—— DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(二)——RSA算 ...

  9. DES算法原理完整版

    1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...

  10. C#与Java同步加密解密DES算法

    在实际项目中,往往前端和后端使用不同的语言.比如使用C#开发客户端,使用Java开发服务器端.有时出于安全性考虑需要将字符加密传输后,由服务器解密获取.本文介绍一种采用DES算法的C#与Java同步加 ...

随机推荐

  1. apk逆向 - smali动态调试

    author: Dlive date: 2016/10/6 0x00 前言 ​ 之前有人问过smali的动态调试方法,其实网上已经有很多文章讲这些内容,但是为了方便大家学习,我还是写一下让大家少走点坑 ...

  2. mongoDB index introduction

    索引为mongoDB的查询提供了有效的解决方案,如果没有索引,mongodb必须的扫描文档集中所有记录来match查询条件的记录.然而这些扫描是没有必要,而且每一次操作mongod进程会处理大量的数据 ...

  3. 【原创】node+express+socket搭建一个实时推送应用

    技术背景 Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新. 应用场景: 监控系统:后台硬件热插拔.LED.温度.电压发生变化 即 ...

  4. iOS企业分发证书制作

    自签名证书制作流程 打开终端,输入 openssl genrsa - ,生成名称为ca的秘钥 注:openssl生成的文件皆放在用户文档下(finder菜单栏'前往' - 电脑 -Macintosh ...

  5. 如何搞定IE+google双内核的360浏览器表单自动回填兼容问题

    最近开发中碰到一个关于表单问题,在用户提交表单时候浏览器会提示是否保存帐号 如果点击保存,在退出帐号切换其他帐号时,浏览器会自动为表单填充数据,为了解决这个自动填充问题时, 主要分2个思路来解决,一个 ...

  6. C/C++调试工具gdb

    关于Gdb的使用,请参考:http://blog.csdn.net/haoel/article/details/2879

  7. 修改MySql 数据默认存储路径

    1. cmd进入控制台 net stop mysql 2.复制原来数据库目录到新目录 复制C:\ProgramData\MySQL\MySQL Server 5.5\中的data目录到 D:\Prog ...

  8. sqlserver Between And的问题

    Id Name RegisterDate 1 澎澎 2007/1/5 00:00:00 2 丁丁 2007/1/6 04:37:00 3 亞亞 2007/1/7 00:00:00 数据库的数据如上.若 ...

  9. OC推箱子

    #include<stdio.h> #include<stdlib.h> int main(void) { char sr;//存储用户输入的指令 //绘制地图 char a[ ...

  10. Android中自定义控件TextSize属性问题

    本文主要说明一个自定义控件添加TextSize属性的坑,刚刚从坑里面爬出来,写个随笔,记录一下: *************************************************** ...