Padding is invalid and cannot be removed

Topics: Cryptography Application Block
Jan 12, 2007 at 6:18 PM
I have some pretty basic encrypt/decrypt routines that work great on my local/development box. When I copied the code to another server, I get the above error message. I installed the January 2006 enterprise library on the other server, ran the configuration, created a new key with the same KEY and made it for the MACHINE. I thought that would fix it. But it didn't. Can somebody please help me get past this error? I hope it's a simple fix.

Thanks a lot,
Jan 13, 2007 at 1:01 PM
From what I've been reading, there is more work than just:

txtEncryptedPassword.Text = Cryptographer.EncryptSymmetric("RijndaelManaged", txtPassword.Text);


txtDecryptedPassword.Text = Cryptographer.DecryptSymmetric("RijndaelManaged", txtEncryptedPassword.Text);

but I get exactly what I need. It wasn't until I copied the dev code to another server when I run into the above error.

Thanks again!
Jan 13, 2007 at 2:57 PM
More info which I hope makes this clearer.

If I take a string, say TEST and I encrypt on computer A. I get a 44 character string which is encrypted. I can decrpyt it all I want and I end up with TEST. Just like I would expect.

Now, I put the same code on computer B and ran the test. I encrypted TEST, got a 44 character string, and decrypted it back to TEST.

The part I don't understand is this. I take the encrypted 44 character code from computer A and copy it to the application on computer B. Both use the same "password" in the key file; which were both generated on each computer at the LocalMachine level. When I try to encrypt the text from Computer A on Computer B, I get the padding error.

The reason this is an issue is that I'm encrypting database contents. If I initialize them on the computer A, computer B can't decrypt because of the padding.

Maybe I just don't understand how the encrypt/decrypt works. But if this makes sense to anybody, can you please help?

Mar 1, 2007 at 1:27 AM
Running into the same problem
wondering if anyone knows the answer.
Most likely i'm misunderstanding how encryption should work.
Mar 6, 2007 at 9:01 PM
Me too:
Running into the same problem :'(

Some tip?
Mar 6, 2007 at 11:45 PM

the way i got this working was by manually doing the conversions (so I could enforce the base64 conversion)

to encrypt:

byte[] encryptedArray = Convert.FromBase64String(temp(int)k.KeyId);
byte[] unencryptedArray = Cryptographer.DecryptSymmetric("RijndaelManaged", encryptedArray);
string unencryptedText = Encoding.Unicode.GetString(unencryptedArray);

to decrypt:
byte[] encryptedArray = Convert.FromBase64String(dataReader"key_value".ToString());
byte[] unencryptedArray = Cryptographer.DecryptSymmetric("RijndaelManaged", encryptedArray);
string unencryptedText = Encoding.Unicode.GetString(unencryptedArray);
Sep 15, 2012 at 1:07 AM
Edited Sep 15, 2012 at 1:07 AM


I know this is an old post. However I have the same error "Padding is invalid and cannot be removed." in my Cryptography block. 

what are the values

byte[] encryptedArray = Convert.FromBase64String(temp(int)k.KeyId);  --- what is k.KeyId ?

byte[] encryptedArray = Convert.FromBase64String(dataReader"key_value".ToString());  -- what is key_value?


Please let me know how to resolve this issue?


Sep 15, 2012 at 1:21 AM

Take a look at this FAQ:,%20%22Padding%20is%20invalid%20and%20cannot%20be%20removed%22

Randy Levy
Enterprise Library support engineer