如何使用 DES 实施 CBC-MAC?

发布于 2024-12-07 21:08:43 字数 1566 浏览 0 评论 0原文

我应该用 C# 实现 MAC-CBC 生成方法,并提供一些有关加密算法的信息。这就是我所拥有的:

  • 我应该使用 DES。
  • 密钥为 byte[] {11, 11, 11, 11, 11, 11, 11, 11}
  • 数据(16 字节)应以 8 字节部分加密。前 8 个字节使用 Instance Vector = new byte[8] 进行加密(8 个字节,值为 0)。 (CBC?)
  • 加密值的最后 8 个字节应转换为十六进制字符串。这是我应该发送的结果。

有了这些信息,我实现了以下方法:

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...?我做错了什么吗?

I should implement a MAC-CBC generation method in C# with some information about the cryptography algorithm. Here's what I have:

  • I should use DES.
  • The key is byte[] {11, 11, 11, 11, 11, 11, 11, 11}
  • The data (16 bytes) should be encrypted in 8-byte parts. First 8 bytes is encrypted using Instance Vector = new byte[8] (8 bytes with 0 value). (CBC?)
  • that last 8 bytes of the encrypted value should be converted to Hex string. this is the result I should send.

With this information, I have implemented the following method:

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

The sample raw data they have provided me is: input=byte[] {0, 6, 4, 1, 6, 4, 1, 7, E, E, F, F, F, F, B, B) which should return the output of value: A7CBFB3C730B059C. This means the last eight bytes of encrypted data should be: byte[] {167, 203, 251, 60, 115, 11, 05, 156}.

But unfortunately using the above method, I get: 32D91200D0007632. meaning my encrypted data is not correct. (the last eight byte of my method's generated encrypted value is byte[] {50, 207, 18, 0, 208, 0, 118, 50}).

Is there any way that I can find out what I should do to get to A7CB...? Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-12-14 21:08:43

CBC-MAC 需要零初始化向量。最好明确指定 IV:

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

看看是否效果更好。

CBC-MAC requires a zero Initialisation Vector. Much better to specify the IV explicitly:

var IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };  

You say your key is byte[] { 11, 11, 11, 11, 11, 11, 11, 11 } are those bytes in hex or in base 10? You might want to try:

var key = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };

and see if that works better.

白衬杉格子梦 2024-12-14 21:08:43

Mono 项目有一个通用的 MAC-CBC 实现,应该适用于任何 SymmetricAlgorithm - 即使它在内部仅用于实现MACTripleDES

您可以找到 MIT.X11 许可的源代码 这里。按原样使用它或将其与您自己的代码进行比较。

The Mono project has a generic MAC-CBC implementation that should work on any SymmetricAlgorithm - even if it's used, internally, only to implement MACTripleDES.

You can find the MIT.X11 licensed source code here. Use it as-is or compare it to your own code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文