dongwu8050 2018-05-29 10:50
浏览 121
已采纳

PHP和C#中的不同MD5哈希值

Why hashes become different if i change input string?

My C# code is:

public static string CreateMD5(string strInput)
    {
        using (MD5 md5 = MD5.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(strInput);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            string hashedString = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
            return hashedString;
        }
    }

In PHP I use the md5() function. It is an online service so I have no source code; I just use that website to match results.

If I have this string:

test-server::7250406f7c43524545f794ff50dfd15b

Hashes are the same: 20202421c846813960404af7dd36c146. But if I extend the string to this (with encoded characters):

test-server::7250406f7c43524545f794ff50dfd15b::name=%D0%98%D0%BD%D0%B5%D1%81%D1%81%D0%B0

Now hashes are different: 3db825e09eae0a83db535fda7f5ee5b5 and ee1ae334e4bdeceb54caab15555f2f40.

Why this happening?

  • 写回答

1条回答 默认 最新

  • doushou3814 2018-05-29 11:09
    关注

    The hash value ee1ae334e4bdeceb54caab15555f2f40 is the MD5 hash over test-server::7250406f7c43524545f794ff50dfd15b::name=Инесса. This input is the URL-decoded version of test-server::7250406f7c43524545f794ff50dfd15b::name=%D0%98%D0%BD%D0%B5%D1%81%D1%81%D0%B0.

    Your C# code fragment performs MD5 on the non-decoded version. PHP decodes GETs by default so to get the same result you will need to double encode the value being set to the PHP script.

    See here, https://3v4l.org/rK7fi (online PHP code implementation):

    The GET variables are passed through urldecode().

    http://php.net/manual/en/reserved.variables.get.php

    Alternatively if you MD5 the value before you URL encode the values in the C# the systems should return the same hash.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退