简体   繁体   中英

c# md5 and php md5 not mach

I am trying to use c# md5 and php md5 to calculate the same result but they give different results. The c# code must be the same as the php code.

public string ToMD5(string orderId, string statusCode, string secretKey)
{
   string key = orderId + "+" + statusCode;
   key = Base64Encode(key);
   MD5 m = System.Security.Cryptography.MD5.Create();
   byte[] h = m.ComputeHash(Encoding.Default.GetBytes(key + secretKey));
   string r = "";

   foreach (byte b in h)
   {
       r += b.ToString("X2");
   }

   return r;
}

And here is the php code:

$key = $orderid."+".$status;
$prec = base64_encode($key);
$prec = $prec.$SecretKey;
$prec = md5($prec);
echo $prec;

c# returns : CEC71705E5A25CCD21609B72053539FC

php returns : f1542715b25b302553119fda1e8567bb

Thanks for any help.

These 2 lines produce the same MD5 hash value:

  echo md5("12345+200OKSecret");

and

new List<byte>(MD5.Create().ComputeHash(Encoding.Default.GetBytes("12345+200OKSecret"))).ForEach(x => Console.Write("{0:X2}",x));

Results:

8df9a16122b34e41d49ad11d9f1e0c73

and

8DF9A16122B34E41D49AD11D9F1E0C73

Check the values of the strings you are hashing - they must be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM