Skip to content

Instantly share code, notes, and snippets.

@nmiglio
Last active February 18, 2020 10:47
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 nmiglio/88c7e4d408668777675e73b3793fd944 to your computer and use it in GitHub Desktop.
Save nmiglio/88c7e4d408668777675e73b3793fd944 to your computer and use it in GitHub Desktop.
decryption of AES-128 CBC with IV=0 and ephimeral key (OMS telegrams MODE 7)
from Crypto.Hash import CMAC
from Crypto.Cipher import AES
init_vector = bytearray(16) # IV=0
aes_key_str = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
full_tg_str = "B346FA121028000001068C00DB900F002C25030000008298D056148A8B757AE100900710EB64AF75C4FC415400EF598690C50CA2494E0AE679FE25D3BC322881E6EEFF4C34618226A68DA5E9D33DD9DC45C72D8BA8170C6D13551CE15E1FC67AF1D642F1EE51E886A6C242C3FB9CCC0B60F7DAFF0E62E27BF5FD1A7FACD16C0975ED870A7D119F1F87BBC06B201D1509A01088402FE9A3DFD985DD6FB178236A6DED2DEE4FA81867003CEFAFA24B1727F2786812"
stripped_tg = full_tg_str[72:] # strip plain data
# build ephimeral key
id_str = full_tg_str[8:16]
mcr_str = full_tg_str[36:44]
m1 = bytearray.fromhex(f"00{mcr_str}{id_str}07070707070707")
cobj1 = CMAC.new(bytearray.fromhex(aes_key_str), ciphermod=AES, msg=m1)
kenc = cobj1.hexdigest()
cipher = AES.new(bytearray.fromhex(kenc), AES.MODE_CBC, init_vector)
to_be_decoded = bytearray.fromhex(stripped_tg)
decoded_data = bytearray()
while len(to_be_decoded):
encoded_block = to_be_decoded[0:16]
print(f"Decoding block {[f'0x{n:02X}' for n in encoded_block]}")
decoded_block = cipher.decrypt(encoded_block)
print(f"--> {[f'0x{n:02X}' for n in decoded_block]}")
decoded_data.extend(bytearray(decoded_block[:16]))
to_be_decoded = to_be_decoded[16:]
print(f"{[f'0x{n:02X}' for n in decoded_data]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment