Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   ROT13 Encoding Algorithm (http://www.go4expert.com/articles/rot13-encoding-algorithm-t5845/)

ROT13 Encoding Algorithm

### Introduction

ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple Caesar cipher used in online forums as a means of hiding spoilers, punchlines, puzzle solutions, and offensive materials from the casual glance. ROT13 has been described as the "Usenet equivalent of a magazine printing the answer to a quiz upside down".

ROT13 provides no real cryptographic security and is not used for such; in fact it is often used as the canonical example of weak encryption. An additional feature of the cipher is that it is symmetrical; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding.

### Algorithm

Applying ROT13 to a piece of text merely requires examining its alphabetic characters and replacing each one by the letter 13 places further along in the alphabet, wrapping back to the beginning if necessary. A becomes N, B becomes O, and so on up to M, which becomes Z, then the sequence reverses: N becomes A, O becomes B, and so on to Z, which becomes M. Only those letters which occur in the English alphabet are affected; numbers, symbols, whitespace, and all other characters are left unchanged. Because there are 26 letters in the English alphabet and 26 = 2 × 13, the ROT13 function is its own inverse.
In other words, two successive applications of ROT13 restore the original text.

### Implementation

Code: PHP

`function rot13( \$text )  {      \$codec_text = '';      \$len = strlen(\$text);            for ( \$i=0; \$i < \$len; \$i++ )      {          \$k = ord(\$text[\$i]);          // 65-77 to 78-90 and 97-109 to 110-122          if ( (\$k >= 65 && \$k <= 77) || (\$k >= 97 && \$k <= 109) )          {              \$codec_text .= chr(\$k+13);          }          // 78-90 to 65-77 and 110-122 to 97-109          elseif ( (\$k >= 78 && \$k <= 90) || (\$k >= 110 && \$k <= 122) )          {              \$codec_text .= chr(\$k-13);          }          else          {              \$codec_text .= chr(\$k);          }      }      return \$codec_text;  }`

 jwshepherd 14Aug2007 12:55

Re: ROT13 Encoding Algorithm

rot 13 is also one of the easiest to crack even without using a script. By Identifying the most used letters and the single characters you can usually decipher say about 40% on just visual inspection. Please do not rely on it to encrpyt or hid secret notes...

 LenoxFinlay 24Jul2009 09:58

Re: ROT13 Encoding Algorithm

Applying ROT13 to a piece of text merely requires examining its alphabetic characters and replacing each one by the letter 13 places further along in the alphabet, wrapping back to the beginning if necessary.[2] A becomes N, B becomes O, and so on up to M, which becomes Z, then the sequence reverses: N becomes A, O becomes B, and so on to Z, which becomes M. Only those letters which occur in the English alphabet are affected; numbers, symbols, whitespace, and all other characters are left unchanged. Because there are 26 letters in the English alphabet and 26 = 2 × 13, the ROT13 function is its own inverse

 All times are GMT +5.5. The time now is 22:29.