03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 27 ❘ 731<br />

11. The EncryptWithPassword example program does this. Download it to see how it works.<br />

If you try to decrypt a message with the wrong password, even a password that is wrong by<br />

a single character, the decryption operation throws an exception.<br />

A Bad Pad<br />

In my tests, the decryption operation throws a ListDictionaryInternalException<br />

with the message, “Padding is invalid and cannot be removed.” The AesManaged class<br />

uses a block cipher, which means it breaks the message up into blocks and encrypts<br />

them separately. It uses a padding scheme to make the blocks the length it requires.<br />

This exception basically means the object tried to decrypt the encrypted bytes but the<br />

result didn’t have the required padding.<br />

To make a long story short, the decryption failed.<br />

If you set the AesManaged object’s Padding property to PaddingMode.None during<br />

decryption, the decryption will not worry about padding and will return a decrypted<br />

result. However, if the password is even a little wrong, the result is complete gibberish.<br />

12. The RSAReceiver and RSASender example programs do this. They’re relatively straightforward,<br />

so download them to see how they work.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!