C# AES-128-GCM标记不匹配

C# AES-128-GCM标记不匹配,c#,aes-gcm,C#,Aes Gcm,我正在尝试用aes-128-gcm加密和解密。 但当我运行测试时,我有一个错误: System.Security.Cryptography.CryptographyException:计算的身份验证标记与输入身份验证标记不匹配 我不明白为什么会出现这个错误,因为当我用encrypt方法打印标签和用decrypt方法打印标签时,它们是一样的?我读到相关数据可能会改变一些东西,但我没有发现什么 这是测试 [TestCase("ABC", "ABC")] public void TestEncrypD

我正在尝试用aes-128-gcm加密和解密。 但当我运行测试时,我有一个错误:

System.Security.Cryptography.CryptographyException:计算的身份验证标记与输入身份验证标记不匹配

我不明白为什么会出现这个错误,因为当我用encrypt方法打印标签和用decrypt方法打印标签时,它们是一样的?我读到相关数据可能会改变一些东西,但我没有发现什么

这是测试

[TestCase("ABC", "ABC")]
public void TestEncrypDecrypt(string message, string expected)
{
     string cle = "FnUoIZvBUzC1Q/rn5WMi7Q==";
     var aes = new AESEncryption(cle);
     var crypted = aes.Encrypt(message);
     Assert.That(aes.Decrypt(crypted), Is.EqualTo(expected));
}
这是我的班级:

public class AESEncryption : IEncryption
    {
        private byte[] KEY { get; set; }
        private byte[] TAG { get; set; }

        public AESEncryption(string key)
        {
            KEY = Convert.FromBase64String(key);
            TAG = new byte[16];
        }

        public string Encrypt(string message)
        {
            byte[] plainText = Encoding.UTF8.GetBytes(message);
            byte[] ciphertext = new byte[plainText.Length];
            using (AesGcm aesGcm = new AesGcm(KEY))
            {
                aesGcm.Encrypt(
                    new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B }, 
                    plainText, 
                    ciphertext, 
                    TAG);
            }
            return Convert.ToBase64String(ciphertext);
        }

        public string Decrypt(string message)
        {
            byte[] cipherText = Encoding.UTF8.GetBytes(message);
            byte[] plainText = new byte[cipherText.Length];
            using (AesGcm aesGcm = new AesGcm(KEY))
            {
                aesGcm.Decrypt(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B }, 
                    cipherText, 
                    TAG, 
                    plainText);
                Console.WriteLine("d1 " + Convert.ToBase64String(TAG));
            }

            return Convert.ToBase64String(plainText);
        }

    }


非常感谢

您刚刚错过了
tobase64字符串
编码。GetBytes
顺序:

    public class AESEncryption
    {
        private byte[] KEY { get; set; }
        private byte[] TAG { get; set; }
        private byte[] NONCE { get; set; }

        public AESEncryption(string key)
        {
            KEY = Convert.FromBase64String(key);
            TAG = new byte[16];
            NONCE = new byte[12] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B };
        }

        public string Encrypt(string message)
        {
            byte[] plainText = Encoding.UTF8.GetBytes(message);
            byte[] ciphertext = new byte[plainText.Length];
            using (AesGcm aesGcm = new AesGcm(KEY))
            {
                aesGcm.Encrypt(
                    NONCE,
                    plainText,
                    ciphertext,
                    TAG);
            }
            Debug.WriteLine("e " + Convert.ToBase64String(TAG));

            return Convert.ToBase64String(ciphertext);
        }

        public string Decrypt(string message)
        {
            Debug.WriteLine("d " + Convert.ToBase64String(TAG));

            // Notice here -> First get byte from the encoded base64. 
            byte[] cipherText = Convert.FromBase64String(message);
            byte[] plainText = new byte[cipherText.Length];
            using (AesGcm aesGcm = new AesGcm(KEY))
            {
                aesGcm.Decrypt(
                    NONCE,
                    cipherText,
                    TAG,
                    plainText);
            }

            // Notice here -> then get back the string from plain text.
            return Encoding.UTF8.GetString(plainText);
        }

    }
那么

        string cle = "FnUoIZvBUzC1Q/rn5WMi7Q==";
        var aes = new AESEncryption(cle);
        var crypted = aes.Encrypt("Hello");
        Debug.WriteLine($"DECRYPT TEST: {aes.Decrypt(crypted)}");
        // Prints "Hello"