Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RijndaelManaged Decryption - How can I remove the padding /0 gracefully?

How can I remove the padding from a decrypted string? I'm using RijndaelManaged provider to encrypt and decrypt. When I decrypt there are several /0/0/0/0/0/0 at the end of the string. My question is how can I gracefully (properly) remove the characters from the result string?

like image 609
Hcabnettek Avatar asked Mar 10 '11 22:03

Hcabnettek


People also ask

How do you resolve padding is invalid and Cannot be removed?

Just set PaddingMode to PaddingMode. Zeros in encryption and decryption.

Is Rijndael obsolete?

The Rijndael and RijndaelManaged types are obsolete. Use Aes instead. Accesses the managed version of the Rijndael algorithm. This class cannot be inherited.

What is decryption in message?

Decryption is the process of converting an encrypted message back to its original (readable) format. The original message is called the plaintext message.

What is Decrypter?

Definition: The conversion of encrypted data into its original form is called Decryption. It is generally a reverse process of encryption. It decodes the encrypted information so that an authorized user can only decrypt the data because decryption requires a secret key or password.


1 Answers

You are most likely not using the correct padding and block modes of the RijndaelManaged instance (called provider in the code below). Since the symmetric encryption providers in .NET are all block ciphers, these settings affect how padding works (as well as how secure the output will be).

The settings below will give you the best security when using RijndaelManaged:

// set the padding algorithm
provider.Padding = PaddingMode.ISO10126; // default is PKCS7
// set the block chaining mode
provider.Mode = CipherMode.CBC;

If you are not the one encrypting the data and you cannot figure out which settings the originating party used then you'll find help in some of the other answers :)

like image 71
Morten Mertner Avatar answered Sep 21 '22 07:09

Morten Mertner