我在阅读了有关Jon Skeet的有趣内容之后找到了一个字符串,我猜这是在ROT13中.在检查我的猜测之前,我想我会尝试用PHP解密它.这就是我的所作所为:
$string = "Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu.";
$tokens = str_split($string);
for ($i = 1; $i <= sizeof($tokens); $i++) {
$char = $tokens[$i-1];
for ($c = 1; $c <= 13; $c++) {
$char++;
}
echo $char;
}
我的字符串回来了,如你自己aabakaead ABruacae Sacahnaeaiaer到adaeacrypt tahais,ahae’ad acrusah你的sakualal waitah ahais alaauagah.
我的逻辑似乎很接近,但显然是错的.你能帮帮我吗?
解决方法:
这是一个有效的实现,不使用嵌套循环.您也不需要将字符串拆分为数组,因为您可以像使用PHP中的字符串数组一样索引单个字符.
您需要知道ASCII大写字符的范围是65 – 99,小写字符的范围是97 – 122.如果当前字符在其中一个范围内,则将ASCII值加13.然后,检查是否应该转到字母表的开头.如果你应该翻身,减去26.
$string = "Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu.";
for ($i = 0, $j = strlen( $string); $i < $j; $i++)
{
// Get the ASCII character for the current character
$char = ord( $string[$i]);
// If that character is in the range A-Z or a-z, add 13 to its ASCII value
if( ($char >= 65 && $char <= 90) || ($char >= 97 && $char <= 122))
{
$char += 13;
// If we should have wrapped around the alphabet, subtract 26
if( $char > 122 || ( $char > 90 && ord( $string[$i]) <= 90))
{
$char -= 26;
}
}
echo chr( $char);
}
这会产生:
If you asked Bruce Schneier to decrypt this, he’d crush your skull with his laugh.