Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#代码转换为php编码问题_C#_Php - Fatal编程技术网

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

将C#代码转换为php编码问题,c#,php,C#,Php,我尝试在php中复制这个C#代码以获得相同的输出(我不能只在php中更改C#代码) public static string HashData(string textToBeEncripted) { //Convert the string to a byte array Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncr

我尝试在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);
    }
到目前为止,我编写的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) }
正如您所看到的,输出与C#中的输出相同 但是我被困在函数System.Text.Encoding.Unicode.GetString()上。如何在php中复制它?或者有更简单的方法获得相同的输出?(对不起,我无法更改C代码)
编辑:基于Vasili Zverev的答案,因为php哈希有点不同。我最终用C#哈希来近似php的哈希值

请注意,不要将md5用于密码使用

Edit2:我最终使用了Vasili Zverev溶液。
Edit3:对于值“111111”,php中有不同的输出。。。

Edit4:Vasily Zverev更新了他的解决方案,现在正在按预期工作

删除了此变体,因为它是错误的。请参阅上面的正确代码。

删除了此变体,因为它是错误的。请参阅上面的正确代码。

解决方案已更新:

$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);
$a=“SF0D9G9SGGF0gdsfg976590”;
$a=mb_转换_编码($a,“UTF-16LE”);
$a=md5($a,正确);
$res='';

对于($i=0;$i解决方案,更新了

$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);
$a=“SF0D9G9SGGF0gdsfg976590”;
$a=mb_转换_编码($a,“UTF-16LE”);
$a=md5($a,正确);
$res='';

对于($i=0;$i在这里看一看:在这里看一看:Hi它适用于“test”。但是如果我键入$a=“parolaeste”,我会在php端得到不同的输出。我想我知道了问题所在:
return System.Text.Encoding.Unicode.GetString(byteHashValue);
并非所有二进制字符串都能生成正确的UTF-16字符串。某些位的组合无效。而php bin2hex(md5())不会进行额外的UTF转换。我今天稍后会准备一个修复程序。请在另一个答案中试用我的新版本。如果我键入$a=“111111”我在php中得到不同的输出,plus.LOL中有一个0,这是因为
hex+=String.Format(“{0:x2}”,(uint)System.Convert.ToUInt32(tmp.ToString())
吃前导零。Hi它适用于“test”。但是如果我键入$a=“parolaeste”我在php端得到了不同的输出。我想我知道哪里出了问题:
返回System.Text.Encoding.Unicode.GetString(byteHashValue);
并非所有二进制字符串都能生成正确的UTF-16字符串。某些位的组合无效。而php bin2hex(md5())不做额外的UTF转换。我今天稍后会准备一个修复程序。请在另一个答案中试用我的新版本。如果我键入$a=“111111”我在php中得到不同的输出,plus.LOL中有一个0,这是因为
hex+=String.Format(“{0:x2}”,(uint)System.Convert.ToUInt32(tmp.ToString())
吃前导零。该散列的输入是什么?尝试新版本:)我现在一个接一个地搜索无效的UTF-16字符。干得好。终于开始工作了。非常感谢您的帮助,就像真的很棒一样。该散列的输入是什么?尝试新版本:)我现在一个接一个地搜索无效的UTF-16字符。很好的工作。终于开始工作了。非常感谢你的帮助,就像真的很好的工作一样。