Last post Sep 06, 2018 02:09 PM by Stoian Bucovich
Sep 06, 2018 01:23 PM|Stoian Bucovich|LINK
I need some help with this method which is rising the error while trying to decrypt.
public const string key = "secretkey";
public static string TripleDESDecrypt(string cipherString)
byte toDecryptArray = Convert.FromBase64String(cipherString);
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(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(toDecryptArray, 0, toDecryptArray.Length);
Any help? Thanks
Sep 06, 2018 01:41 PM|mgebhard|LINK
Generally the error tells which line threw the exception.
Since this an ASP.NET support forum, I assume the cipherStirng is passed over HTTP and perhaps equal sign (=), base64 place holders, are encoded.
Try using the Visual Studio debugger to view the cipherStirng value.
Sep 06, 2018 01:50 PM|Stoian Bucovich|LINK
Basically I am calling the method and passing the argument (the cipher string to be decrypted) from database they are not passed over HTTP.
The error is thrown at byte resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length); just before tdes.Clear();
if its of any help here is the encryption method as well.
public static string TripleDESEncrypt(string toEncrypt)
byte toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
Which is strange because some cipher string are decrypted and some are not and I am not sure why.
Sep 06, 2018 02:09 PM|Stoian Bucovich|LINK
OK, I just found whats going on. As I mentioned I am feeding the method from database. So I guess I missed my morning cup of coffee during database design because I've set the field data type nvarchar(8) which is way to short to hold the base64 encoded cipher
string, which in turn has been truncated during insert, and making it invalid cipher string.
I guess this resolved the issue. Thank you anyways for your time and effort.