dongyi5425 2016-08-02 03:25
浏览 233
已采纳

c#md5和php md5不匹配

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.

  • 写回答

1条回答 默认 最新

  • doudi8525 2016-08-02 05:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波