MD5哈希不C#和PHP搭配 [英] MD5 hashing does not match in C# and PHP

查看:109
本文介绍了MD5哈希不C#和PHP搭配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试散列使用MD5和C#一样在PHP中的字符串,但结果是不同的..有人可以解释我如何得到这个匹配?

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?

我的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

PHP: 202cb962ac59075b964b07152d234b70

C#:62b92c2059ac5b07964b07152d234b70

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

这篇关于MD5哈希不C#和PHP搭配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆