c# - 如何在 C# 中使用三重 DES 执行 ISO 9797-1 MAC?

标签 c# hash cryptography des 3des

我有一个项目,它为 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/

相关文章:

java - iText/BouncyCaSTLe ClassNotFound org.bouncycaSTLe.asn1.DEREncodable 和 org.bouncycaSTLe.tsp.TimeStampTokenInfo

java - XMLSignature默认为RSA,如何验证SSL签名的DSA曲线?

c# - 将复杂参数传递给 [Theory]

c# - 在C#中的XML中获取Parent二级节点值

c# - 将 "Promo Code"功能添加到我的 ASP.NET C# 网站

java - 不懂Guava PrimitiveSink

c# - 给定单个数据库表,是否可以在 sql 查询中生成报告矩阵作为结果集

c# - 可选参数和接口(interface) - 意外结果

python - 从 Web URL 生成 12 字节散列的算法

Java 11 Curve25519 实现不作为信号库