/// <summary> /// Calculates a MAC using the X9.19 algorithm. /// </summary> /// <param name="data">Hex data to MAC.</param> /// <param name="key">MACing key.</param> /// <param name="iv">Initial vector.</param> /// <param name="blockType">Message block to MAC.</param> /// <returns>MAC result.</returns> public static string MacHexData(string data, HexKey key, string iv, IsoX919BlockType blockType) { if (data.Length % 16 != 0) { data = Iso9797Pad.PadHexString(data, Iso9797PaddingMethodType.PaddingMethod1); } for (var i = 0; i <= (data.Length / 16) - 1; i++) { iv = iv.XorHex(data.Substring(i * 16, 16)); iv = TripleDes.DesEncrypt(key.PartA, iv); } var result = iv; if (blockType == IsoX919BlockType.FinalBlock || blockType == IsoX919BlockType.OnlyBlock) { result = TripleDes.DesDecrypt(key.PartB, iv); result = TripleDes.DesEncrypt(key.PartA, result); } return(result); }
public void TestByteDes(byte[] key, byte[] data, byte[] expected) { Assert.AreEqual(expected, TripleDes.DesEncrypt(key, data)); Assert.AreEqual(data, TripleDes.DesDecrypt(key, expected)); }