DES算法
好久没写过博客啦,最近在gao搞Qt,做出漂亮的UI确实挺难的,做美工也不简单啊,好啦,言归正传,下面是实现DES的python源码,是借鉴了开源中国一个大师的源码写出来的,直接贴啦。
加密部分:
- #!/usr/bin/python
- #coding:utf-8
- from DESstruction import *
- import re
- __all__ = ['desencode']
- class DES():
- def __init__(self):
- pass
- #加密
- def code(self, former_code, key, code_len, key_len):
- output = ""
- trun_len = 0
- #将密文和密钥转换为二进制
- code_string = self.hexTobin(former_code, code_len)
- code_key = self.hexTobin(key, key_len)
- #如果密钥长度不是16的整数倍则以增加0的方式变为16的整数倍
- if code_len % 16 != 0:
- real_len = (code_len / 16) * 16 + 16
- else:
- real_len = code_len
- if key_len % 16 != 0:
- key_len = (key_len / 16) * 16 + 16
- key_len *= 4
- #每个16进制占4位
- trun_len = 4 * real_len
- #对每64位进行一次加密
- for i in range(0, trun_len, 64):
- run_code = code_string[i : i+64]
- l = i % key_len
- run_key = code_key[l : l+64]
- #64位明文、密钥初始置换
- run_code = self.code_Firstchange(run_code)
- run_key = self.key_Firstchange(run_key)
- #16次迭代
- for j in range(16):
- #取出明文左右32位
- code_r = run_code[32 : 64]
- code_l = run_code[0 : 32]
- #64左右交换
- run_code = code_r
- #右边32位扩展置换
- code_r = self.code_Expand(code_r)
- #获取本轮子密钥
- key_l = run_key[0:28]
- key_r = run_key[28 : 56]
- key_l = key_l[d[j] : 28] + key_l[0 : d[j]]
- key_r = key_r[d[j] : 28] + key_r[0 : d[j]]
- run_key = key_l + key_r
- key_y = self.key_Secondchange(run_key)
- #异或
- code_r = self.code_xor(code_r,key_y)
- #S盒代替/选择
- code_r = self.key_Sbox(code_r)
- #P转换
- code_r = self.key_Pbox(code_r)
- #异或
- code_r = self.code_xor(code_l, code_r)
- run_code += code_r
- #32互换
- code_r = run_code[32 : 64]
- code_l = run_code[0 : 32]
- run_code = code_r + code_l
- #将二进制转换为16进制、逆初始置换
- output += self.code_Inversepermutation(run_code)
- return output
- #将十六进制转换为二进制字符串
- def hexTobin(self, code, lens):
- new_code = ''
- lens = lens % 16
- for key in code:
- code_ord = int(key,16)
- new_code += self.Tobin(code_ord, 4)
- if lens != 0:
- new_code += '' * (16 - lens) * 4
- return new_code
- #二进制转换
- def Tobin(self, o, lens):
- new_code = ''
- for i in range(lens):
- new_code = str(o >> i & 1) + new_code
- return new_code
- #密文或明文初s始置换
- def code_Firstchange(self, code):
- changed_code = ''
- for i in range(64):
- changed_code += code[ip[i] - 1]
- return changed_code
- #密钥初始置换
- def key_Firstchange (self, key):
- changed_key = ''
- for i in range(56):
- changed_key += key[pc1[i] - 1]
- return changed_key
- #扩展置换
- def code_Expand(self, code):
- new_list = ''
- for i in range(48):
- new_list += code[e[i] - 1]
- return new_list
- #密钥置换选择2
- def key_Secondchange(self, key):
- new_list = ''
- for i in range(48):
- new_list += key[pc2[i] - 1]
- return new_list
- #异或
- def code_xor(self, code, key):
- code_len = len(key)
- new_list = ''
- for i in range(code_len):
- if code[i] == key[i]:
- new_list += ''
- else:
- new_list += ''
- return new_list
- #S盒代替选择置换
- def key_Sbox(self, key):
- new_list = ''
- for i in range(8):
- row = int(str(key[i * 6]) + str(key[i * 6 + 5]), 2)
- raw = int(str(key[i * 6 + 1]) + str(key[i * 6 + 2]) + str(key[i * 6 + 3]) + str(key[i * 6 + 4]), 2)
- new_list += self.Tobin(s[i][row][raw], 4)
- return new_list
- #置换P
- def key_Pbox(self, code):
- new_list = ''
- for i in range(32):
- new_list += code[p[i] - 1]
- return new_list
- #逆初始置换
- def code_Inversepermutation(self, code):
- lens = len(code) / 4
- new_list = ''
- for i in range(lens):
- list = ''
- for j in range(4):
- list += code[ip_1[i * 4 + j] - 1]
- new_list += "%x" % int(list, 2)
- return new_list
- def DES_encode(former_code,key):
- #转换为16进制
- former_code = Tohex(former_code)
- key = Tohex(key)
- des = DES()
- key_len = len(key)
- code_len = len(former_code)
- if code_len < 1 or key_len < 1:
- print 'error input'
- return False
- key_code = des.code(former_code, key, code_len, key_len)
- return key_code
- #将unicode字符转换为16进制
- def Tohex(string):
- new_string = ''
- for i in string:
- new_string += "%02x" % ord(i)
- return new_string
- def tounicode(string):
- return_string = ''
- string_len = len(string)
- for i in range(0, string_len, 2):
- return_string += chr(int(string[i : i + 2], 16))
- return return_string
- if __name__ == '__main__':
- print DES_encode('12083612isawonder', '0f1571c947刘')
解密方法:
- #!/usr/bin/python
- #coding:utf-8
- from DESstruction import *
- import re
- __all__ = ['desdecode']
- class DES():
- def __init__(self):
- pass
- #解密
- def decode(self, string, key, key_len, string_len):
- output = ""
- trun_len = 0
- num = 0
- #将密文转换为二进制
- code_string = self.hexTobin(string, string_len)
- #获取字密钥
- code_key = self.getKey(key, key_len)
- #如果密钥长度不是16的整数倍则以增加0的方式变为16的整数倍
- real_len = (key_len / 16) + 1 if key_len % 16 != 0 else key_len / 16
- trun_len = string_len * 4
- #对每64位进行一次加密
- for i in range(0, trun_len, 64):
- run_code = code_string[i : i + 64]
- run_key = code_key[num % real_len]
- #64位明文初始置换
- run_code = self.code_Firstchange(run_code)
- #16次迭代
- for j in range(16):
- code_r = run_code[32 : 64]
- code_l = run_code[0 : 32]
- #64左右交换
- run_code = code_r
- #右边32位扩展置换
- code_r = self.code_Expand(code_r)
- #获取本轮子密钥
- key_y = run_key[15 - j]
- #异或
- code_r = self.code_xor(code_r, key_y)
- #S盒代替/选择
- code_r = self.code_Sbox(code_r)
- #P转换
- code_r = self.code_Pbox(code_r)
- #异或
- code_r = self.code_xor(code_l,code_r)
- run_code += code_r
- num += 1
- #32互换
- code_r = run_code[32 : 64]
- code_l = run_code[0 : 32]
- run_code = code_r + code_l
- #将二进制转换为16进制、逆初始置换
- output += self.code_Inversepermutation(run_code)
- return output
- #将十六进制转换为二进制字符串
- def hexTobin(self, code, lens):
- return_code = ''
- lens = lens % 16
- for key in code:
- code_ord = int(key,16)
- return_code += self.Tobin(code_ord, 4)
- if lens != 0:
- return_code += '' * (16 - lens) * 4
- return return_code
- #获取子密钥
- def getKey(self, key, key_len):
- #将密钥转换为二进制
- code_key = self.hexTobin(key, key_len)
- a = [''] * 16
- real_len = (key_len / 16) * 16 + 16 if key_len % 16 != 0 else key_len
- b = [''] * (real_len / 16)
- for i in range(real_len / 16):
- b[i] = a[ : ]
- num = 0
- trun_len = 4 * key_len
- for i in range(0, trun_len, 64):
- run_key = code_key[i : i + 64]
- run_key = self.key_Firstchange(run_key)
- for j in range(16):
- key_l = run_key[0 : 28]
- key_r = run_key[28 : 56]
- key_l = key_l[d[j] : 28] + key_l[0 : d[j]]
- key_r = key_r[d[j] : 28] + key_r[0 : d[j]]
- run_key = key_l + key_r
- key_y = self.key_Secondchange(run_key)
- b[num][j] = key_y[ : ]
- num += 1
- return b
- #密文或明文初始置换
- def code_Firstchange(self, code):
- changed_code = ''
- for i in range(64):
- changed_code += code[ip[i] - 1]
- return changed_code
- #扩展置换
- def code_Expand(self, code):
- return_list = ''
- for i in range(48):
- return_list += code[e[i] - 1]
- return return_list
- #异或
- def code_xor(self, code, key):
- code_len = len(key)
- return_list = ''
- for i in range(code_len):
- if code[i] == key[i]:
- return_list += ''
- else:
- return_list += ''
- return return_list
- #二进制转换
- def Tobin(self, o, lens):
- return_code = ''
- for i in range(lens):
- return_code = str(o >> i & 1) + return_code
- return return_code
- #S盒代替选择置换
- def code_Sbox(self, key):
- return_list=''
- for i in range(8):
- row = int(str(key[i * 6]) + str(key[i * 6 + 5]), 2)
- raw = int(str(key[i*6+1]) + str(key[i * 6 + 2]) + str(key[i * 6 + 3]) + str(key[i * 6 + 4]), 2)
- return_list += self.Tobin(s[i][row][raw], 4)
- return return_list
- #置换P
- def code_Pbox(self, code):
- return_list = ''
- for i in range(32):
- return_list += code[p[i] - 1]
- return return_list
- #密钥初始置换
- def key_Firstchange(self, key):
- changed_key = ''
- for i in range(56):
- changed_key += key[pc1[i] - 1]
- return changed_key
- #逆初始置换
- def code_Inversepermutation(self, code):
- return_list = ''
- for i in range(16):
- list = ''
- for j in range(4):
- list += code[ip_1[i * 4 + j] - 1]
- return_list += "%x" % int(list, 2)
- return return_list
- #密钥置换选择2
- def key_Secondchange(self, key):
- return_list = ''
- for i in range(48):
- return_list += key[pc2[i] - 1]
- return return_list
- #入口函数
- def DES_decode(from_code, key):
- key = tohex(key)
- des = DES()
- key_len = len(key)
- string_len = len(from_code)
- if string_len % 16 != 0:
- return False
- if string_len < 1 or key_len < 1:
- return False
- key_code = des.decode(from_code, key, key_len, string_len)
- return tounicode(key_code)
- #将unicode字符转换为16进制
- def tohex(string):
- return_string = ''
- for i in string:
- return_string += "%02x" % ord(i)
- return return_string
- def tounicode(string):
- return_string = ''
- string_len = len(string)
- for i in range(0, string_len, 2):
- return_string += chr(int(string[i : i + 2], 16))
- return return_string
- #测试
- if __name__ == '__main__':
- print DES_decode('3530b23e0d9d0f5c2cb2602ac98fd26382ed6769d3dcf00a', '0f1571c947刘')
DES算法的更多相关文章
- DES算法详解
本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 1.DES算法简介 DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准. DES是一个分组 ...
- 基于DES算法加密的防撞库密码系统项目总结
项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...
- DES 算法的 C++ 与 JAVA 互相加解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- MFC 简单实现 DES 算法
前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...
- 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密
在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...
- Asp.Net 常用工具类之加密——对称加密DES算法(2)
又到周末,下午博客园看了两篇文章,关于老跳和老赵的程序员生涯,不禁感叹漫漫程序路,何去何从兮! 转眼毕业的第三个年头,去过苏州,跑过上海,从一开始的凌云壮志,去年背起行囊默默回到了长沙准备买房,也想有 ...
- AES算法,DES算法,RSA算法JAVA实现
1 AES算法 1.1 算法描述 1.1.1 设计思想 Rijndael密码的设计力求满足以下3条标准: ① 抵抗所有已知的攻击. ② 在多个平台上速度快,编码紧凑. ③ 设计 ...
- 安全体系(一)—— DES算法详解
本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(二)——RSA算 ...
- DES算法原理完整版
1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...
- C#与Java同步加密解密DES算法
在实际项目中,往往前端和后端使用不同的语言.比如使用C#开发客户端,使用Java开发服务器端.有时出于安全性考虑需要将字符加密传输后,由服务器解密获取.本文介绍一种采用DES算法的C#与Java同步加 ...
随机推荐
- apk逆向 - smali动态调试
author: Dlive date: 2016/10/6 0x00 前言 之前有人问过smali的动态调试方法,其实网上已经有很多文章讲这些内容,但是为了方便大家学习,我还是写一下让大家少走点坑 ...
- mongoDB index introduction
索引为mongoDB的查询提供了有效的解决方案,如果没有索引,mongodb必须的扫描文档集中所有记录来match查询条件的记录.然而这些扫描是没有必要,而且每一次操作mongod进程会处理大量的数据 ...
- 【原创】node+express+socket搭建一个实时推送应用
技术背景 Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新. 应用场景: 监控系统:后台硬件热插拔.LED.温度.电压发生变化 即 ...
- iOS企业分发证书制作
自签名证书制作流程 打开终端,输入 openssl genrsa - ,生成名称为ca的秘钥 注:openssl生成的文件皆放在用户文档下(finder菜单栏'前往' - 电脑 -Macintosh ...
- 如何搞定IE+google双内核的360浏览器表单自动回填兼容问题
最近开发中碰到一个关于表单问题,在用户提交表单时候浏览器会提示是否保存帐号 如果点击保存,在退出帐号切换其他帐号时,浏览器会自动为表单填充数据,为了解决这个自动填充问题时, 主要分2个思路来解决,一个 ...
- C/C++调试工具gdb
关于Gdb的使用,请参考:http://blog.csdn.net/haoel/article/details/2879
- 修改MySql 数据默认存储路径
1. cmd进入控制台 net stop mysql 2.复制原来数据库目录到新目录 复制C:\ProgramData\MySQL\MySQL Server 5.5\中的data目录到 D:\Prog ...
- 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 数据库的数据如上.若 ...
- OC推箱子
#include<stdio.h> #include<stdlib.h> int main(void) { char sr;//存储用户输入的指令 //绘制地图 char a[ ...
- Android中自定义控件TextSize属性问题
本文主要说明一个自定义控件添加TextSize属性的坑,刚刚从坑里面爬出来,写个随笔,记录一下: *************************************************** ...