热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

这篇文章主要介绍了Python基于DES算法加密解密实现方法,以实例形式分析了DES算法实现加密解密的相关技巧,需要的朋友可以参考下
本文实例讲述了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,0x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0xd,0x7,0x8,0xf,0x9,0xc,0x5,0x6,0x3,0x0,0xe,
    0xb,0x8,0xc,0x7,0x1,0xe,0x2,0xd,0x6,0xf,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代码示例,帮助读者更好地理解这些概念。 ... [详细]
  • Coursera ML 机器学习
    2019独角兽企业重金招聘Python工程师标准线性回归算法计算过程CostFunction梯度下降算法多变量回归![选择特征](https:static.oschina.n ... [详细]
  • Java 实现二维极点算法
    本文介绍了一种使用 Java 编程语言实现的二维极点算法。该算法用于从一组二维坐标中筛选出极点,适用于需要处理几何图形和空间数据的应用场景。文章不仅详细解释了算法的工作原理,还提供了完整的代码示例。 ... [详细]
  • 智能车间调度研究进展
    本文综述了基于强化学习的智能车间调度策略,探讨了车间调度问题在资源有限条件下的优化方法。通过数学规划、智能算法和强化学习等手段,解决了作业车间、流水车间和加工车间中的静态与动态调度挑战。重点讨论了不同场景下的求解方法及其应用前景。 ... [详细]
  • 本文详细介绍了福昕软件公司开发的Foxit PDF SDK ActiveX控件(版本5.20),并提供了关于其在64位Windows 7系统和Visual Studio 2013环境下的使用方法。该控件文件名为FoxitPDFSDKActiveX520_Std_x64.ocx,适用于集成PDF功能到应用程序中。 ... [详细]
  • 本文详细介绍了如何将 Python 3.6.3 程序转换为 Windows 可执行文件(.exe),并解决了使用 py2exe 和 cx_Freeze 时遇到的问题。推荐使用 PyInstaller 进行打包,提供完整的安装和打包步骤。 ... [详细]
  • ZooKeeper集群脑裂问题及其解决方案
    本文深入探讨了ZooKeeper集群中可能出现的脑裂问题,分析其成因,并提供了多种有效的解决方案,确保集群在高可用性环境下的稳定运行。 ... [详细]
  • 本次挑战涉及数组截断操作,初看似乎简单,但实际上考察了对数组切片方法的理解与应用。本文将详细解析该算法的实现逻辑,并提供多个示例以加深理解。 ... [详细]
  • 本文深入探讨了Memcached的内存管理机制,特别是其采用的Slab Allocator技术。该技术通过预分配不同大小的内存块来有效解决内存碎片问题,并确保高效的数据存储与检索。文中详细描述了Slab Allocator的工作原理、内存分配流程以及相关的优化策略。 ... [详细]
  • 华为智慧屏:超越屏幕尺寸的智能进化
    继全球发布后,华为智慧屏于9月26日在上海正式亮相,推出65英寸和75英寸版本。该产品不仅在屏幕尺寸上有所突破,更在性能和智能化方面实现了显著提升。 ... [详细]
  • 本文介绍如何利用栈数据结构在C++中判断字符串中的括号是否匹配。通过顺序栈和链栈两种方式实现,并详细解释了算法的核心思想和具体实现步骤。 ... [详细]
  • Redux入门指南
    本文介绍Redux的基本概念和工作原理,帮助初学者理解如何使用Redux管理应用程序的状态。Redux是一个用于JavaScript应用的状态管理库,特别适用于React项目。 ... [详细]
  • 历经三十年的开发,Mathematica 已成为技术计算领域的标杆,为全球的技术创新者、教育工作者、学生及其他用户提供了一个领先的计算平台。最新版本 Mathematica 12.3.1 增加了多项核心语言、数学计算、可视化和图形处理的新功能。 ... [详细]
  • 机器学习核心概念与技术
    本文系统梳理了机器学习的关键知识点,涵盖模型评估、正则化、线性模型、支持向量机、决策树及集成学习等内容,并深入探讨了各算法的原理和应用场景。 ... [详细]
  • 如何使用Ping命令来测试网络连接?当网卡安装和有关参数配置完成后,可以使用ping命令来测试一下网络是否连接成功。以winXP为例1、打开XP下DOS窗口具体操作是点击“开始”菜 ... [详细]
author-avatar
今天是星期天嘛_512
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有