我有以下执行 XOR 编码/解码过程的代码:
<?php
/*
* Simple XOR encoder/decoder, Inkubus
*
*/
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
//$encoded = ")5/Y+F8'0P$/;"; // <- OK, Working
//$decoded = 'hM5cf$350';
$encoded = "-?^<]TLV.GQV)B4[YQ "; // <- BAD, Not working
$decoded = "CTC(*zlkb4848";
//$encoded = ')3\8?E(21@$;='; // <- OK, Working
//$decoded = 'suances06';
function decode($encoded,$key) {
$cipher = convert_uudecode($encoded);
$plain = $cipher ^ $key;
$result = substr($plain, 0, strlen($cipher));
return $result;
}
function encode($decoded,$key) {
$plain = $decoded ^ $key;
$result = convert_uuencode(substr($plain, 0, strlen($decoded)));
$result = preg_replace("/\n/m","",$result);
$result = preg_replace("/`$/m","",$result);
return $result;
}
echo "Encoded: " . $encoded . "\n";
echo "Decoded: " . decode($encoded,$key) . "\n";
echo "Encoded: " . encode($decoded,$key) . "\n";
echo "Decoded: " . decode(encode($decoded,$key),$key) . "\n";
?>
在标题之后评论的是两个有效的示例,未注释的是有问题的示例。我怎样才能通常评论要解码的 UUENCODED 字符串,使其保持不变并正确解码?注释字符串中的任何有问题的字符不是一种选择,而是整个字符串。
工作运行示例:
bash-$ php xor.php
Encoded: )3\8?E(21@$;=
Decoded: suances06
Encoded: )3\8?E(21@$;=
Decoded: suances06
不工作的例子:
bash-$ php xor.php
Encoded: -?^<]TLV.GQV)B4[YQ
Decoded: CTC(*zlkb484
Encoded: ,?^<]TLV.GQV)B4[Y
Decoded: CTC(*zlkb484
某些字符丢失或其他。有任何想法吗?
谢谢!
更新:另一个不工作的例子:
$encoded = "-!8H<RY67FP';C1+]R@ "; // <- BAD, Not working
$decoded = "99b1rchw00d06";
冉:
bash-$ php xor.php
Encoded: -!8H<RY67FP';C1+]R@
Decoded: 99b1rchw00d0
Encoded: ,!8H<RY67FP';C1+]
Decoded: 99b1rchw00d0