c#md5和php md5不匹配 [英] c# md5 and php md5 not match

查看:292
本文介绍了c#md5和php md5不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的c#计算签名代码

using System;
using System.Text;

public class Test
{
    public static void Main()
    {
    string str;
    string syskey = "123456789";
    str = GenSignature(syskey,"foo","20170426001757");
    Console.WriteLine(str);
}

public static string GenSignature(string syskey,params string[] paramValues)
    {
        string source = "";
        for (int i = 0; i < paramValues.Length; i++)
        {
            if (!string.IsNullOrEmpty(paramValues[i]))
            {
                source += (string.IsNullOrEmpty(source) ? "" : "&") + paramValues[i];
            }
        }

        if (string.IsNullOrEmpty(source))
        {
            return "";
        }

        source +=  "&" + syskey;
        // Debug.WriteLine(source);
        using (System.Security.Cryptography.MD5 m = System.Security.Cryptography.MD5.Create())
        {
            byte[] hashData = m.ComputeHash(UTF8Encoding.UTF8.GetBytes(source));
            return BitConverter.ToString(hashData).Replace("-", "").ToUpper();
        }
    }
}

和php获取签名代码如下:

function getSignature() {
    $syskey = "123456789";
    $sysCode =  "foo";
    $timeStamp = "20170426001757";
    $str = "&".$sysCode."&".$timeStamp."&".$syskey;
    return strtoupper(md5($str));
}
echo getSignature();

我对C#不太了解,但是得到的结果却完全不同,是否有人知道这两种语言并告诉我一些东西,非常感谢.

解决方案

在PHP示例中,每个部分都用一个&符号(&)开头,但是在C#版本中,除了第一个部分之外,每个部分都附加了前缀:

source += (string.IsNullOrEmpty(source) ? "" : "&") + paramValues[i];

自从最初将source设置为""以来,

string.IsNullOrEmpty(source)将首次返回true,因此在字符串的开头不会添加与号.

将您的代码更改为此,它应该可以工作:

source += "&" + paramValues[i];

在线测试: https://dotnetfiddle.net/KhoFtL

here is my c# compute signature code

using System;
using System.Text;

public class Test
{
    public static void Main()
    {
    string str;
    string syskey = "123456789";
    str = GenSignature(syskey,"foo","20170426001757");
    Console.WriteLine(str);
}

public static string GenSignature(string syskey,params string[] paramValues)
    {
        string source = "";
        for (int i = 0; i < paramValues.Length; i++)
        {
            if (!string.IsNullOrEmpty(paramValues[i]))
            {
                source += (string.IsNullOrEmpty(source) ? "" : "&") + paramValues[i];
            }
        }

        if (string.IsNullOrEmpty(source))
        {
            return "";
        }

        source +=  "&" + syskey;
        // Debug.WriteLine(source);
        using (System.Security.Cryptography.MD5 m = System.Security.Cryptography.MD5.Create())
        {
            byte[] hashData = m.ComputeHash(UTF8Encoding.UTF8.GetBytes(source));
            return BitConverter.ToString(hashData).Replace("-", "").ToUpper();
        }
    }
}

and the php get signature code is below:

function getSignature() {
    $syskey = "123456789";
    $sysCode =  "foo";
    $timeStamp = "20170426001757";
    $str = "&".$sysCode."&".$timeStamp."&".$syskey;
    return strtoupper(md5($str));
}
echo getSignature();

I do not know c# very well, but the result I get are totally different, Did any one know the two languages and tell me something, Thanks a lot.

解决方案

In the PHP example you prepend every part with an ampersand (&), however in the C# version you prepend every part but the first:

source += (string.IsNullOrEmpty(source) ? "" : "&") + paramValues[i];

string.IsNullOrEmpty(source) will return true the first time since you initially set source to "", thus an ampersand won't get added in the very beginning of the string.

Change your code to this and it should work:

source += "&" + paramValues[i];

Online test: https://dotnetfiddle.net/KhoFtL

这篇关于c#md5和php md5不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆