User Tools

Site Tools


Sidebar

retail_mac_alg3

Source:

from Crypto.Cipher import DES
from Crypto.Util.strxor import strxor
import binascii


def macIso9797_m2_alg3(key, msg):
    return macIso9797_alg3(key, msg, "80")


def macIso9797_m1_alg3(key, msg):
    return macIso9797_alg3(key, msg, "00")


def macIso9797_alg3(key, msg, pad_start):
    key_len = int(len(key) / 2)

    if (key_len != 16):
        raise ValueError("Key length should be 16 digits")

        # force header  padding
    msg += pad_start

    # padding with "00"
    lenRestOfData = int((len(msg) / 2) % 8)
    if lenRestOfData > 0 :
        msg += "00" * (8 - lenRestOfData)

    msg += "80"
    msg += "00" * 7
    # msg += "00" * 7
    # msg += "01"

    loopNum = int((len(msg) / 2) / 8)

    bufferOutput = binascii.unhexlify("00" * 8)
    IV = '\x00' * 8

    keya = binascii.unhexlify(key[0:16])
    keyb = binascii.unhexlify(key[16:])

    print ("\n")
    i = 0
    for i in range(0, loopNum):
        tdesa = DES.new(keya, DES.MODE_ECB, IV)

        data = msg[i * 16:i * 16 + 16]
        print(str(i) + "=" + data)

        x = bufferOutput
        bufferOutput = strxor(binascii.unhexlify(data), bufferOutput)
        print (data + " xor " + binascii.hexlify(x).decode('utf-8').upper() + " = " + binascii.hexlify(
            bufferOutput).decode('utf-8').upper())

        bufferOutput = tdesa.encrypt(bufferOutput)
        print (" encrypted val = " + binascii.hexlify(bufferOutput).decode('utf-8').upper())

        print ("\n")

    tdesb = DES.new(keyb, DES.MODE_ECB, IV)
    bufferOutput = tdesb.decrypt(bufferOutput)

    print (" decrypted val = " + binascii.hexlify(bufferOutput).decode('utf-8').upper())

    tdesa = DES.new(keya, DES.MODE_ECB, IV)
    bufferOutput = tdesa.encrypt(bufferOutput)

    print (" encrypted val = " + binascii.hexlify(bufferOutput).decode('utf-8').upper())

    return bufferOutput

if __name__ == '__main__' :
    macIso9797_alg3('6522B4E171195BB218223A976C040111','937745C20883A1BAD1E04193722A1592378F81A8F1DC589157AEB0F7544FA1BA','')

Execute Result:

C:\Users\liqh99\AppData\Local\Programs\Python\Python37\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 49936 --file D:/work/py/test/mac/main.py
pydev debugger: process 2252 is connecting

Connected to pydev debugger (build 191.7479.30)


0=937745C20883A1BA
937745C20883A1BA xor 0000000000000000 = 937745C20883A1BA
 encrypted val = 14E6E7D66AF44F20


1=D1E04193722A1592
D1E04193722A1592 xor 14E6E7D66AF44F20 = C506A64518DE5AB2
 encrypted val = 2269A469B411EBE9


2=378F81A8F1DC5891
378F81A8F1DC5891 xor 2269A469B411EBE9 = 15E625C145CDB378
 encrypted val = 33BFAEA2B3C71F66


3=57AEB0F7544FA1BA
57AEB0F7544FA1BA xor 33BFAEA2B3C71F66 = 64111E55E788BEDC
 encrypted val = 4646C6DA7A4B2A41


4=8000000000000000
8000000000000000 xor 4646C6DA7A4B2A41 = C646C6DA7A4B2A41
 encrypted val = 51810BABD874CAC3


 decrypted val = 3E1C13A3C18872B3
 encrypted val = 1AD7FB6A3389E017

Process finished with exit code 0


retail_mac_alg3.txt · Last modified: 2019/08/19 13:21 by liqh99