0

这是.net的代码

public string GetMD5Hash(string name) {
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] ba = md5.ComputeHash(Encoding.UTF8.GetBytes(name));
    StringBuilder hex = new StringBuilder(ba.Length * 2);

    foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
    return Convert.ToString(hex);
}

在php中我使用下面的代码

class foobar {

    public function str2hex($string) {
        $hex = "";
        for ($i = 0; $i < strlen($string); $i++)
            $hex .= (strlen(dechex(ord($string[$i]))) < 2) ? "0" . dechex(ord($string[$i])) : dechex(ord($string[$i]));       
        return $hex;
    }

    public function GetMD5($pStr) { 
        $data = mb_convert_encoding($pStr, 'UTF-16LE', 'UTF-8');
        $h = $this->str2hex(md5($data, true)); 
        return $h;
    } 
}

$foobar = new foobar;  
$nisha =$foobar->GetMD5('5698882');
echo "</br>";
echo $nisha;

但输出与.net 加密输出不匹配两者都不同

4

1 回答 1

1

md5在 php 中生成字符串的哈希,您只需要使用该md5()函数的更多详细信息,网址为http://php.net/manual/en/function.md5.php

您可以使用这些代码获取 md5 哈希,这对于 .net 和 php 都是相同的

PHP md5 哈希

<?php
echo md5('abcdefghijklmnopqrstuvwxyz');
?>

结果 - > c3fcd3d76192e4007dfb496cca67e13b

.NET md5 哈希

public string CalculateMD5Hash(string input)

{

    // step 1, calculate MD5 hash from input

    MD5 md5 = System.Security.Cryptography.MD5.Create();

    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);

    byte[] hash = md5.ComputeHash(inputBytes);


    // step 2, convert byte array to hex string

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < hash.Length; i++)

    {

        sb.Append(hash[i].ToString(“x2”));

    }

    return sb.ToString();

}

结果 - > c3fcd3d76192e4007dfb496cca67e13b

于 2016-12-21T07:55:08.697 回答