Python implementations of KeeLoq

Source: Internet
Author: User
Tags decrypt

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) * * + bit (x,c) + 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 ')

Python implementations of KeeLoq

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.