Istudent Rana

Istudent Rana

  • 1.4k
  • 364
  • 85.2k

Padding is invalid and cannot be removed while decrypting using AES

Oct 15 2024 1:37 PM

My code used to work before when I used RijndaelManaged. Since its obsolete and suggest us to AES. 

I change the code and now I get the error 'Padding is invalid and cannot be removed'.

Below is my code:

public static string DecryptStringAES(string encryptedText, string key)
{
    var keybytes = Encoding.UTF8.GetBytes(key);
    var iv = keybytes;

    var encryptStringToBytes = EncryptStringToBytes_Aes(encryptedText, keybytes, iv);

    // Decrypt the bytes to a string.  here encryptStringToBytes is byte[80]
    var roundtrip = DecryptStringFromBytes_Aes(encryptStringToBytes, keybytes, iv);

    //DECRYPT FROM CRIPTOJS
    var encrypted = Convert.FromBase64String(roundtrip);

    //here encryptStringToBytes is byte[48]
    //This Line throws the exception --Padding is invalid and cannot be removed
    var id = DecryptStringFromBytes_Aes(encrypted, keybytes, iv);

    return id;
}
private static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
    if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText");
    if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");

    string plaintext = null;

    try
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Mode = CipherMode.CFB;
            aesAlg.Padding = PaddingMode.PKCS7;
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        string err = ex.Message;
        throw;
    }

    return plaintext;
}
	
private static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
    if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText");
    if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");

    byte[] encrypted;

    try
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Mode = CipherMode.CFB;
            aesAlg.Padding = PaddingMode.PKCS7;
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(plainText);
                    }

                    encrypted = msEncrypt.ToArray();
                }
            }
        }
    }
    catch (Exception ex)
    {
        string er = ex.Message;
        throw ex;
    }

    return encrypted;
}

 


Answers (2)