Padding is invalid and cannot be removed

In this post I’m looking at the Padding is invalid and cannot be removed error

Background on Padding is invalid and cannot be removed

Today I tried to send data encrypted between an application built in c# and an application built in Silverlight. When I encrypted the data in one application the decryption would fail with the following exception:

An exception of type ‘System.Security.Cryptography.CryptographicException’ occurred in mscorlib.dll but was not handled in user code Additional information: Padding is invalid and cannot be removed

Your Solution

To encrypt my plain text I used the following code:

public static string EncryptStringAES(string plainText, string sharedSecret) {

  string outStr = null;
  AesManaged aesAlg = null;
  try
  {
   Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt); 
   aesAlg = new AesManaged(); 
   aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); 
   aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); 
   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);
         }
      } 
      outStr = Convert.ToBase64String(msEncrypt.ToArray());
   }
  } 
  finally {
    if (aesAlg != null) aesAlg.Clear();
  }
  return outStr;
}

for the decryption I used this bit of code:

public static string DecryptStringAES(string cipherText, string sharedSecret)

{
AesManaged aesAlg = null; string plaintext = null; try {
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
aesAlg = new AesManaged();
aesAlg.BlockSize = aesAlg.LegalBlockSizes[0].MaxSize;
aesAlg.KeySize = aesAlg.LegalKeySizes[0].MaxSize;
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
}
}
finally
{
if (aesAlg != null) aesAlg.Clear();
}
  return plaintext;
}

only after setting the block size and the key size the error went away.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Avatar of Pieter Veenstra

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There's a better way — and I can show you exactly what it looks like. I'm the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I've been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@sharepains.com

Related Posts

2 thoughts on “Padding is invalid and cannot be removed

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading