如何在C#中使用三重DES执行ISO 9797-1 MAC? [英] How can I do an ISO 9797-1 MAC with triple DES in C#?

查看:1037
本文介绍了如何在C#中使用三重DES执行ISO 9797-1 MAC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它为24字节的数据块规定了以下加密规则。


1)加密应该使用9797-1中定义的完全三重DES MAC算法作为MAC
算法3,其中输出变换3没有截断,并且在CBC模式中具有DES,而块C
具有ICV设置为零的加密。最后8个字节的加密数据构成了我们需要的值。


程序说加密完成是错误的。是否还有其他事情需要做匹配上面的规范?



数据是一个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密钥执行CBC MAC,然后只对最后一个块执行完整的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();
des1Key = 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算法3
byte [] intermediate = des1.CreateEncryptor()。TransformFinalBlock(data,0,data.Length);

//输出转换3
byte [] intermediate2 = des2.CreateDecryptor()。TransformFinalBlock(intermediate,intermediate.Length - 8,8);
byte [] result = des1.CreateEncryptor()。TransformFinalBlock(intermediate2,0,8);


I've got a project which stipulates the following encryption rules for a 24 byte block of data.

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.

The program is saying the encryption done is wrong. Are there any other things I need to do to match the above spec?

The data is a 24 byte value and output of the encryption should be 8 bytes, I guess (as per the spec). I am getting the whole 24 bytes as output :(

I wrote the following code to achieve the said specification:

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);

I tried this also:

MACTripleDES des = new MACTripleDES(ParseHex(key));
byte[] CDCryptp = des.ComputeHash(CryptoOutput);

解决方案

ISO 9797-1 MAC Algorithm 3 consists of using the first DES key to perform a CBC MAC and then only for the final block perform a full 3-DES operation.

Try this:

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#中使用三重DES执行ISO 9797-1 MAC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆