我正在尝试将一些C#代码转换为PHP。
C#代码如下:
strRequest="\auth\\pid\3045\ch\ehCkPNGS\resp\1529559b837decb2e0ed2f576806f10aeda4d9cdf781acd3e53ad1c3b11a20684f131229\ip\16777343\skey\3759\reqproof\1";
byte[] sendBuffer = Encoding.ASCII.GetBytes(strRequest);
Encode(sendBuffer, strRequest.Length);
private static void Encode(byte[] buff, int size){
byte[] tmp = new byte[8];
tmp[0] = 103;
tmp[1] = 97;
tmp[2] = 109;
tmp[3] = 101;
tmp[4] = 115;
tmp[5] = 112;
tmp[6] = 121;
tmp[7] = 0;
if (size != 0){
int i = size;
int j = 0;
int k = 0;
do{
if (tmp[j] == 0)
j = 0;
buff[k++] ^= tmp[j++];
--i;
} while (i != 0);
}
}
哪个应该输出一个字节数组:
'59','0','24','17','27','44','37','23','8','9','57','64','64','77','82','61','14','13','47','21','17','36','10','61','43','52','35','37','21','4','30','21','47','65','76','85','88','88','80','74','18','65','84','86','9','0','16','18','75','2','81','8','1','65','22','76','80','87','85','85','69','22','72','87','0','8','1','18','68','29','94','2','9','3','68','72','72','6','2','9','86','22','69','74','6','5','92','6','64','18','72','86','0','95','85','69','72','77','1','80','94','84','65','66','64','59','8','29','57','66','70','78','80','86','94','81','64','44','10','12','4','20','57','64','71','76','94','61','31','0','2','0','11','8','14','11','57','66',
我的等效PHP代码如下:
<?php
$strReq="\\auth\\\\pid\\3045\\ch\\atGzgVH5\\resp\\1529559b837decb2e0ed2f576806f10ac7d8b28205dcbd64c59666a11d43b814024837cb\\ip\\16777343\\skey\\1292\\reqproof\\1";
$strReqLen = strlen($strReq);
$strHex = unpack("C*",$strReq);
$test = Encode($strHex,$strReqLen);
print_r ($test);
function Encode($buff, $size){
$tmp = array(103,97,109,101,115,112,121,0);
$tester = array();
if ($size != 0){
$i = $size;
$j = 0;
$k = 0;
do {
if($tmp[$j] == 0){
$j = 0;
}
$buff[$k++] ^= $tmp[$j++];
--$i;
} while ($i != 0);
}
return $buff;
}
?>
但不输出任何类似C#代码的内容。
非常感谢任何帮助。
由于
戴夫
答案 0 :(得分:0)
C#使用从零开始的数组,但PHP的unpack(..)使用基于1的数组。
您可以更改
$strHex = unpack("C*",$strReq);
到
$strHex = array_merge(unpack("C*",$strReq));
使您的PHP数组从零开始,或者只是将$k
设置为初始值1。
在测试任一解决方案后,PHP结果与C#输出相同。