我是 Xor 加密的新手,并且在使用以下代码时遇到了一些问题:
function xor_this($string) {
// Let's define our key here
$key = ('magic_key');
// Our plaintext/ciphertext
$text =$string;
// Our output text
$outText = '';
// Iterate through each character
for($i=0;$i<strlen($text);)
{
for($j=0;$j<strlen($key);$j++,$i++)
{
$outText .= $text{$i} ^ $key{$j};
//echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging
}
}
return $outText;
}
当我运行它时,它适用于普通字符串,例如“dog”,但它仅部分适用于包含数字的字符串,例如“12345”。
展示...
xor_this('dog')
= 'UYV'
xor_this('123')
= ''
还值得注意的是xor_this( xor_this('123') )
='123',正如我所期望的那样。我很确定问题出在我对按位运算符的不稳定理解上,或者可能出在 PHP 处理包含数字的字符串的方式上。我敢打赌,一定有一个聪明的人知道这里出了什么问题。谢谢。
编辑#1:这不是真正的“加密”。我想混淆是正确的术语,这就是我正在做的事情。我需要传递包含用户不重要数据的代码,而他们又不能轻易篡改它。他们正在离线完成一项定时活动,并通过此代码将其时间提交到在线记分牌。线下活动将混淆他们的时间(以毫秒为单位)。我需要编写一个脚本来接收此代码并将其转回包含其时间的字符串。
我是如何做到的,可能会帮助别人......
$msg = 'say hi!';
$key = 'whatever_123';
// print, and make unprintable chars available for a link or alike.
// using $_GET, php will urldecode it, if it was passed urlencoded
print "obfuscated, ready for url: " . urlencode(obfuscate($msg, $key)) . "\n";
print "deObfuscated: " . obfuscate(obfuscate($msg, $key), $key);
function obfuscate($msg, $key) {
if (empty($key)) return $msg;
return $msg ^ str_pad('', strlen($msg), $key);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)