c# - AES CMAC计算C#

标签 c# encryption aes cmac message-authentication-code

我知道 MAC 是最后一个 block 加密的第 4 个字节,并找到了这个 CMAC 解释 here但这有点难以理解。也许已经有一些 CMAC AES 问题,但很抱歉我不能很好地理解它。

谁能解释一下如何计算 CMAC?如果需要,还可以使用 C# 中的一些示例代码。谢谢

最佳答案

首先,您需要从 AES key 中派生出两个子 key 。该算法在 RFC4493 中有很好的描述。 ,但我将在此处包含一些代码示例以供引用。 为此,您将需要 AESEncrypt 函数,您可以使用 dotNet 编写该函数 AesCryptoServiceProvider :

    byte[] AESEncrypt(byte[] key, byte[] iv, byte[] data)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;

            using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();

                return ms.ToArray();
            }
        }
    }

还有将数组左移一位的东西:

    byte[] Rol(byte[] b)
    {
        byte[] r = new byte[b.Length];
        byte carry = 0;

        for (int i = b.Length - 1; i >= 0; i--)
        {
            ushort u = (ushort)(b[i] << 1);
            r[i] = (byte)((u & 0xff) + carry);
            carry = (byte)((u & 0xff00) >> 8);
        }

        return r;
    }

现在只保留 RFC4493 中的算法实现。为了更容易理解,我对 RFC 中的逻辑进行了评论。

    byte[] AESCMAC(byte[] key, byte[] data)
    {
        // SubKey generation
        // step 1, AES-128 with key K is applied to an all-zero input block.
        byte[] L = AESEncrypt(key, new byte[16], new byte[16]);

        // step 2, K1 is derived through the following operation:
        byte[] FirstSubkey = Rol(L); //If the most significant bit of L is equal to 0, K1 is the left-shift of L by 1 bit.
        if ((L[0] & 0x80) == 0x80)
            FirstSubkey[15] ^= 0x87; // Otherwise, K1 is the exclusive-OR of const_Rb and the left-shift of L by 1 bit.

        // step 3, K2 is derived through the following operation:
        byte[] SecondSubkey = Rol(FirstSubkey); // If the most significant bit of K1 is equal to 0, K2 is the left-shift of K1 by 1 bit.
        if ((FirstSubkey[0] & 0x80) == 0x80)
            SecondSubkey[15] ^= 0x87; // Otherwise, K2 is the exclusive-OR of const_Rb and the left-shift of K1 by 1 bit.

        // MAC computing
        if (((data.Length != 0) && (data.Length % 16 == 0)) == true)
        {
            // If the size of the input message block is equal to a positive multiple of the block size (namely, 128 bits),
            // the last block shall be exclusive-OR'ed with K1 before processing
            for (int j = 0; j < FirstSubkey.Length; j++)
                data[data.Length - 16 + j] ^= FirstSubkey[j];
        }
        else
        {
            // Otherwise, the last block shall be padded with 10^i
            byte[] padding = new byte[16 - data.Length % 16];
            padding[0] = 0x80;

            data = data.Concat<byte>(padding.AsEnumerable()).ToArray();

            // and exclusive-OR'ed with K2
            for (int j = 0; j < SecondSubkey.Length; j++)
                data[data.Length - 16 + j] ^= SecondSubkey[j];
        }

        // The result of the previous process will be the input of the last encryption.
        byte[] encResult = AESEncrypt(key, new byte[16], data);

        byte[] HashValue = new byte[16];
        Array.Copy(encResult, encResult.Length - HashValue.Length, HashValue, 0, HashValue.Length);

        return HashValue;
    }

祝你好运!

关于c# - AES CMAC计算C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29163493/

相关文章:

C# - 处理重定向输出 - 控制台不同于 CMD 窗口

java - 按加密字段对数据库表进行排序

android - 加密 (AES)

c# - 如何复制此在线工具使用的 AES 加密

c# - 适用于 UWP 的 Google OAuth v2

c# - ASP.NET属性路由和默认路由可以一起工作吗?

php - 如何在 NODE.JS 上模仿 php crypt()

c# - 是否有将 AES 256 加密实现为流的 .NET 加密库?

c# - 如何使用自定义图标在 C# 中创建自定义文件类型?

twitter-bootstrap - Visual Studio 以加密方式发布 Bootstrap.Js