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)
将首次返回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屋!