# ROT13 Encoding Algorithm

Discussion in 'PHP' started by pradeep, Aug 14, 2007.

Joined:
Apr 4, 2005
Messages:
1,646
86
Trophy Points:
0
Occupation:
Programmer
Location:
Kolkata, India

### 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

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

Last edited: Aug 14, 2007
2. ### jwshepherdNew Member

Joined:
Aug 13, 2007
Messages:
135
0
Trophy Points:
0
Occupation:
VP Technology Corporation
Location:
Texas
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...

Joined:
Apr 15, 2009
Messages:
46