简体   繁体   中英

“Padding is invalid and cannot be removed”

I am trying this example of decryption and encryption: http://msdn.microsoft.com/en-us/library/ms229740.aspx

When I only use the Decrypt method on a previously encrypted file, I am having an error: padding is invalid and cannot be removed

I tried it like this:

Decrypt(xmlDoc, key);
Console.WriteLine("The element was decrypted");
Console.WriteLine(xmlDoc.InnerXml);

It seems that Decrypt is not working without Encypt first.

Some en/decryption algorithms will take whatever you feed them and give you gibberish for an invalid input, but other algorithms simply won't work. This is such a case.

For decryption to work properly, the input has to be in the correct format expected by the decryption algorithm. Encryption often produces blocks of encrypted data of a precise size and the last block can be padded if it isn't complete (see Block cipher ).

The decryption algorithm fails in your code because the padding it is expecting is invalid since it wasn't encrypted in the first place.

EDIT:

If the decryption fails when decrypting an already encrypted file, compare the result of applying Encrypt to the original file with the already encrypted XML file. There is probably a leading/trailing whitespace or newline character that breaks the algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM