PYTHON實現DES加密源碼

來源:互聯網
上載者:User

標籤:please   ring   input   handle   opera   global   校正   decode   base64   

要求是實現DES加密,解密,我是用python實現的,還是有挺多坑的,改bug就改了挺久,加密實現後,解密過程就比較輕鬆。

要求:輸入秘鑰為64位位元(有效位為56位,其中每八位最後一位為同位位元),明文為64位位元,輸出為64位位元,解密過程大致為逆向過程

代碼如下:

# -*- coding: utf-8 -*-import timeimport base64C0=‘‘D0=‘‘L0=‘‘R0=‘‘cipher=‘‘outtext=‘‘substitute1=[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]substitute2=[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]shift=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]KEY=[ dict() for i in range(16)]sbox= [        [        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,        ]    ]substituteip= [        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]selectoperate=[        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]substituteP=[        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]resubstituteip=[        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]def handleraw(string):    a=‘‘    for i in string:        a+=bin(ord(i)).replace(‘0b‘,‘‘).zfill(8)    return adef shiftleft(string,x):    return string[-(len(string)-x):]+string[:x]def encodepassword1(x):    global C0    global D0    for i in range(0,28):        C0+=x[substitute1[i]-1]    for j in range(28,56):        D0+=x[substitute1[j]-1]k=0def encodepassword2():    global k,C0,D0    s=‘‘    m=‘‘    if(k!=16):        C0=shiftleft(C0,shift[k])        D0=shiftleft(D0,shift[k])        s=C0+D0        for i in substitute2:            m+=s[i-1]        KEY[k]=m        k+=1        encodepassword2()def substituteIP(string):    global L0,R0    a=‘‘    for i in substituteip:        a+=string[i-1]    L0=a[:32]    R0=a[-32:]def sboxoperate():    mid=‘‘    for i in selectoperate:        mid+=R0[i-1]    mid=str(bin(int(mid,2)^int(KEY[m-1],2)).replace(‘0b‘,‘‘).zfill(48))    outraw=‘‘    for n in range(8):        a=int(mid[6*n]+mid[6*n+5],2)        b=int(mid[(6*n+1):(6*n+5)],2)        outraw+=str(bin(int(str(sbox[n][a*16+b]),10)).replace(‘0b‘,‘‘).zfill(4))    ‘‘‘print ‘S:‘,m,outraw‘‘‘    out=‘‘    for i in substituteP:        out+=outraw[i-1]    return outm=0def encodemain():    global m,L0,R0,cipher    ‘‘‘print ‘L0,R0‘,L0,R0‘‘‘    m+=1    a=L0    L0=R0    R0=str(bin(int(sboxoperate(),2)^int(a,2)).replace(‘0b‘,‘‘).zfill(32))    if(m!=16):        encodemain()    else:        b=R0+L0        ‘‘‘print ‘L0,R0‘,L0,R0‘‘‘        for i in resubstituteip:            cipher+=b[i-1]def decodecircle():    global L0,R0,outtext,m    a=R0    R0=L0    L0=str(bin(int(sboxoperate(),2)^int(a,2)).replace(‘0b‘,‘‘).zfill(32))    m-=1    if(m!=0):        decodecircle()    else:        d=L0+R0        decodetext=[0 for i in range(64)]        for i in range(64):            decodetext[substituteip[i]-1]=d[i]        outtext=‘‘.join(decodetext)def decoderesubstituteip(cipher):    global R0,L0    a=[0 for i in range(64)]    for i in range(64):        a[resubstituteip[i]-1]=cipher[i]    a=‘‘.join(a)    R0=a[:32]    L0=a[-32:]if __name__==‘__main__‘:    print ‘This is a DES encrypt and decrypt function‘    ‘‘‘    rawpassword=raw_input(‘please input the password:\n‘)    password=handleraw(rawpassword)    ‘‘‘    password=raw_input(‘please input the password:\n‘)    a=time.time()    encodepassword1(password)    encodepassword2()    ‘‘‘    print ‘the KEY is:\n‘,KEY    ‘‘‘    ‘‘‘    rawtext=raw_input(lea‘pse input the text:\n‘)    text=handleraw(rawtext)    ‘‘‘    b=time.time()    text=raw_input(‘please input the text:\n‘)    c=time.time()    substituteIP(text)    encodemain()    print ‘the cipher is:‘,cipher    ‘‘‘decode the cipher‘‘‘    decoderesubstituteip(cipher)    decodecircle()    print ‘the decodetext is:‘,outtext    print ‘the time cost is:‘,time.time()-c+b-a,‘s‘

 

  

PYTHON實現DES加密源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.