【问题标题】:AES-128-GCM Tag doest not matchAES-128-GCM 标签不匹配
【发布时间】:2020-02-19 17:45:47
【问题描述】:

我正在尝试使用 aes-128-gcm 进行加密和解密。 但是当我运行测试时出现错误:

System.Security.Cryptography.CryptographicException : 计算出的身份验证标签与输入身份验证标签不匹配。

我不明白为什么会出现这个错误,因为当我在加密方法中打印标签并在解密方法中打印它时,它们是一样的?我已经读到相关数据可能会改变一些东西,但我没有找到任何东西。

这是测试

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

    }

非常感谢!

【问题讨论】:

    标签: c# aes-gcm


    【解决方案1】:

    您刚刚错过了ToBase64StringEncoding.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"
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 2013-07-17
      • 2016-12-19
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多