(100%結貼)求把這幾行代碼翻譯成PHP的。解決方案

來源:互聯網
上載者:User
(100%結貼)求把這幾行代碼翻譯成PHP的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
const

XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$84,$47); //字串加密用

function Dec(Str:String):String;//字元解密函數
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) div 2 do
begin
Result:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]);
j:=(j+1) mod 8;
end;
end;



就上面幾行代碼,
求懂php的幫忙把這個函數翻譯成php的,
我delphi加密,然後php解密,


我對php一點也不懂,但是懂調用。
------解決思路----------------------
$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);

echo $s = enc('Edit2', $XorKey), PHP_EOL;

echo dec($s, $XorKey);

function Enc($Str, $XorKey) { //:String;//字元加密函數 這是用的一個異或加密
$Result = '';
$j = 0;
for($i=0; $i $Result .= sprintf('%02X', ord($Str{$i}) ^ $XorKey[$j]);
$j = ($j+1) % 8;
}
return $Result;
}

function Dec($str, $XorKey){
$result = "";
for($i=0, $j=0; $i $result .= chr(hexdec($str{$i} . $str{$i+1}) ^ $XorKey[$j]);
$j = ++$j % 8;
}
return $result;
}
F76DC321A1
Edit2

------解決思路----------------------
引用:
能順便把加密函數也翻譯一下嗎

function Enc(Str:String):String;//字元加密函數 這是用的一個異或加密
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) do
begin
Result:=Result+IntToHex(Byte(Str[i]) xor XorKey[j],2);
j:=(j+1) mod 8;
end;
end;



好測試密文



$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);
function Dec($str){
global $XorKey;
$result = "";
$j = 0;
for ($i = 0; $i < strlen($str)/2; $i++)
{
$result = $result . chr(hexdec($str[$i*2] . $str[$i*2+1]) ^ $XorKey[$j]);
$j = ++$j % 8;
}
return $result;
}


function Enc($str){
global $XorKey;
$result = "";
$j = 0;
for ($i = 0; $i < strlen($str); $i++)
{
$result = $result. dechex(ord($str[$i])^$XorKey[$j]);
$j = ++$j % 8;
}
return $result;
}
echo Enc("Edit2")."\n";
echo Dec("F76DC321A1");
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.