Skip to content

Instantly share code, notes, and snippets.

@matteomattei
Created July 8, 2014 08:15
Show Gist options
  • Save matteomattei/f9b5e4263c9a10f01305 to your computer and use it in GitHub Desktop.
Save matteomattei/f9b5e4263c9a10f01305 to your computer and use it in GitHub Desktop.
How to implement MAC_X919 algorithm in Python
#!/usr/bin/env python2
import Crypto.Cipher.DES as des
import Crypto.Cipher.DES3 as des3
def mac_x919(key,data):
while len(data) % 8 != 0:
data += '\x00'
des_key1 = des.new(key[0:8],des.MODE_CBC)
des_key2 = des.new(key[0:8],des.MODE_ECB)
buf = des_key1.encrypt(data)
buf = buf[len(buf)-8:]
buf = des_key2.decrypt(buf)
des3_key = des3.new(key,des3.MODE_ECB)
buf = des3_key.encrypt(buf)
return buf[0:4]
mac = mac_x919('0123456789abcdef','test data to be encrypted')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment