Тег AES-128-GCM не соответствует - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь зашифровать и расшифровать с помощью 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);
        }

    }

Большое спасибо!

1 Ответ

0 голосов
/ 31 марта 2020

Вы только что пропустили заказ ToBase64String и Encoding.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"
...