This paper describes the implementation method of Python encryption and decryption based on DES algorithm. Share to everyone for your reference. The implementation method is as follows:
#coding =utf-8 from functools import partial Import Base64 Class des (object): "" "DES encryption algorithm 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,2 2,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,1 9,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 , 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,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3,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 , 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 ': ' 0 ' 101 ', ' 6 ': ' 0110 ', ' 7 ': ' 0111 ', ' 8 ': ' + ', ' 9 ': ' 1001 ', ' A ': ' 1010 ', ' B ': ' 1011 ', ' C ': ' 1100 ', ' d ': ' 1101 ', ' e ': ' 1110 ', ' F ': ' 1 111 ', ': ' 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 he X_bin, S: '. Join (Hex_bin[w] for W ". 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 ()), __k0, __k2) __k = par Tial (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" 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 (+): r_t = r r = self.__e (r) r = Self.__f (r, K[i]) R = SE lf.__s (r) r = Self.__p (r) r = Self.__f (R, l) L = r_t return Self.__class__.__ip1 (r+l) def ENC Ode (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 = ' Another paragraph Chinese ' a = D.encode (s) print a b = D.decode (a) Print B
Hopefully this article will help you with Python programming.