views:

56

answers:

1

Hi All,
Just wanna confirm my thoughts about CryptographicException on decryption. I have some tests, exactly the one with wrong passphraze. This test throws

System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.

I was looking what would cause this. However I could not find anything. It leads me to conclusion that once I use wrong passphraze for decryption I got this error.

Am I right?

Thanks for any comments. Cheers, X.

+2  A: 

When using padding and the key and IV aren't correct the decryption will fail at the point when padding is to be removed. This is because in all likelyhood the padded bytes doesn't conform to the selected PaddingMode. There is a slight chance that it does and then the decryption would still go through without error but the result would be garbled.

However, if using PaddingMode.None then you will still be able decrypt the data without errors (provided the length of the plaintext is a multiple of the block size) but the data would again be garbled.

To conclude: you cannot rely on the fact that if the wrong key is used then you'd get an exception. You'd have to check the resulting data.

Sani Huttunen
thansk for explanation. I think I can rely on fact that once I catch this error during decrypting it will be morelikely because wrong passphraze was used.So I can leave it without worried that st is set or used wrong in implementation.
Xabatcha