Python基于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,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x6,0xd,
    ],
    [
    0xf,0x1,0x8,0xe,0x6,0xb,0x3,0x4,0x9,0x7,0x2,0xd,0xc,0x0,0x5,0xa,
    0x3,0xd,0x4,0x7,0xf,0x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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程序设计有所帮助。

(0)

相关推荐

  • Python基于回溯法子集树模板实现8皇后问题

    本文实例讲述了Python基于回溯法子集树模板实现8皇后问题.分享给大家供大家参考,具体如下: 问题 8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 分析 为了简化问题,考虑到8个皇后不同行,则每一行放置一个皇后,每一行的皇后可以放置于第0.1.2.....7列,我们认为每一行的皇后有8种状态.那么,我们只要套用子集树模板,从第0行开始,自上而下,对每一行的皇后,遍历它的8个状态即可. 代码: ''' 8皇后问题 '''

  • python回溯法实现数组全排列输出实例分析

    本文实例讲述了python回溯法实现数组全排列输出的方法.分享给大家供大家参考.具体分析如下: 全排列解释:从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列. from sys import stdout #code from http://www.jb51.net/ def perm(li, start, end): if(start == end): for elem in li: stdout.wr

  • python k-近邻算法实例分享

    简单说明 这个算法主要工作是测量不同特征值之间的距离,有个这个距离,就可以进行分类了. 简称kNN. 已知:训练集,以及每个训练集的标签. 接下来:和训练集中的数据对比,计算最相似的k个距离.选择相似数据中最多的那个分类.作为新数据的分类. python实例 复制代码 代码如下: # -*- coding: cp936 -*- #win系统中应用cp936编码,linux中最好还是utf-8比较好.from numpy import *#引入科学计算包import operator #经典pyt

  • python 示例分享---逻辑推理编程解决八皇后

    可以和Haskell , Prolog 一样做到模式匹配, 建立逻辑推到规则,描述问题,得出答案. from pyDatalog import pyDatalog pyDatalog.create_atoms( 'N, N1, X, Y, X0, X1, X2, X3, X4, X5, X6, X7' ) pyDatalog.create_atoms( 'ok, queens, next_queen, pred, pred2' ) size = 8 ok( X1, N, X2 ) <= ( X1

  • Python 连连看连接算法

    功能:为连连看游戏提供连接算法 说明:模块中包含一个Point类,该类是游戏的基本单元"点",该类包含属性:x,y,value. 其中x,y代表了该点的坐标,value代表该点的特征:0代表没有被填充,1-8代表被填充为游戏图案,9代表被填充为墙壁 模块中还包含一个名为points的Point列表,其中保存着整个游戏界面中的每个点 使用模块的时候应首先调用createPoints方法,初始化游戏界面中每个点,然后可通过points访问到每个点,继而初始化界面 模块中核心的方法是link

  • python编写的最短路径算法

    一心想学习算法,很少去真正静下心来去研究,前几天趁着周末去了解了最短路径的资料,用python写了一个最短路径算法.算法是基于带权无向图去寻找两个点之间的最短路径,数据存储用邻接矩阵记录.首先画出一幅无向图如下,标出各个节点之间的权值. 其中对应索引: A --> 0 B--> 1 C--> 2 D-->3 E--> 4 F--> 5 G--> 6 邻接矩阵表示无向图: 算法思想是通过Dijkstra算法结合自身想法实现的.大致思路是:从起始点开始,搜索周围的路径

  • Python多线程经典问题之乘客做公交车算法实例

    本文实例讲述了Python多线程经典问题之乘客做公交车算法.分享给大家供大家参考,具体如下: 问题描述: 乘客乘坐公交车问题,司机,乘客,售票员协同工作,通过多线程模拟三者的工作. 司机:开车,停车 售票员:打开车门,关闭车门 乘客:上车,下车 用Python的Event做线程同步通信,代码如下: # *-* coding:gb2312 *-* import threading import time stationName=("车站0","车站1","车

  • python基于右递归解决八皇后问题的方法

    本文实例讲述了python基于右递归解决八皇后问题的方法.分享给大家供大家参考.具体分析如下: 凡是线性回溯都可以归结为右递归的形式,也即是二叉树,因此对于只要求一个解的问题,采用右递归实现的程序要比回溯法要优美的多. def Test(queen,n): '''这个就不用说了吧,就是检验第n(下标,0-7)行皇后的位置是否合理''' q=queen[n] for i in xrange(n): if queen[i]==q or queen[i]-q==n-i or queen[i]-q==i

  • python实现RSA加密(解密)算法

    RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准. 今天只有短的RSA钥匙才可能被强力方式解破.到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式.只要其密钥的长度足够长,用RSA加密的信息实际上是不能被解破的.但在分布式计算和量子计算机理论日趋成熟的今天,RSA加密安全性受到了挑战. RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥.

  • Python基于递归算法实现的走迷宫问题

    本文实例讲述了Python基于递归算法实现的走迷宫问题.分享给大家供大家参考,具体如下: 什么是递归? 简单地理解就是函数调用自身的过程就称之为递归. 什么时候用到递归? 如果一个问题可以表示为更小规模的迭代运算,就可以使用递归算法. 迷宫问题:一个由0或1构成的二维数组中,假设1是可以移动到的点,0是不能移动到的点,如何从数组中间一个值为1的点出发,每一只能朝上下左右四个方向移动一个单位,当移动到二维数组的边缘,即可得到问题的解,类似的问题都可以称为迷宫问题. 在python中可以使用list

随机推荐