好久没写过博客啦,最近在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算法的更多相关文章

  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. css的relative和position探究

    在CSS中,Position 属性经常会用到,主要是绝对定位和相对定位,简单的使用都没有问题,尤其嵌套起来,就会有些混乱,今记录总结一下,防止久而忘之. CSS position 属性值: absol ...

  2. 安装yum

    RedHat 安装配置 YUM 删除 1 . 查询系统是否安装 yum : rpm – qa|grep yum 2删除原有 yum rpm -qa|grep yum|xargs rpm -e – no ...

  3. 基于SSM的租赁管理系统0.2_20161225_开发环境

    项目环境搭建 1. 开发环境 Sybase PowerDesigner 15.1.0 + MySQL 5.7.15 + Navicat 11.0.9 + eclipse EE Mars 2.0 + F ...

  4. Java学习笔记__异常机制_try_catch_finally_return执行顺序

    package cn.xiaocangtian.Exception; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  5. iOS 企业证书的使用文档

    IN-HOUSE应用程序分发 下面介绍下使用网络方式进行部署的方式,用户直接在iPhone/iPad的Safari浏览器里面输入URL地址即可安装, 注意:目前对于这种企业级开发的应用程序最好的分发方 ...

  6. [leetcode]leetcode初体验

    这几天把之前的设计模式回顾了一遍,整理了一点以前的项目.同学说,打算刷leetcode题目,也勾起了我的兴趣,索性也刷一些题目,再提高一些内功.刚开始进去,leetcode随机分配的题目,直接也就做了 ...

  7. 用ILSpy查看Session.SessionID的生成算法

    缘由 asp.net Session在InProc模式下,容易丢失,经常需要重新登录,且不支持分布式共享. 所以在研究Redis实现原生的Session,本来想用GUID作为key存入cookie,又 ...

  8. RHEL6.5安装11.2.0.3 RAC并打补丁

    [TOC] 一,主机配置 1.修改hosts文件(两节点) #127.0.0.1 localhost localhost.localdomain localhost4 localhost4.local ...

  9. [Spring] AOP, Aspect实例解析

    最近要用到切面来统一处理日志记录,写了个小实例练了练手: 具体实现类: public interface PersonServer { public void save(String name); p ...

  10. JavaScript异步编程的主要解决方案—对不起,我和你不在同一个频率上

    众所周知(这也忒夸张了吧?),Javascript通过事件驱动机制,在单线程模型下,以异步的形式来实现非阻塞的IO操作.这种模式使得JavaScript在处理事务时非常高效,但这带来了很多问题,比如异 ...