我有一个项目,它为 24 字节的数据 block 规定了以下加密规则。
1) Cryptography should be done using full triple DES MAC algorithm as defined in 9797-1 as MAC algorithm 3 with output transformation 3 without truncation and with DES in CBC mode as block cipher with ICV set to zeros. Last 8 bytes of encrypted data constitute the value we need.
程序说加密是错误的。我还需要做其他事情来满足上述规范吗?
数据是一个 24 字节的值,加密的输出应该是 8 个字节,我猜(根据规范)。我将整个 24 个字节作为输出:(
我写了下面的代码来实现上述规范:
des.KeySize = 128;
des.Key = ParseHex(key);
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.None;
ICryptoTransform ic = des.CreateEncryptor();
CryptoOutput = ic.TransformFinalBlock(CryptoOutput, 0, 24);
我也试过这个:
MACTripleDES des = new MACTripleDES(ParseHex(key));
byte[] CDCryptp = des.ComputeHash(CryptoOutput);
最佳答案
ISO 9797-1 MAC 算法 3 包括使用第一个 DES key 执行 CBC MAC,然后仅对最后一个 block 执行完整的 3-DES 操作。
试试这个:
byte[] keybytes = ParseHex(key);
byte[] key1 = new byte[8];
Array.Copy(keybytes, 0, key1, 0, 8);
byte[] key2 = new byte[8];
Array.Copy(keybytes, 8, key2, 0, 8);
DES des1 = DES.Create();
des1.Key = key1;
des1.Mode = CipherMode.CBC;
des1.Padding = PaddingMode.None;
des1.IV = new byte[8];
DES des2 = DES.Create();
des2.Key = key2;
des2.Mode = CipherMode.CBC;
des2.Padding = PaddingMode.None;
des2.IV = new byte[8];
// MAC Algorithm 3
byte[] intermediate = des1.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
// Output Transformation 3
byte[] intermediate2 = des2.CreateDecryptor().TransformFinalBlock(intermediate, intermediate.Length - 8, 8);
byte[] result = des1.CreateEncryptor().TransformFinalBlock(intermediate2, 0, 8);
关于c# - 如何在 C# 中使用三重 DES 执行 ISO 9797-1 MAC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6055763/