简体   繁体   English

PHP和C#中的不同MD5哈希

[英]Different MD5 hashes in PHP and C#

Why hashes become different if i change input string? 如果更改输入字符串,为什么哈希变得不同?

My C# code is: 我的C#代码是:

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. 在PHP中,我使用md5()函数。 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 . 哈希值相同: 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 . 现在,哈希值有所不同: 3db825e09eae0a83db535fda7f5ee5b5ee1ae334e4bdeceb54caab15555f2f40

Why this happening? 为什么会这样呢?

The hash value ee1ae334e4bdeceb54caab15555f2f40 is the MD5 hash over test-server::7250406f7c43524545f794ff50dfd15b::name=Инесса . 哈希值ee1ae334e4bdeceb54caab15555f2f40test-server::7250406f7c43524545f794ff50dfd15b::name=Инесса的MD5哈希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 . 此输入是test-server::7250406f7c43524545f794ff50dfd15b::name=%D0%98%D0%BD%D0%B5%D1%81%D1%81%D0%B0的URL解码版本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. 您的C#代码片段在未解码的版本上执行MD5。 PHP decodes GET s by default so to get the same result you will need to double encode the value being set to the PHP script. PHP默认情况下会解码GET ,因此要获得相同的结果,您将需要对设置给PHP脚本的值进行双重编码。

See here, https://3v4l.org/rK7fi (online PHP code implementation): 参见https://3v4l.org/rK7fi (在线PHP代码实现):

The GET variables are passed through urldecode(). GET变量通过urldecode()传递。

http://php.net/manual/en/reserved.variables.get.php 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. 另外,如果在URL对C#中的值进行URL编码之前,先对MD5值进行处理,则系统应返回相同的哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM