C#中的ISO 9797-1算法1 [CBC-MAC]

时间:2008-12-11 19:00:59

标签: algorithm cbc-mac

似乎CBC-MAC算法有6种变化。我一直在努力匹配PINPad 1000SE上的MAC算法[每本手册都是ISO 9797-1算法1]。

我从here开始了一个很好的开始。

我将算法编码如下:

public static byte[] CalculateMAC(this IPinPad pinpad, byte[] message, byte[] key)
{
    //Divide the key with Key1[ first 64 bits] and key2 [last 64 bits]
    var key1 = new byte[8];
    Array.Copy(key, 0, key1, 0, 8);

    var key2 = new byte[8];
    Array.Copy(key, 8, key2, 0, 8); //64 bits

    //divide the message into 8 bytes blocks
    //pad the last block with "80" and "00","00","00" until it reaches 8 bytes
    //if the message already can be divided by 8, then add 
    //another block "80 00 00 00 00 00 00 00"
    Action<byte[], int> prepArray = (bArr, offset) =>
                                     {
                                         bArr[offset] = 0; //80
                                         for (var i = offset + 1; i < bArr.Length; i++)
                                             bArr[i] = 0;
                                     };
    var length = message.Length;
    var mod = length > 8? length % 8: length - 8;

    var newLength = length + ((mod < 0) ? -mod : (mod > 0) ? 8 - mod : 0);
    //var newLength = length + ((mod < 0) ? -mod : (mod > 0) ? 8 - mod : 8);
    Debug.Assert(newLength % 8 == 0);

    var arr = new byte[newLength];
    Array.Copy(message, 0, arr, 0, length);
    //Encoding.ASCII.GetBytes(message, 0, length, arr, 0);
    prepArray(arr, length);
    //use initial vector {0,0,0,0,0,0,0,0} 
    var vector = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };

    //encrypt by DES CBC algorith with the first key KEY 1
    var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
    var cryptor = des.CreateEncryptor(key1, vector);
    var outputBuffer = new byte[arr.Length];
    cryptor.TransformBlock(arr, 0, arr.Length, outputBuffer, 0);

    //Decrypt the result by DES ECB with the second key KEY2 [Original suggestion]
    //Now I'm Encrypting
    var decOutputBuffer = new byte[outputBuffer.Length];
    des.Mode = CipherMode.ECB;
    var decryptor = des.CreateEncryptor(key2, vector);
    //var decryptor = des.CreateDecryptor(key2, vector);
    decryptor.TransformBlock(outputBuffer, 0, outputBuffer.Length, decOutputBuffer, 0);

    //Encrypt the result by DES ECB with the first key KEY1
    var finalOutputBuffer = new byte[decOutputBuffer.Length];
    var cryptor2 = des.CreateEncryptor(key1, vector);
    cryptor2.TransformBlock(decOutputBuffer, 0, decOutputBuffer.Length, finalOutputBuffer, 0);

    //take the first 4 bytes as the MAC
    var rval = new byte[4];
    Array.Copy(finalOutputBuffer, 0, rval, 0, 4);
    return rval;
}

然后我发现有3个填充方案,并且给我一个开始的方案可能不一定是正确的。手册又来了我的救援。看起来该设备仅填充0。还没有提到附加块,所以我做了以下更改:

    Action<byte[], int> prepArray = (bArr, offset) =>
                                     {
                                         bArr[offset] = 0; ... }

没有额外的块(如果mod 0 [可被8整除]不会改变数组长度)

var newLength = length + ((mod < 0) ? -mod : (mod > 0) ? 8 - mod : 0);

最初的建议要求我在第二步解密...但是Valery here建议它一直加密。所以我将Decrypt改为Encrypt。但是我仍然无法获得必要的MAC ......

手册中说明了密钥“6AC292FAA1315B4D8234B3A3D7D5933A”[因为密钥应该是16个字节,我在这里找到了十六进制字符串,所以我取了6A,C2,92,FA的字节值...... new byte [] {106,194,146,...]如果消息是[0x1a + MENTERODOMETER的字节数组],MAC应为7B,40,BA,95 [4字节]

有人可以帮忙吗?请?


因为Pinpad要求消息中的第一个字符是0x1a ...

public static byte[] CalculateAugmentedMAC(this IPinPad pinpad, string message, byte[] key)
{
    var arr = new byte[message.Length + 1];
    var source = Encoding.ASCII.GetBytes(message);
    arr[0] = 0x1a; //ClearScreenIndicator
    Array.Copy(source, 0, arr, 1, source.Length);
    return CalculateMAC(pinpad, arr, key);
}

我用这个输入调用上面的代码:

var result = pad.CalculateAugmentedMAC("MENTERODOMETER", new byte[] { 106, 194, 146, 250, 161, 49, 91, 77, 130, 52, 179, 163, 215, 213, 147, 58 });

3 个答案:

答案 0 :(得分:2)

大多数CBC MAC算法都在BouncyCastle的JCE提供程序中实现。

请注意:BouncyCastleProvider.java

你可能正在寻找DESEDEISO9797ALG1MACWITHISO7816-4PADDING,这是DESEDEMAC64WITHISO7816-4PADDING的别名,在这里实现(好吧,它是使用DESedeEngine和ISO7816d4Padding的CBCBlockCipherMac的特定配置,你必须在它们之间跳转一些课程,以获得完整的图片): JCEMac.java

另外,看看jPos:

JCESecurityModule.java

及其贡献的零售MAC算法实施:

retail-mac-contributed-by-vsalaman.zip

答案 1 :(得分:0)

我很确定(IIRC)您需要在最后调用TransformFinalBlock(每个加密器)。

答案 2 :(得分:0)

无法回答您的特定终端,但我用它来测试MAC。

public static byte[] GenerateMAC(byte[] key, byte[] data)
{
    using (MACTripleDES mac = new MACTripleDES(key))
        return mac.ComputeHash(data);
}
豫ICP备18024241号-1