Click here to Skip to main content
11,928,403 members (59,853 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET C#4.0 .NET4
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
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

this is my code for decrption

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));
               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);
           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

[edit]Code block added - OriginalGriff[/edit]
Posted 26-Oct-12 3:07am
Edited 26-Oct-12 3:10am
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100