【问题标题】:How to produce a different md5 hash with different encodings?如何生成具有不同编码的不同 md5 哈希?
【发布时间】:2014-09-23 19:27:30
【问题描述】:

我正在将一些代码从 .net (vb) 移植到 php,我遇到了一些我无法在 php 中重现的 md5 散列。在 .net 中,有两个函数,一个使用 UTF-8 编码,另一个使用 Unicode 编码。在字符串中传递时输出是不同的哈希

// First function (returns GUID)
Dim oHasher As Cryptography.MD5 = Cryptography.MD5.Create()
Dim oEncoder As New System.Text.UTF8Encoding()
Dim csData() As Byte

csData = oHasher.ComputeHash(oEncoder.GetBytes(strInput))


// Second function (returns GUID)
Dim oHasher As Cryptography.MD5 = Cryptography.MD5.Create()
Dim oEncoder As New System.Text.UnicodeEncoding()
Dim csData() As Byte

csData = oHasher.ComputeHash(oEncoder.GetBytes(strInput))

在php中我尝试过使用

$passHash = md5(utf8_encode($password));
$newPassHash =  md5($password);

但它们都产生相同的结果。是否可以在 php 中使用 md5 散列产生相同的结果?

【问题讨论】:

    标签: php vb.net


    【解决方案1】:

    Unicode 可以通过不同的字符编码来实现。最常用的编码是UTF-8,但不知System.Text.UnicodeEncoding()默认使用的是什么。

    尝试使用 UTF-16,因为在这种情况下,它是 UTF-8 唯一真正可能的替代方案。

    您可以使用mb_convert_encoding在不同的字符编码之间移动,它支持UTF-16。

    【讨论】:

    • UTF-16 在 Windows 上是一个安全的选择。
    • 我会试一试。谢谢!
    • 我不断得到 utf-8 和 utf-16 的不同结果。我正在使用以下 $passHash = md5(utf8_encode($password)); $newPassHash = md5(mb_convert_encoding($password, "UTF-16"));
    • 这就是我所期望的 - 因为您正在散列不同的字节序列。 utf-16 哈希是否与 Windows UnicodeEncoding() 哈希匹配?
    • 没有。我需要将字符串转换为字节数组,然后对该数组进行散列。知道如何在 php 中做到这一点吗? N一个哈希当前正在工作
    【解决方案2】:

    答案开启:http://technikq.blogspot.de/2013/09/php-utf-16-hashed-md5-string.html

    基本上:

    $string = "e3e3_e37";
    $stringEncoded = mb_convert_encoding($string, 'UTF-16LE', 'UTF-8');
    $encoded_string = md5($string);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 2015-02-25
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      相关资源
      最近更新 更多