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


Posted in Python onJune 03, 2015

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

#coding=utf-8  
from functools import partial  
import base64 
class DES(object):  
  """ 
  DES加密算法 
  interface: input_key(s, base=10), encode(s), decode(s) 
  """  
  __ip = [  
    58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,  
    62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,  
    57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,  
    61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,  
  ]  
  __ip1 = [  
    40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,  
    38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,  
    36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,  
    34,2,42,10,50,18,58,26,33,1,41, 9,49,17,57,25,  
  ]  
  __e = [  
    32, 1, 2, 3, 4, 5,  
    4 , 5, 6, 7, 8, 9,  
    8 , 9,10,11,12,13,  
    12,13,14,15,16,17,  
    16,17,18,19,20,21,  
    20,21,22,23,24,25,  
    24,25,26,27,28,29,  
    28,29,30,31,32, 1,  
  ]  
  __p = [  
    16, 7,20,21,29,12,28,17,  
    1 ,15,23,26, 5,18,31,10,  
    2 ,8 ,24,14,32,27, 3, 9,  
    19,13,30, 6,22,11, 4,25,  
  ]  
  __s = [  
    [  
    0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7,
    0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8,
    0x4,0x1,0xe,0x8,0xd,0x6,0x2,0xb,0xf,0xc,0x9,0x7,0x3,0xa,0x5,0x0,
    0xf,0xc,0x8,0x2,0x4,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x6,0xd,
    ],  
    [  
    0xf,0x1,0x8,0xe,0x6,0xb,0x3,0x4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f,0x0,0x9,0xa,0x4,0x5,0x3,
    ],  
    [  
    0xc,0x1,0xa,0xf,0x9,0x2,0x6,0x8,0x0,0xd,0x3,0x4,0xe,0x7,0x5,0xb,
    0xa,0xf,0x4,0x2,0x7,0xc,0x9,0x5,0x6,0x1,0xd,0xe,0x0,0xb,0x3,0x8,
    0x9,0xe,0xf,0x5,0x2,0x8,0xc,0x3,0x7,0x0,0x4,0xa,0x1,0xd,0xb,0x6,
    0x4,0x3,0x2,0xc,0x9,0x5,0xf,0xa,0xb,0xe,0x1,0x7,0x6,0x0,0x8,0xd,
    ],  
    [  
    0x4,0xb,0x2,0xe,0xf,0x0,0x8,0xd,0x3,0xc,0x9,0x7,0x5,0xa,0x6,0x1,
    0xd,0x0,0xb,0x7,0x4,0x9,0x1,0xa,0xe,0x3,0x5,0xc,0x2,0xf,0x8,0x6,
    0x1,0x4,0xb,0xd,0xc,0x3,0x7,0xe,0xa,0xf,0x6,0x8,0x0,0x5,0x9,0x2,
    0x6,0xb,0xd,0x8,0x1,0x4,0xa,0x7,0x9,0x5,0x0,0xf,0xe,0x2,0x3,0xc,
    ],  
    [  
    0xd,0x2,0x8,0x4,0x6,0xf,0xb,0x1,0xa,0x9,0x3,0xe,0x5,0x0,0xc,0x7,
    0x1,0xf,0xd,0x8,0xa,0x3,0x7,0x4,0xc,0x5,0x6,0xb,0x0,0xe,0x9,0x2,
    0x7,0xb,0x4,0x1,0x9,0xc,0xe,0x2,0x0,0x6,0xa,0xd,0xf,0x3,0x5,0x8,
    0x2,0x1,0xe,0x7,0x4,0xa,0x8,0xd,0xf,0xc,0x9,0x0,0x3,0x5,0x6,0xb,
    ],  
  ]  
  __k1 = [  
    57,49,41,33,25,17, 9,  
    1 ,58,50,42,34,26,18,  
    10, 2,59,51,43,35,27,  
    19,11, 3,60,52,44,36,  
    63,55,47,39,31,23,15,  
    7 ,62,54,46,38,30,22,  
    14, 6,61,53,45,37,29,  
    21,13, 5,28,20,12, 4,  
  ]  
  __k2 = [  
    14,17,11,24, 1, 5, 3,28,  
    15, 6,21,10,23,19,12, 4,  
    26, 8,16, 7,27,20,13, 2,  
    41,52,31,37,47,55,30,40,  
    51,45,33,48,44,49,39,56,  
    34,53,46,42,50,36,29,32,  
  ]  
  __k0 = [  
    1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,  
  ]  
  __hex_bin = {  
    '0':'0000','1':'0001','2':'0010','3':'0011',  
    '4':'0100','5':'0101','6':'0110','7':'0111',  
    '8':'1000','9':'1001','a':'1010','b':'1011',  
    'c':'1100','d':'1101','e':'1110','f':'1111',  
    ' ':'0000'  
  }  
  __re = lambda t, s: ''.join(s[i-1] for i in t)  
  __IP = partial(__re, __ip)  
  __IP1 = partial(__re, __ip1)  
  __E = partial(__re, __e)  
  __P = partial(__re, __p)  
  __K1 = partial(__re, __k1)  
  __K2 = partial(__re, __k2)  
  __B = partial( lambda hex_bin, s: ''.join(hex_bin[w]  
    for w in ''.join('%2x' % ord(w) for w in s))  
    , __hex_bin)  
  __DB = partial( lambda s: ''.join(chr(int(s[i:i+8], 2)) for i in range(0, len(s), 8)))  
  __S = partial( lambda hex_bin, __s, s: ''.join(hex_bin['%x' % __s[i][  
    int(s[i*6]+s[i*6+5], 2)*16 + int(s[i*6+1:i*6+5], 2)]] for i in range(8))  
    , __hex_bin, __s)  
  __F = partial( lambda s, k: ''.join('0' if s[i]==k[i] else '1' for i in range(len(s))))  
  __K0 = partial( lambda k0, K2, k: map(K2,  
    (k[k0[i]:28]+k[0:k0[i]] + k[k0[i]+28:56]+k[28:k0[i]+28] for i in range(16)))  
    , __k0, __K2)  
  __K = partial( lambda K1, K0, k: K0(K1(k))  
    , __K1, __K0)  
  def __init__(self):  
    pass  
  def input_key(self, key, base=10):  
    if base == 2:  
      pass  
    elif base == 16:  
      key = ''.join(self.__class__.__hex_bin[w] for w in key)  
    else:  
      key = self.__class__.__B(key)  
    self.__k = self.__class__.__K(key)  
  def __code(self, s, k):  
    s = self.__IP(s)  
    l, r = s[0:32], s[32:64]  
    for i in range(16):  
      r_t = r  
      r = self.__E(r)  
      r = self.__F(r, k[i])  
      r = self.__S(r)  
      r = self.__P(r)  
      r = self.__F(r, l)  
      l = r_t  
    return self.__class__.__IP1(r+l)  
  def encode(self, s):  
    a = ''  
    s += ' ' * ((8-len(s)%8)%8)  
    for i in range(0, len(s), 8):  
      before = self.__class__.__B(s[i:i+8])  
      after = self.__code(before, self.__k)  
      a += '%16x' % int(after, 2)  
    return ''.join(w if w!=' ' else '0' for w in a)  
  def decode(self, s):  
    a = ''  
    s.lower()  
    for i in range(0, len(s), 16):  
      before = ''.join(self.__class__.__hex_bin[s[j]] for j in range(i, i+16))  
      after = self.__code(before, self.__k[::-1])  
      a += self.__class__.__DB(after)  
    return a.rstrip().decode('utf-8')  
if __name__ == '__main__':  
  d = DES()  
  d.input_key('123456789')  
  s = '再来一段中文'  
  a = d.encode(s)  
  print a 
  b = d.decode(a) 
  print b

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

Python 相关文章推荐
Python2.x中str与unicode相关问题的解决方法
Mar 30 Python
Python and、or以及and-or语法总结
Apr 14 Python
python实现按任意键继续执行程序
Dec 30 Python
python3 中文乱码与默认编码格式设定方法
Oct 31 Python
Python3.0中普通方法、类方法和静态方法的比较
May 03 Python
python实现简单图书管理系统
Nov 22 Python
python中count函数简单的实例讲解
Feb 06 Python
Tensorflow限制CPU个数实例
Feb 06 Python
Python MySQL 日期时间格式化作为参数的操作
Mar 02 Python
python为QT程序添加图标的方法详解
Mar 09 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
Mar 13 Python
pytorch 实现多个Dataloader同时训练
May 29 Python
Python使用minidom读写xml的方法
Jun 03 #Python
Python实现程序的单一实例用法分析
Jun 03 #Python
python简单获取本机计算机名和IP地址的方法
Jun 03 #Python
Python自动调用IE打开某个网站的方法
Jun 03 #Python
Python找出文件中使用率最高的汉字实例详解
Jun 03 #Python
Python实现Windows上气泡提醒效果的方法
Jun 03 #Python
Python捕捉和模拟鼠标事件的方法
Jun 03 #Python
You might like
提升PHP执行速度全攻略
2006/10/09 PHP
用PHP来写记数器(详细介绍)
2006/10/09 PHP
PHP 字符串编码截取函数(兼容utf-8和gb2312)
2009/05/02 PHP
php 日期时间处理函数小结
2009/12/18 PHP
PHP实现上一篇下一篇的方法实例总结
2016/09/22 PHP
PHP基于array_unique实现二维数组去重
2020/07/14 PHP
IE php关于强制下载文件的代码
2008/08/23 Javascript
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
浅谈javascript的数据类型检测
2010/07/10 Javascript
JQury slideToggle闪烁问题及解决办法
2011/07/05 Javascript
JS+flash实现chrome和ie浏览器下同时可以复制粘贴
2013/09/22 Javascript
关于jQuery中的each方法(jQuery到底干了什么)
2014/03/05 Javascript
完美兼容各大浏览器的jQuery插件实现图片切换特效
2014/12/12 Javascript
JS+CSS实现TreeMenu二级树形菜单完整实例
2015/09/18 Javascript
AugularJS从入门到实践(必看篇)
2017/07/10 Javascript
vue 文件目录结构详解
2017/11/24 Javascript
你点的 ES6一些小技巧,请查收
2018/04/25 Javascript
node上的redis调用优化示例详解
2018/10/30 Javascript
解决前后端分离 vue+springboot 跨域 session+cookie失效问题
2019/05/13 Javascript
使用微信SDK自定义分享的方法
2019/07/03 Javascript
[13:40]TI3青蛙君全程回顾 DOTA2我们为梦想再战
2013/09/13 DOTA
Python判断操作系统类型代码分享
2014/11/22 Python
基于spring boot 日志(logback)报错的解决方式
2020/02/20 Python
Python PyQt5整理介绍
2020/04/01 Python
浅析python 通⽤爬⾍和聚焦爬⾍
2020/09/28 Python
Django model class Meta原理解析
2020/11/14 Python
基于CSS3实现的黑色个性导航菜单效果
2015/09/14 HTML / CSS
米兰网婚纱礼服法国网上商店:Milanoo法国
2016/08/20 全球购物
教师考核评语
2014/04/28 职场文书
好习惯伴我成长演讲稿
2014/05/21 职场文书
投标服务承诺书
2014/05/28 职场文书
公务员个人年终总结
2015/02/12 职场文书
公司人事任命通知
2015/04/20 职场文书
幼儿教师三分钟演讲稿
2019/06/21 职场文书
Python基础之hashlib模块详解
2021/05/06 Python
vue使用watch监听属性变化
2022/04/30 Vue.js