Python encryption and decryption instance based on DES algorithm

Source: Internet
Author: User
Tags join range

This article describes the Python based on DES algorithm encryption and decryption implementation method. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 #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,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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8,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,4 0, 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 (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 '. Join ('%2x '% ord (W) ) for W-s), __hex_bin) __db = partial (lambda s: '. Join (Chr (int (s[i:i+8), 2)) for I in range (0, Len (s), 8)) __s = P Artial (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 = P Artial (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, b ASE=10): if base = = 2:pass Elif Base = = 16:key = '. Join (Self.__class__.__hex_bin[w] for W key) Else:key = Self.__cl Ass__.__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 = self.__s (r) r = Self.__p (r) r = Self.__f (R, l) L = r_t ret Urn 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.__kA = = '%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)): Before = '. Join (Self.__class__.__hex_bin[s[j]] to J in range (I, i+16)) after = Self.__code (befo Re, self.__k[::-1]) A + + self.__class__.__db return A.rstrip (). Decode (' Utf-8 ') if __name__ = = ' __main__ ': D = DES () D.input_key (' 123456789 ') s = ' another Chinese ' a = D.encode (s) print a b = D.decode (a) Print B

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.