Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
When I am trying to dercypt my encrypted text I am getting the following error
"Bad Data:Cryptographic exception Was unhandled"

I am getting the error at following line of code
C#
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


this is my code for decrption

C#
public string decrypt(string code, string Key)
       {
           bool useHashing = true;
           byte[] keyArray;
           byte[] toEncryptArray = Convert.FromBase64String(code);

           //System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
           //Get your key from config file to open the lock!
           //string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
           string key = Key;
           if (useHashing)
           {
               MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
               keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
               hashmd5.Clear();
           }
           else
               keyArray = UTF8Encoding.UTF8.GetBytes(key);

           TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
           tdes.Key = keyArray;
           tdes.Mode = CipherMode.ECB;
           tdes.Padding = PaddingMode.PKCS7;

           ICryptoTransform cTransform = tdes.CreateDecryptor();
           byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

           tdes.Clear();
           return UTF8Encoding.UTF8.GetString(resultArray);

       }

I am not getting what is the probles as this code was working properly before.plz help. thanks in advance
swati

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 26-Oct-12 2:10am
v2
Comments
OriginalGriff 26-Oct-12 8:08am    
"this code was working properly before."
Before what?
I.e. What has changed since it last worked. This is probably relevant to your question...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900