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