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
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?
my C# code looks like
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
Outputs;
PHP: 202cb962ac59075b964b07152d234b70
C# : 62b92c2059ac5b07964b07152d234b70
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题在这里:
我不确定为什么你要将编码字节加载到 Guid 中,但这不是将字节转换回字符串的正确方法。使用 BitConverter 代替:
Your problem is here:
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:Juliet 的解决方案没有给我提供与我比较的 PHP 哈希(由 Magento 1.x 生成)相同的结果,但是基于 github 上的此实现:
The solution from Juliet didn't give me the same result as a PHP hash I was comparing against (produced by Magento 1.x), however the following did, based on this implementation on github: