注意最后一步要用整个key加密
计算过程:
MAC: PBOC-MAC DES算法
密钥 长度16(0x10)字节
57 75 20 4D 69 61 6F 6A 75 6E 40 47 26 44 43 11
初始向量 长度8(0x08)字节
00 00 00 00 00 00 00 00
数据 长度74(0x4A)字节
43 48 45 4E 48 41 4F 2D 50 43 7C 42 46 45 42 46 42 46 46 30 30 30 33 30 36 43 33 7E 39 42 50 30 50 44 32 43 4E 37 30 31 36 33 36 36 49 30 30 34 33 7C 32 30 32 34 30 31 31 31 7C 32 30 32 35 30 31 30 31 7C 38 31 39 31 7C 30
结果 长度8(0x08)字节
40 89 81 9D 3D 99 1C C7
计算过程
强制补0x80再用0x00补齐8的倍数得到4348454E48414F2D50437C424645424642464630303033303643337E39425030504432434E3730313633363649303034337C32303234303131317C32303235303130317C383139317C30800000000000
后续步骤与X99/X9.19一样
0000000000000000异或4348454E48414F2D得到4348454E48414F2D
用5775204D69616F6A对4348454E48414F2D进行DES加密得到9E21A468008DB6B4
9E21A468008DB6B4异或50437C4246454246得到CE62D82A46C8F4F2
用5775204D69616F6A对CE62D82A46C8F4F2进行DES加密得到196C0FEDC9D24187
196C0FEDC9D24187异或4246463030303330得到5B2A49DDF9E272B7
用5775204D69616F6A对5B2A49DDF9E272B7进行DES加密得到C41CE10A60F953E1
C41CE10A60F953E1异或3643337E39425030得到F25FD27459BB03D1
用5775204D69616F6A对F25FD27459BB03D1进行DES加密得到A31ED80C9E6D58F7
A31ED80C9E6D58F7异或504432434E373031得到F35AEA4FD05A68C6
用5775204D69616F6A对F35AEA4FD05A68C6进行DES加密得到82024776C27DDC18
82024776C27DDC18异或3633363649303034得到B43171408B4DEC2C
用5775204D69616F6A对B43171408B4DEC2C进行DES加密得到788739F54CE3DEF2
788739F54CE3DEF2异或337C323032343031得到4BFB0BC57ED7EEC3
用5775204D69616F6A对4BFB0BC57ED7EEC3进行DES加密得到6A808D8F7991B2FC
6A808D8F7991B2FC异或31317C3230323530得到5BB1F1BD49A387CC
用5775204D69616F6A对5BB1F1BD49A387CC进行DES加密得到B7EFC86C359642EA
B7EFC86C359642EA异或3130317C38313931得到86DFF9100DA77BDB
用5775204D69616F6A对86DFF9100DA77BDB进行DES加密得到FEC7A7C308C3357E
FEC7A7C308C3357E异或7C30800000000000得到82F727C308C3357E
用5775204D69616F6A756E404726444311对82F727C308C3357E进行DES加密得到4089819D3D991CC7
from Crypto.Cipher import DES, DES3def des_encrypt(data, key):"""DES加密函数,返回加密后的字节数据。"""cipher = DES.new(key, DES.MODE_ECB)return cipher.encrypt(data)def des3_encrypt(data, key):"""双倍长密钥的DES3加密函数,返回加密后的字节数据。"""cipher = DES3.new(key, DES3.MODE_ECB)return cipher.encrypt(data)def xor_bytes(a, b):"""对两个字节序列进行异或操作,返回结果字节序列。"""return bytes(x ^ y for x, y in zip(a, b))def pboc_mac(data, key, iv):"""PBOC MAC计算函数:param data: 原始数据,字节序列:param key: 加密密钥,字节序列,长度必须为16字节:param iv: 初始向量,字节序列,长度为8字节:return: 计算出的MAC值,字节序列"""# 确保密钥长度为16字节,切分成两个8字节的密钥key1, key2 = key[:8], key[8:]# 强制补0x80再用0x00补齐8的倍数data += b'\x80'data += b'\x00' * ((8 - len(data) % 8) % 8)print(f"补齐后的数据: {data.hex().upper()}")# 逐步进行异或和DES加密操作block = ivprint(f"初始向量: {block.hex().upper()}")final_mac= b''for i in range(0, len(data), 8):block = xor_bytes(block, data[i:i + 8])print(f"异或后第{i // 8 + 1}块: {block.hex().upper()}")if i//8+1 != len(data)//8: block = des_encrypt(block, key1)print(f"加密后第{i // 8 + 1}块: {block.hex().upper()}")else:# 最后一步用key1-key2双倍长密钥对结果进行DES-EDE加密print("block=",block.hex().upper())print("key=",(key).hex().upper())final_mac = des3_encrypt(block, key)print(f"最终加密结果: {final_mac.hex().upper()}")return final_mac# 示例数据
key = bytes.fromhex('5775204D69616F6A756E404726444311')
iv = bytes.fromhex('0000000000000000')
data = bytes.fromhex('1148454E48414F2D50437C424645424642464630303033303643337E39425030504432434E3730313633363649303034337C32303234303131317C32303235303130317C383139317C30')# 计算MAC
mac = pboc_mac(data, key, iv)# 打印结果
print("计算的MAC:", mac.hex().upper())# 验证结果是否正确
expected_mac = bytes.fromhex('B519448B9A29F920')
print("计算是否正确:", mac == expected_mac)