Python version des and Mac algorithms

Source: Internet
Author: User
Tags binary to decimal decimal to binary hex to binary hex to decimal key loop ord

The DES algorithm in Python has been used in recent work, although there are ready-made libraries, but the total feeling is inconvenient. The Des and Mac algorithms previously written in C and Java are then ported to Python. There's no problem with the test.

It's convenient after that. Finally, the DES algorithm can be used in Python. Before the C-write des algorithm encapsulated into a dynamic library, and then by the Python ctypes call, although can but not very convenient.

Attached: Python version des algorithm source code, has not encapsulated into a class, is still in perfect. SOURCE CSDN resources can be downloaded. Direct compile execution no problem.

#!/usr/bin/env python#-*-coding:utf-8-*-# 2014/10/16 wrote by yangyongzhen# qq:534117529# global definition# base = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, f]__author__ = ' yangyongzhen ' base = [str (x) for x in range] + [Chr (x) fo R x in range (Ord (' A '), Ord (' a ') +6)]# bin2dec# binary to decimal: Int (str,n=10) def bin2dec (string_num): Return str (int (string_num, 2) # hex2dec# hex to decimal def hex2dec (string_num): Return str (int (string_num.upper (), +)) # dec2bin# decimal to binary: Bin () def de C2bin (string_num): num = Int (string_num) mid = []while true:if num = = 0:breaknum,rem = Divmod (num, 2) mid.append (Base[rem]) r Eturn '. Join ([STR (x) for x in Mid[::-1]]) # dec2hex# Decimal to octal: Oct () # Decimal to 16 binary: Hex () def dec2hex (string_num): num = in  T (string_num) if Num==0:return ' 0 ' mid = []while true:if num = = 0:breaknum,rem = Divmod (num, +) Mid.append (Base[rem]) return '. Join ([STR (x) for x in Mid[::-1]]) # hex2tobin# hex to binary: Bin (int (str,16)) def hex2bin (string_num): Return Dec2bin (hex2 Dec (String_num.upper ())) # bin2hex# binary to 16 binary: Hex (int (str,2)) def bin2hex (string_num): Return Dec2hex (Bin2dec (string_num)) '/** * pboc3des encryption Algorithm * @author Administrator * */"class pboc_des ():p"/** *************************** compression Replacement s-box********************** */"' subkey = [([0] * *) for-ll in range (+)]s1 = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12 , 5, 9, 0, 7],[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10,  5, 0],[, 8, 2, 4, 9, 1, 7, 5, one, 3,, 0, 6,]]s2 = [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0 and 5, 10],[3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],[0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],[ 8, 1, 3, A, 4, 2, one, 6, 7, 0, 5, 9,,]S3 = [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],[1 3, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],[13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],[1, 10, 13 , 0, 6, 9, 8, 7, 4, 15, 14, 3,One, 5, 2,]S4 = [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],[13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1  , 10, 14, 9],[10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],[3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2,  ]S5 = [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],[14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8,  6],[4, 2, 1, one, ten, 7, 8, 9, 5, 6, 3,, 0, 8, 7, 1, 2, 6,, 0,, 9, 4, 5]]s6.  = [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],[10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],[9, 5, 2, 8, ten, 3, 7, 0, 4, ten, 1,, 6],[4, 3, 2,, 9, 5, 1, 7, 6, 0, 8,]s7 = [[4], 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],[13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],[1, 4, 11, 1  3, 3, 7, 6, 8, 0, 5, 9, 2],[6, one,, 8, 1, 4, 7, 9, 5,, 0, 2, 3,,]S8 = [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],[1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],[7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],[2, 1,, 7, 4, ten, 8,,, 9, 0, 3, 5, 6, each]]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 , 3,61, one, one, 5, 7, 40, 48, 16, 56, 24, 64, 32, 8,, +,, +----------- , 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]# each key loop left shift number ls = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2,2, 1] "/** * IP initial permutation * @param source * @return */" def CHANGEIP (source):d est= [0]*64global ipfor I   In range:d est[i] = source[ip[i]-1]return destdef string2binary (str): le = Len (str) dest =[0]*le*4i = 0for C in str: i + = 4 J = 0 S = Hex2bin (c) L = Len (s) for D in S:dest[i-l+j]=int (d) J + = 1return dest "/** * IP-1 Reverse * @param source * @return */" def CH Angeinverseip (source):d est = [0]*64global _ipfor i in range (+):d est[i] = source[_ip[i]-1]return dest '/** * * Get wheel key (4 8bit) * * @param source * * @return * */"def setkey (source): Global subkey# Reload 4bittemp = string2binary (source) # 6bit are divided into two parts  Split left = [0]*28right = [0]*28# pc-14bit converted 6bittemp1 = [0]*56TEMP1 = keypc_1 (temp) # Printarr (TEMP1); #将经过转换的temp1均分成两部分for I in range: left[i] = temp1[i]right[i] = temp1[i + 28]# After 16 cycles left, then PC-2 displace for I in range (+): Ieft = Keyleftmove Ls[i]) right = Keyleftmove (right, Ls[i]) for J in range: temp1[j] = Left[j]temp1[j + [] = Right[j]subkey[i] = keypc_2 (te MP1) "/** * * 6bit key converted to 48bit * @param source * @return * */" def keypc_2 (source):d est = [0]*48temp = [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 , 48, 44, 49, 39, 56, 34, ]for i in range:d est[i] = source[temp[i]-1]return dest "/** * * * Move the key to the left I * @param sourc * * * Number of E-binary keys * @param i loop left shift number * @return * */"def keyleftmove (source, i): temp = 0global Lsle = len (source) ls = ls[i]for k i n Range (ls): temp = source[0]for J in range (le-1): source[j] = source[j + 1]source[le-1] = Tempreturn source "'/** * * 4bi The key of T is converted to 56bit * @param source * @return * */"def keypc_1 (source):d est = [0]*56temp = [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,  (6), 5, 4]for i in range (:d est[i] = source[temp[i]-1]return-------------- Dest '/** * Two equal-length arrays do XOR * @param source1 * @param source2 * @return * * * "def diffor (Source1, source2): le = Len (source1) d EST = [0]*lefor i in Range (le):d est[i] = source1[i] ^ source2[i]return dest "/** * * des encryption---> symmetric key * D = Ln (32bit) +rn ( 32bit) * After 16-wheel-mount * @param D (16byte) plaintext * @param K (16byte) Wheel key * @return (16byte) ciphertext */"DEF encryption (d, K): temp = [0]*64;data = String2binary (d # first Step Initial data = CHANGEIP (data) left = [([0] * +) for I in range (+)]right = [([0] * +) for I in range (+)]for J in range  (+): left[0][j] = data[j]right[0][j] = data[j + 32]setkey (K) # sub Key okfor I in range (1,17): # get (48bit) wheel key = Subkey[i -1]# L1 = r0left[i] = right[i-1]# R1 = L0 ^ f (r0,k1) ftemp = f (right[i-1], key) # 32bitright[i] = diffor (left[i-1), ftemp) #组合的时候, switch around for I in range (+): temp[i] = right[16][i]temp[32 + i] = Left[16][i]temp = CHANGEINVERSEIP (temp) str = Bina RY2ASC (INTARR2STR (temp)) return str "/** * 8bit Compressed 2bit * @param source (48bit) * @return R (32bit) b=e (R) ⊕k, divide 48 bits of B into 8 groupings, b =B1B2B3B4B5B6B7B8 */"def press (source): ret = [0]*32temp = [([0] * 6) for I in range (8)]s =[s1,s2,s3,s4,s5,s6,s7,s8]st =[]for I in range (8): to J in range (6): temp[i][j] = source[i * 6 + j]for I in range (8): # (+) x = temp[i][0] * 2 + temp[i][ 5]# (2345) y = temp[i][1] * 8 + temp[i][2] * 4 + temp[i][3] * Temp[i][4]val = S[I][X][Y]CH = Dec2hex (str (val)) # System.out.println ("x=" + x + ",  Y= "+ y +"-"+ ch); # String ch = integer.tobinarystring (val); St.append (CH) # System.out.println (str.tostring ()); ret = String2binary (ST) # Printarr (ret); # permutation pret = Datap (ret) return ret '/** * permutation P (32bit) * @param source * @return */"Def da TaP (source):d est = [0]*32temp = [16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9 , 6, A, 4,]le = Len (source) for I in Range (le):d est[i] = source[temp[i]-1]return dest ' '/** * 2bit extension  8bit * @param source * @return */"def expend (source): ret = [0]*48temp = [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 1, 1]for i in range: ret[i] = source[temp[i]-1]return ret "'/** * @param R (2bit) * @param K (48bit wheel-tight * @return 32bit */"Def f (R, K):d EST = [0]*32temp = [0]*48# First input 32bit expansion 8bitexpendR = Expend (R) # 48bit# with wheel key xor Operation temp = Diffor (Expendr, K); # compression 2bitdest = Press (temp) return dest '/** * Stitching an array of type int into a string * @param arr * @return */"Def intarr2str (arr): SB = []le=len (arr) for I I N Range (le): Sb.append (str (arr[i))) return '. Join (SB) '/** * Converts binary string to hexadecimal character * @param s * @return */"def BINARY2ASC (s) : st = ' II = 0le= len (s) #不够4bit左补0if le% 4! = 0:while II < (4-len% 4): s = "0" + sle=le/4for i in Range (le): st + = Bi N2hex (s[i * 4:i * 4 + 4]) return Stif __name__== "__main__":D = ' 1111111111111111 ' k= ' ffffffffffffffff ' printencryption (D,K)


Python version des and Mac algorithms

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.