Python DES-based encryption and decryption instance

Source: Internet
Author: User
This article mainly introduces Python's Implementation Method of encryption and decryption Based on the DES algorithm, and analyzes the related skills of the DES algorithm in the form of examples, for more information about how to implement Python's DES-based encryption and decryption, see the example in this article. Share it with you for your reference. The specific 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 =,, 24, 39,] _ ip1 = [, 1, 58,26, 33,1, 41, 9,49, 17,57, 25,] _ e = [32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 13, 12, 13, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1,] _ p = [16, 17, 1, 26, 2, 8, 24, 14, 3, 9, 19,13, 30, 6, 22, 11,] _ s = [[0xe, 0x4, 0xd, 0x2, 0xf, 0xb, 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, 0x9, 0x5, 0xb, 0x3, 0xe, 0xa, 0x6, 0xd,], [0xf, 0x8, 0xe, 0x6, 0xb, 0x2, 0xd, 0xc, 0x5, 0xa, 0x3, 0xd, 0x4, 0x7, 0xf, 0x2, 0x8, 0xe, 0xc, 0x0, 0x 1, 0xa, 0x6, 0x9, 0xb, 0x5, 0x0, 0xe, 0x7, 0xb, 0xa, 0x4, 0xd, 0x5, 0x8, 0xc, 0x6, 0x9, 0x3, 0x2, 0xf, 0xd, 0x8, 0xa, 0x3, 0xf, 0x2, 0xb, 0x6, 0x7, 0xc, 0x5, 0xe, 0x9,], [0xa, 0x9, 0xe, 0x6, 0x3, 0xf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 0x5, 0x2, 0x8, 0xc, 0x3, 0x7, 0x0 0x4, 0xa, 0x1, 0xd, 0xb, 0x6, 0x2, 0xc, 0x9, 0x5, 0xf, 0xa, 0xb, 0xe, 0x6, 0x0, 0x8, 0xd,], [0x4, 0xb, 0x2, 0xe, 0xf, 0x0 0 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, 0x9, 0xc, 0xe, 0x0x6, 0xa, 0xd, 0xf, 0x3, 0x5, 0x8, 0x2, 0x1, 0xe, 0x7, 0x4, 0xa, 0x8, 0xd, 0xf, 0xc, 0x9, 0x0 0x3, 0x5, 0x6, 0xb,],] _ k1 = [, 49, 9, 1, 10,, 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, 2,] _ k0 =, 0000, 0001,] _ hex_bin = {'0': '20170', '1': '20170', '2 ': '123', '3': '123', '4': '123', '5': '123', '6': '123', '7 ': '000000', '8': '000000', '9': '000000', 'A': '000000', 'B': '000000', 'C ': '000000', 'D': '000000', 'E': '000000', 'F': '000000 ','': '000000'} _ 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 (h Ex_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 _ ini T _ (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 = 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. rstrip (). decode ('utf-8') if _ name _ = '_ main _': d = DES () d. input_key ('000000') s = 'Next to a Chinese character 'a = d. encode (s) print a B = d. decode (a) print B

I hope this article will help you with Python programming.

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.