portella.com.br

Blog pessoal de Felipe Portella

CryptographicException due to PaddingMode of TripleDESCryptoServiceProvider

without comments

Today I faced a “bug” in my decryption algorith due to the PaddingMode used by the TripleDESCryptoServiceProvider. As I was using the PaddingMode.PKCS7, when I try to decrypt using a wrong key (not the same key used to encrypt) I receive a System.Security.Cryptography.CryptographicException with the message “Bad Data”

This is not the behaviour that I wanted for my application. I want to decrypt and give as a result some invalid data … but decrypt …

If I change the PaddingMode in the correct cases (right keys) with input data that is not Base64, I´ll receive decrypted string that has spaces at the end … which is not also a desireble behaviour for me.

My solution was to use always PaddingMode.PKCS7, try to decrypt, catch the exception (when occurs) and decrypt again now without Padding.

If anyone has a better approach I will be happy to hear …

Written by Felipe Portella

janeiro 26th, 2011 at 2:07 pm

Leave a Reply

You must be logged in to post a comment.