The Code Cave's ROT13 Encoder & Decoder

ROT13 is a quick and easy encyrption method. It's like one of those first codes you learn when you form your own super hero club. The REALLY neat thing about ROT13 is that the exact same method to encrypt the text is used to decrypt the message.


Enter your message hear and click "Encode/Decode" to see ROT13 in action:

How does ROT13 Work?

ROT13 stands for Rotate by 13. Why by 13? Well, that's because it cuts the 26 letters of the alphabet in half and the alphabet sort of folds in on itself. You encode/decode ROT 13 by counting forward to the 13th letter after the one you are on. If you count forward by another 13 characters, you're back to the letter you first started on.

Perhaps the best way to understand it is to make a kids decoder bracelet. Just write out the whole alphabet in large letters in Notepad. Print that out on your printer, making sure it all fits on one line. Cut that line out and tape the ends toghether. Then you just find your letter and count forward by thirteen rotating the bracelet as you go. Start at the letter A and move forward by 13, and you will be at the letter N. Start at N and move forward by 13 and you'll be at the letter A.

Another way to do this illustrates, perhaps, the cleanest method for coding a ROT13 function. In Notepad type out the first 13 letters, of the alphabet, on one line. On the next line write out the next 13 letters. (Make sure that you us a fixed space font like courier. This will mean there letters are all the same width and directly above and below each other.) Then for the first half of the alphabet you just look down the page and see the adjacent letter. For letters in the second half of the alphabet, you simply back up and look at the character above it.

In ASCII, that's the same as adding 13 (decimal) to the character if it is A..M and subtracting 13 if it is from N..Z. As is shown in this chart:

Character   Letter     ASCII    Shift      Result
   A            1         65      +13         N
   B            2         66      +13         O
   M           13         77      +13         Z
   N           14         78      -13         A
   Y           25         89      -13         L
   Z           26         90      -13         M

In PHP you can use the str_rot13() method to convert your text. If you build your own functions, you can have neat stuff like ROT85 or any strange function like that IF you choose your own character sets. Did that help? The Code Cave