我应该在C#中实现一种MAC-CBC生成方法,并提供有关密码算法的一些信息。我现在拥有的是:
byte[] {11, 11, 11, 11, 11, 11, 11, 11}Instance Vector = new byte[8]加密(8个字节值为0)。(CBC?)有了这些信息,我实现了以下方法:
public static string Encrypt(byte[] data)
{
var IV = new byte[8];
var key = new byte[] { 11, 11, 11, 11, 11, 11, 11, 11 };
var result = new byte[16];
// Create DES and encrypt.
var des = DES.Create();
des.Key = key;
des.IV = IV;
des.Padding = PaddingMode.None;
des.Mode = CipherMode.CBC;
ICryptoTransform cryptoTransform = des.CreateEncryptor(key, IV);
cryptoTransform.TransformBlock(data, 0, 16, result, 0);
// Get the last eight bytes of the encrypted data.
var lastEightBytes = new byte[8];
Array.Copy(result, 8, lastEightBytes, 0, 8);
// Convert to hex.
var hexResult = string.Empty;
foreach (byte ascii in lastEightBytes)
{
int n = (int)ascii;
hexResult += n.ToString("X").PadLeft(2, '0');
}
return hexResult;
}他们提供的原始数据示例是:input=byte[] {0, 6, 4, 1, 6, 4, 1, 7, E, E, F, F, F, F, B, B),它应该返回值:A7CBFB3C730B059C的输出。这意味着加密数据的最后八个字节应该是:byte[] {167, 203, 251, 60, 115, 11, 05, 156}。
但不幸的是,使用上述方法,我得到:32D91200D0007632。意味着我的加密数据是不正确的。(我的方法生成的加密值的最后八个字节是byte[] {50, 207, 18, 0, 208, 0, 118, 50})。
有什么办法能让我找到去A7CB.该做些什么?我做错了什么吗?
发布于 2011-10-03 10:21:19
CBC-MAC需要一个零初始化向量.更好的办法是明确说明第四章:
var IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; 您说您的密钥是byte[] { 11, 11, 11, 11, 11, 11, 11, 11 },是十六进制中的字节还是基数为10的字节?你可能想试试:
var key = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };看看能不能更好点。
发布于 2011-10-03 13:49:04
SymmetricAlgorithm项目有一个通用的MAC-CBC实现,可以在任何单声道上工作--即使它在内部只用于实现MACTripleDES。
您可以找到MIT.X11许可的源代码这里。按原样使用,或者将其与您自己的代码进行比较。
https://stackoverflow.com/questions/7631998
复制相似问题