MD5散列在C#和PHP中不匹配
我已经尝试使用MD5在PHP中散列字符串和在C#中使用相同的字符串,但结果是不同的…有人可以解释我如何使其匹配吗?
我的C#代码看起来像
md5 = new MD5CryptoServiceProvider(); originalBytes = ASCIIEncoding.Default.GetBytes(AuthCode); encodedBytes = md5.ComputeHash(originalBytes); Guid r = new Guid(encodedBytes); string hashString = r.ToString("N");
提前致谢
编辑:我的字符串是123字符串
输出;
PHP:202cb962ac59075b964b07152d234b70
C#:62b92c2059ac5b07964b07152d234b70
你的问题在这里:
Guid r = new Guid(encodedBytes); string hashString = r.ToString("N");
我不确定你为什么要将编码的字节加载到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