2016-12-21 81 views
0

這裏匹配是.NETPHP加密不是用asp.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加密輸出不匹配 都不同

回答

0

要生成吃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

+0

感謝您的回覆,但裏克.NET開發人員不能改變他們的代碼。有可能我們可以將上面的.net代碼複製到php中? –

+0

你可以給我使用你的.net代碼的字符串abcdefghijklmnopqrstuvwxyz md5散列 –