我已经尝试使用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
朱丽叶的解决方案没有给我与我正在比较的PHP哈希(由Magento 1.x生成)相同的结果,但是以下是基于this implementation on github:
using (var md5 = MD5.Create())
{
result = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)))
.Replace("-", string.Empty).ToLower();
}