Skip to content

Instantly share code, notes, and snippets.

@grocid
Created February 13, 2018 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grocid/fbbc52980af2001626389cf6611c7ed2 to your computer and use it in GitHub Desktop.
Save grocid/fbbc52980af2001626389cf6611c7ed2 to your computer and use it in GitHub Desktop.
from Crypto.Cipher import AES
def crc16(data):
"""
Calculate an ISO13239 CRC checksum of the input buffer (bytestring).
"""
m_crc = 0xffff
for this in data:
m_crc ^= ord(this)
for _ in xrange(8):
j = m_crc & 1
m_crc >>= 1
if j:
m_crc ^= 0x8408
return m_crc
otp = "cctglbkvkdglnnerijrlcldevckbbbninjccibignvln"
key = "a113f491bee316c628508b2e31fa8ca4".decode("hex")
mh = {
'c': '0', 'b': '1', 'd': '2', 'e': '3',
'f': '4', 'g': '5', 'h': '6', 'i': '7',
'j': '8', 'k': '9', 'l': 'a', 'n': 'b',
'r': 'c', 't': 'd', 'u': 'e', 'v': 'f'
}
mh_otp = ''.join(mh[i] for i in otp)
pub_id = mh_otp[:2*6] # length-6 id
msg = mh_otp[-32:].decode('hex')
cipher = AES.new(key, AES.MODE_ECB)
dec = cipher.decrypt(msg)
data = [ord(c) for c in dec]
assert(crc16(dec) == 0xf0b8)
# Parse data
response = {
"uid": dec[0:6],
"ctr": data[6] + (data[7] << 8),
"tst": data[8] + (data[9] << 8) + (data[10] << 16),
"scr": data[11],
"rnd": data[12] + (data[13] << 8),
"crc": data[14] + (data[15] << 8)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment