Deploy ROT13 with PHP

I found a line after reading funny things about John Skeet, and I guessed that it was in ROT13. Before just checking my guess, I decided to try and decrypt it using PHP. Here is what I had:

$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;
}

My string is returned as AIaf you aasakaead ABruacae Sacahnaeaiaer to adaeacrypt tahais, ahae'ad acrusah your sakualal waitah ahais alaauagah.

My logic seems pretty close, but obviously this is wrong. Can you help me?

+3
source share
5 answers

Here is a working implementation, without using a nested loop. You also do not need to split the string into an array, as you can index individual characters in the same way as an array with strings in PHP.

, ASCII 65 99, - 97 122. , 13 ASCII. , . , 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);
}

:

, .

+3

, , . 13 (, 13!) . 13 A-M 13 N-Z. , , .

, , , , , .

+2

, z ++ - aa

$letter = "z";
$letter++;
echo($letter);

returns aa not a

EDIT: A possible alternative solution that does not use the built-in,

$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));
}
+2
source

A few years later late, but I thought I would give you another option to do this

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));
}
0
source

All Articles