'AES throwing Exception when decrypting

I am using AES encryption, I have no issues encrypting and writing to DB when I decrypt, it returns null.

The Key and IV are the same while encryption and also checked the padding, they are the same while performing encryption and decryption.

public byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
{
    byte[] password;
    // Create a new AesManaged.    
    using (AesManaged aes = new AesManaged())
    {
        // Create encryptor    
        ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
        // Create MemoryStream    
        using (MemoryStream ms = new MemoryStream())
        {
            // Create crypto stream using the CryptoStream class. This class is the key to encryption    
            // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream    
            // to encrypt    
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                // Create StreamWriter and write data to a stream    
                using (StreamWriter sw = new StreamWriter(cs))
                    sw.Write(plainText);
                password = ms.ToArray();
            }
        }
    }
}

public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
    string plaintext = null;
    // Create AesManaged    
    using (AesManaged aes = new AesManaged())
    {
        // Create a decryptor    
        ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
        // Create the streams used for decryption.    
        using (MemoryStream ms = new MemoryStream(cipherText))
        {
            // Create crypto stream    
            using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
            {
                // Read crypto stream    
                using (StreamReader reader = new StreamReader(cs))
                plaintext = reader.ReadToEnd(); // Error is here , Throws exception "Padding is invalid and cannot be removed."
            }
        }
    }
    return plaintext;
}

Result : Decryption is achieved as expected


Solution 1:[1]

Your code had some syntax errors. Other than that, It's working.

public static void Main(string[] args)
{
    string plainText = "Here is somewe data to encrypt!";

    using (Aes myAes = Aes.Create())
    {
        byte[] cipherText = Encrypt(plainText, myAes.Key, myAes.IV);
        plainText = Decrypt(cipherText, myAes.Key, myAes.IV);

        Console.WriteLine(plainText);
    }
}

public static byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
{
    byte[] encrypted;
    // Create a new AesManaged.    
    using (AesManaged aes = new AesManaged())
    {
        // Create encryptor    
        ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
        // Create MemoryStream    
        using (MemoryStream ms = new MemoryStream())
        {
            // Create crypto stream using the CryptoStream class. This class is the key to encryption    
            // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream    
            // to encrypt    
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                // Create StreamWriter and write data to a stream    
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    //Write all data to the stream.
                    sw.Write(plainText);
                }
                encrypted = ms.ToArray();    
            }                    
        }
    }
    // Return the encrypted bytes from the memory stream.
    return encrypted;
}

public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
    string plaintext = string.Empty;
    // Create AesManaged    
    using (AesManaged aes = new AesManaged())
    {
        // Create a decryptor    
        ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
        // Create the streams used for decryption.    
        using (MemoryStream ms = new MemoryStream(cipherText))
        {
            // Create crypto stream    
            using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
            {
                // Read crypto stream 
                using (StreamReader reader = new StreamReader(cs))
                {
                    plaintext = reader.ReadToEnd();
                }
            }
        }
    }
    return plaintext;
}

Online Demo: https://rextester.com/NMJZ99435

For more info, refer this document: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.7.2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1