用PHP实现ROT13

用PHP实现ROT13,php,encryption,rot13,Php,Encryption,Rot13,在读了关于乔恩·斯基特的趣事后,我发现了一根绳子,我猜它是在13号公路上。在检查我的猜测之前,我想我应该尝试用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++) { $

在读了关于乔恩·斯基特的趣事后,我发现了一根绳子,我猜它是在13号公路上。在检查我的猜测之前,我想我应该尝试用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;
}
$string=“Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf,ur'q pehfu lbhe FXHY jvgu uvf ynhtu。”;
$tokens=str_split($string);

对于($i=1;$i请尝试
stru rot13


不需要自己创建,它是内置的。

如果你想自己创建,而不是使用现有的解决方案,你需要检查每个字母是在字母表的前半部分还是后半部分。你不能天真地添加13(还有,为什么要使用循环来添加13?!)对每个字符。你必须在A-M中加13,在N-Z中减去13。你还必须,不改变任何其他字符,如空格


修改代码,在修改之前检查每个字符的内容,这样您就知道是否以及如何修改它。

这里是一个工作实现,不使用嵌套循环。您也不需要将字符串拆分为数组,因为您可以像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);
}
$string=“Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf,ur'q pehfu lbhe FXHY jvgu uvf ynhtu。”;
对于($i=0,$j=strlen($string);$i<$j;$i++)
{
//获取当前字符的ASCII字符
$char=ord($string[$i]);
//如果该字符在A-Z或A-Z范围内,请在其ASCII值上加13

如果($char>=65&&$char=97&&$char 122 | |($char>90&&ord($string[$i])这不起作用,因为z++是aa

$letter = "z";
$letter++;
echo($letter);
返回aa而不是a

编辑:可能的替代解决方案不使用内置的is

$string = "Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu.";
$tokens = str_split($string);

foreach($tokens as $char)
{
    $ord = ord($char);
    if (($ord >=65 && $ord <=90 ) || ($ord >= 97 && $ord <= 122))
    $ord = $ord+13;
    if (($ord > 90 && $ord < 110) || $ord > 122)
        $ord = $ord - 26;
    echo (chr($ord));
}
$string=“Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf,ur'q pehfu lbhe FXHY jvgu uvf ynhtu。”;
$tokens=str_split($string);
foreach($char形式的代币)
{
$ord=ord($char);
如果($ord>=65&&$ord=97&&$ord 90&&$ord<110)| |$ord>122)
$ord=$ord-26;
echo(chr($ord));
}

派对晚了几年,但我想我会给你另一个选择

function rot13($string) {
    // split into array of ASCII values
    $string = array_map('ord', str_split($string));

    foreach ($string as $index => $char) {
        if (ctype_lower($char)) {
            // for lowercase subtract 97 to get character pos in alphabet
            $dec = ord('a');
        } elseif (ctype_upper($char)) {
            // for uppercase subtract 65 to get character pos in alphabet
            $dec = ord('A');
        } else {
            // preserve non-alphabetic chars
            $string[$index] = $char;
            continue;
        }
        // add 13 (mod 26) to the character
        $string[$index] = (($char - $dec + 13) % 26) + $dec;
    }

    // convert back to characters and glue back together
    return implode(array_map('chr', $string));
}

啊,谢谢。我不知道。尽管如此,重点是锻炼我的大脑,所以我还是想自己构建它。为了它的价值(对于任何将其标记为加密的人),rot13不是加密,它是一种密码。加密涉及一个密钥,使用该密钥可以使相关数据无法读取,从而使得在不拥有加密密钥的情况下很难获取数据。@damianb哦,这是ROT加密,密钥的值为13:).虽然我能理解标签,但它吸引了合适的人群,人们可能会用这个词来寻找问题/解决方案,在这方面确实有效。