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 关于浏览器调试工具断点失效的问题
  • ¥15 pubsub.js无法订阅到消息
  • ¥15 非正弦供电多相感应电机毕业设计
  • ¥15 请用asp语言将得到一组数组转换为图片
  • ¥15 stm32f103c8t6采集HLK-LD2450毫米波雷达传感器数据并打印显示距离和角度
  • ¥15 不重复生成字母+数字交易号
  • ¥15 AD封装导入后层混乱了
  • ¥15 如何将excel中的数据导入ansys中
  • ¥15 MATLAB中图像问题
  • ¥15 rk3399 安卓7.1 应用音频无声音