简体   繁体   English

将C#代码转换为php编码问题

[英]Converting C# code to php encoding issue

I try to replicate this C# code in php to get the same output(I cannot change c# code only php).And here I'm stuck 我尝试在php中复制此C# 代码以获取相同的输出(我无法仅更改php的c#代码)。

 public static string HashData(string textToBeEncripted)
         {
        //Convert the string to a byte array
        Byte[] byteDataToHash =              System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);

        //Compute the MD5 hash algorithm
        Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);

        return System.Text.Encoding.Unicode.GetString(byteHashValue);
    }

The php code that I have made till now looks like this 到目前为止,我制作的php代码看起来像这样

$a = "test";
$a = mb_convert_encoding($a, "UTF-16LE");

$a = md5($a,true);

$a = unpack('C*', $a);
var_dump($a);
//with the output
array(16) { [1]=> int(200) [2]=> int(5) [3]=> int(158) [4]=> int(46) [5]=> int(199) [6]=> int(65) [7]=> int(159) [8]=> int(89) [9]=> int(14) [10]=> int(121) [11]=> int(215) [12]=> int(241) [13]=> int(183) [14]=> int(116) [15]=> int(191) [16]=> int(230) }

As you can see the output is the same as in the C# code But I'm stuck at the function System.Text.Encoding.Unicode.GetString().How to replicate this in php?Or there is a easier way to get the same output?(I cannot change the C# code sorry) 如您所见,输出与C# 代码相同,但是我被函数System.Text.Encoding.Unicode.GetString()所困扰。如何在php中复制它?或者有一种更简单的方法来获取输出相同吗?(抱歉,我无法更改C#代码)
Edit: Based on Vasiliy Zverev answers since the php hash is different a little bit.I end up making aproximating the hash value of php with the C# hash 编辑:基于Vasiliy Zverev的答案,因为php哈希值略有不同。我最终使用C#哈希值来近似化php的哈希值

function validare_parola($parola,$dbHash){

$parola = mb_convert_encoding($parola, "UTF-16LE");
$parola = md5($parola, true);
$parola = mb_convert_encoding($parola, "UCS-2BE", "UCS-2LE");
$parola = bin2hex($parola);

$procent;
similar_text($dbHash,$parola,$procent);

if($procent>=90){
    return true;
}else{
    return false;
}

}

$parola = "testa";

$dbHash = "10095018710be2bcbbf9bba3f9d91ce8";
if(validare_parola($parola,$dbHash)){
echo 'PASSWORD CORRECT.You can log in.';
}else{
echo 'INCORRECT PASSWORD.Try again.';
}

As a side note don't use md5 for passwords use php password hashing api 附带说明,请勿使用md5作为密码,请使用php密码哈希API

Edit2: I ended up using Vasiliy Zverev solution. Edit2:我最终使用了Vasiliy Zverev解决方案。
Edit3: For the value "111111" there is different output in php... Edit3:对于值“ 111111”,php中有不同的输出...
Edit4: Vasily Zverev updated his solution and now is working as expected Edit4: Vasily Zverev更新了他的解决方案,现在按预期工作

Removed this variant because it was wrong. 删除了此变体,因为它是错误的。 See correct code above. 请参阅上面的正确代码。

The solution, updated : 解决方案, 已更新

$a = "SF0D9G9SGGF0gdsfg976590";
$a = mb_convert_encoding($a, "UTF-16LE");
$a = md5($a, true);
$res = '';
for($i=0; $i<16; $i+=2) {
    // System.Text.Encoding.Unicode.GetString(byteHashValue) replaces invalid characters to 0xfffd ('я')
    // while PHP to 0x003d ('?') or empty string. So replace them like C# does
    $a2 = mb_convert_encoding($a[$i].$a[$i+1], "UTF-16LE","UTF-16LE"); // check if this is invalid UTF-16 character
    if(strlen($a2)==0 || $a[$i]!=$a2[0]) {
        // replace invalid UTF-16 character with C# like
        $v = 0xfffd;
    }
    else {
        // prepare a word (UTF-16 character)
        $v = ord($a[$i+1])<<8 | ord($a[$i]);
    }
    // print each word without leading zeros as C# code does
    $res .= sprintf("%x",$v);
}
echo($res);

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

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