玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python实现DES加密算法源码例详解

python 培训

本篇为大家提供的是关于Python实现DES加密算法源码例详解,讲解了DES加密的算法原理及具体加密方法。DES加密Python语言的实现,代码比较长,大家可以拆解来学习。

DES数据加密原理:DES使用一个 56 位的密钥以及附加的 8 位奇偶校验位,每组的第8位作为奇偶校验位,从而产生最大 64 位的分组大小。迭代的分组密码,其中将加密的文本块分成两半。一半应用循环功能,然后将输出与另一半进行“异或”运算...具体加密原理大家可以百度下,这里就不详细介绍了。

Python实现DES加密算法源码例详解,源代码如下:

#coding=utf-8  

from functools import partial  
      
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,0x2,0x8,0xe,0xc,0x0,0x1,0xa,0x6,0x9,0xb,0x5,  
        0x0,0xe,0x7,0xb,0xa,0x4,0xd,0x1,0x5,0x8,0xc,0x6,0x9,0x3,0x2,0xf,  
        0xd,0x8,0xa,0x1,0x3,0xf,0x4,0x2,0xb,0x6,0x7,0xc,0x0,0x5,0xe,0x9,  
        ],  
        [  
        0xa,0x0,0x9,0xe,0x6,0x3,0xf,0x5,0x1,0xd,0xc,0x7,0xb,0x4,0x2,0x8,  
        0xd,0x7,0x0,0x9,0x3,0x4,0x6,0xa,0x2,0x8,0x5,0xe,0xc,0xb,0xf,0x1,  
        0xd,0x6,0x4,0x9,0x8,0xf,0x3,0x0,0xb,0x1,0x2,0xc,0x5,0xa,0xe,0x7,  
        0x1,0xa,0xd,0x0,0x6,0x9,0x8,0x7,0x4,0xf,0xe,0x3,0xb,0x5,0x2,0xc,  
        ],  
        [  
        0x7,0xd,0xe,0x3,0x0,0x6,0x9,0xa,0x1,0x2,0x8,0x5,0xb,0xc,0x4,0xf,  
        0xd,0x8,0xb,0x5,0x6,0xf,0x0,0x3,0x4,0x7,0x2,0xc,0x1,0xa,0xe,0x9,  
        0xa,0x6,0x9,0x0,0xc,0xb,0x7,0xd,0xf,0x1,0x3,0xe,0x5,0x2,0x8,0x4,  
        0x3,0xf,0x0,0x6,0xa,0x1,0xd,0x8,0x9,0x4,0x5,0xb,0xc,0x7,0x2,0xe,  
        ],  
        [  
        0x2,0xc,0x4,0x1,0x7,0xa,0xb,0x6,0x8,0x5,0x3,0xf,0xd,0x0,0xe,0x9,  
        0xe,0xb,0x2,0xc,0x4,0x7,0xd,0x1,0x5,0x0,0xf,0xa,0x3,0x9,0x8,0x6,  
        0x4,0x2,0x1,0xb,0xa,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':'010
2000
0','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)  

#www.iplaypython.com
      
        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()  
      
      
    if __name__ == '__main__':  
        d = DES()  
        d.input_key('密码8位!')  
        s = '这是原文\n'  
        a = d.encode(s)  
        print a  
        b = d.decode(a)  
        print b  

Python实现DES加密算法源码例详解,源码第二部分:

#coding=utf-8  

import unittest  
from des import DES  
      
    class KnownValues(unittest.TestCase):  
        """"""  
        __known_values = (('12345678','ABC我们大家','388a9c61849462e20af277ffe6891072'),  
                )  
      
        def test_encode(self):  
            des = DES()  
            for key, before, after in self.__known_values:  
                des.input_key(key)  
                result = des.encode(before)  
                self.assertEqual(after, result)  
      
        def test_decode(self):  
            des = DES()  
            for key, after, before in self.__known_values:  
                des.input_key(key)  
                result = des.decode(before)  
                self.assertEqual(after, result)  
      
    class BadInput(unittest.TestCase):  
        """"""  
        __key_inputs = (  
                )  
        def test_input_key(self):  
            des = DES()  
            self.assertRaises(IndexError, des.input_key, '1234567', 10)  
            self.assertRaises(IndexError, des.input_key, '123我阿, 4567', 2)  
            self.assertRaises(KeyError, des.input_key, '123我哦哦哦, 4567', 16)  
      
        def test_encode(self):  
            pass  
      
        def test_decode(self):  
            des = DES()  
            self.assertRaises(KeyError, des.decode, '123我哦哦哦, 4567')  
      
    class SanityCheck(unittest.TestCase):  
        """"""  
        """"""  
        __encode_inputs = (('asfd我 ','的萨sa1'),  
                )  
        __decode_inputs = (('12345678','388a9c61849462e20af277ffe6891072'),  
                )  
      
        def test_encode(self):  
            des = DES()  
            for key, before in self.__encode_inputs:  
                des.input_key(key)  
                after = des.encode(before)  
                result = des.decode(after)  
                self.assertEqual(before, result)  
      
        def test_decode(self):  
            des = DES()  
            for key, before in self.__decode_inputs:  
                des.input_key(key)  
                after = des.decode(before)  
                result = des.encode(after)  
                self.assertEqual(before, result)  
      
    if __name__ == '__main__':  
        unittest.main()  

以上Python源码供参考,如要使用请自行整理后再应用。

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/security/se2545.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-20 19:42 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部