I have tried hashing a string in PHP using MD5 and the same in C#, but the results are different.. can someone explain me how to get this matched?
我已经尝试使用MD5在PHP中散列字符串和在C#中使用相同的字符串,但结果是不同的...有人可以解释我如何使其匹配吗?
my C# code looks like
我的C#代码看起来像
md5 = new MD5CryptoServiceProvider();
originalBytes = ASCIIEncoding.Default.GetBytes(AuthCode);
encodedBytes = md5.ComputeHash(originalBytes);
Guid r = new Guid(encodedBytes);
string hashString = r.ToString("N");
Thanks in advance
提前致谢
Edited: My string is 123 as a string
编辑:我的字符串是123字符串
Outputs;
输出;
PHP: 202cb962ac59075b964b07152d234b70
PHP:202cb962ac59075b964b07152d234b70
C# : 62b92c2059ac5b07964b07152d234b70
C#:62b92c2059ac5b07964b07152d234b70
1 个解决方案
#1
32
Your problem is here:
你的问题在这里:
Guid r = new Guid(encodedBytes);
string hashString = r.ToString("N");
I'm not sure why you're loading your encoded bytes into a Guid, but that is not the correct way to convert bytes back to a string. Use BitConverter
instead:
我不确定你为什么要将编码的字节加载到Guid中,但这不是将字节转换回字符串的正确方法。改为使用BitConverter:
string testString = "123";
byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(testString);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
// hashString == 202cb962ac59075b964b07152d234b70
#1
32
Your problem is here:
你的问题在这里:
Guid r = new Guid(encodedBytes);
string hashString = r.ToString("N");
I'm not sure why you're loading your encoded bytes into a Guid, but that is not the correct way to convert bytes back to a string. Use BitConverter
instead:
我不确定你为什么要将编码的字节加载到Guid中,但这不是将字节转换回字符串的正确方法。改为使用BitConverter:
string testString = "123";
byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(testString);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
// hashString == 202cb962ac59075b964b07152d234b70