i have the following piece of code. It has been working in my Staging and pre-production environments as well as in production.
How it has suddenly stopped working ONLY in the Production environment. It still works in pre-production and production.
It is throwing the "Hash value does not match" error meaning the storedHash != calcHash.
Any ideas why this might be happening in only of the 3 environments?
static public string StrDec(string value, string key)
{
String dataValue = "";
String calcHash = "";
String storedHash = "";
MACTripleDES mac3des = new MACTripleDES();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
mac3des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
try
{
string strRemoveSpace = value.Replace(" ", "+");
dataValue = Encoding.UTF8.GetString(System.Convert.FromBase64String(strRemoveSpace.Split(System.Convert.ToChar("-"))[0]));
storedHash = Encoding.UTF8.GetString(System.Convert.FromBase64String(strRemoveSpace.Split(System.Convert.ToChar("-"))[1]));
calcHash = Encoding.UTF8.GetString(mac3des.ComputeHash(Encoding.UTF8.GetBytes(dataValue)));
if (storedHash != calcHash)
{
//Throw exception because data was corrupted here
throw new ArgumentException("Hash value does not match");
}
}
catch (System.Exception ex)
{
//Catch System.Exception here
}
return dataValue;
}
This is the problem - or at least a problem:
Encoding.UTF8.GetString(mac3des.ComputeHash(Encoding.UTF8.GetBytes(dataValue)));
... and quite possibly the same for the previous lines.
You're calling Encoding.UTF8.GetString
with arbitrary binary data which isn't a UTF-8-encoded string. You mustn't do that - it's like trying to load an arbitrary blob of data as an image file.
If you want to convert some arbitrary binary data to a string, use Convert.ToBase64String
or convert it to hex.