Keeloq的python實現

來源:互聯網
上載者:User

標籤:try   blog   bsp   int   div   turn   decrypt   nbsp   res   

KeeLoq_NLF = 0x3A5C742E

def bit(x,n):

    x = (((x)>>(n))&1)

    return x

 

def g5(x,a,b,c,d,e):

    y = (bit(x,a) + bit(x,b)*2 + bit(x,c)*4 + bit(x,d)*8 + bit(x,e)*16)

    return y

 

def Keeloq_Encrypt(data,key):

    x = int(data)

    for i in range(528):

        x = (x>>1) ^ ((bit(x,0) ^ bit(x,16) ^ bit(key,i&63) ^ bit(KeeLoq_NLF,g5(x,1,9,20,26,31)))<<31);

    return x

 

def Keeloq_Decrypt(data,key):

    x = int(data)

    for i in range(528):

        x = int(x<<1) ^ bit(x,31) ^ bit(x,15) ^ bit(key,(15-i)&63) ^ bit(KeeLoq_NLF,g5(x,0,8,19,25,30))

    return x

 

cmd = 0

while(cmd != 3):

    try:

        cmd = input("Please select the function \n 1:Keeloq Encrypt\n 2:Keeloq Decrypt\n 3:Quit\n select:")

        if cmd == 1:

            data1 = input("Please input data for Keeloq Encrypt:")

            Sk = input("Please input Sk:")

            result = Keeloq_Encrypt(data1,Sk)

            print(‘\nResult = %x\n‘%result)

        if cmd == 2:

            data1 = input("Please input data for Keeloq Decrypt:")

            Sk = input("Please input Sk:")

            result = Keeloq_Decrypt(data1,Sk) & 0xFFFFFFFF

            print(‘\nResult = %x\n‘%result)

    except:

        pass

print(‘End‘)

Keeloq的python實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.