c# md5 和 php md5 不匹配
c# md5 and php md5 not match
这是我的 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)
将首次 return true
,因此不会在字符串的最开头。
将您的代码更改为此,它应该可以工作:
source += "&" + paramValues[i];
这是我的 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)
将首次 return true
,因此不会在字符串的最开头。
将您的代码更改为此,它应该可以工作:
source += "&" + paramValues[i];