首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c# md5与PHPs md5哈希相同

c# md5与PHPs md5哈希相同
EN

Stack Overflow用户
提问于 2013-06-27 21:12:29
回答 1查看 537关注 0票数 0

我对此有点抓狂了!我正在尝试根据现有的数据库对我的应用程序进行身份验证(所以我不能更改PHP端),我需要将我的密码字段转换为与php的md5(moo)命令相同的密码字段。

然而,我尝试创建散列的每个公式都具有相同的md5,与数据库中的内容非常不同。

有没有一个公式可以产生同样的结果?

我试过了:

代码语言:javascript
运行
复制
 public static string MbelcherEncodePassword(string originalPassword)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            // Conver the original password to bytes; then create the hash
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            // Bytes to string
            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();


        }

和:

代码语言:javascript
运行
复制
public static string MD5(string password)
        {
            byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
                cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash = cryptHandler.ComputeHash(textBytes);
                string ret = "";
                foreach (byte a in hash)
                {
                    if (a < 16)
                        ret += "0" + a.ToString("x");
                    else
                        ret += a.ToString("x");
                }
                return ret;
            }
            catch
            {
                throw;
            }
        }

和:

代码语言:javascript
运行
复制
 public static string MD5Hash(string text)
            {
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), "-", "");
            }

无济于事。任何帮助都将不胜感激!

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-06-27 22:09:56

下面应该会给出与PHP md5相同的十六进制字符串:

代码语言:javascript
运行
复制
public string GetMd5Hex(MD5 crypt, string input)
{
    return crypt.ComputeHash(UTF8Encoding.UTF8.GetBytes(input))
        .Select<byte, string>(a => a.ToString("x2"))
        .Aggregate<string>((a, b) => string.Format("{0}{1}", a, b));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17344151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档