Hudzilla.org - the homepage of Paul Hudson
Contents > Security concerns > Protecting your data Wish List | Report Bug | About Me ]

17.3.4     Basic symmetric encryption in action: str_rot13()

This is NOT the latest copy of this book; click here for the latest version.

string str_rot13 ( string input)

Symmetric encryption in PHP is very easy to get started first, so we will be looking at it first using the ROT13 algorithm. The ROT13 algorithm is not very secure - the algorithm is probably the most widely know encryption method around, and it can be decrypted by anybody just using pencil and paper. However, that does not make it useless, as ROT13 is very popular as an "obfuscator". For example, web sites that include sensitive information, such as information about the ending of a movie that many will not have seen yet, often ROT13 their information. Because of this, readers are just one click away from reading the spoilers, but they cannot read it by accident by just looking at the screen.

PHP gives you access to the ROT13 algorithm through the function str_rot13() - it takes just one parameter (the string to encrypt), and returns the encrypted version. Take a look at this example:

<?php
    $string
= "Hello, world!\n"
    
print str_rot13($string);
?>

That script outputs "Uryyb, jbeyq!", as expected. There are three key things to note with that output - firstly, because every letter is simply shifted thirteen places to the right in the alphabet, double letters like the "ll" in "Hello" are glaring obvious. Secondly, the frequency that letters appear in English is not uniform - E appears approximately 60 times more often than Q, twice as often as S, and four times as often as M and H. Using knowledge of English letter frequencies, it is easy to decrypt any message that uses such simple encryption.

Finally, note that symbols (and also numbers) are left as-is using ROT13, which makes it useless for encrypting numeric messages.

Author's Note: have no doubt that ROT13 is useless for encryption - its main role is to make the meaning of text not immediately apparent, so that readers must specifically select to view potentially offensive/unwanted material. In this capacity, ROT13 is a good choice.





<< 17.3.3 Asymmetric vs. Symmetric   17.3.5 Advanced symmetric encryption >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 13 Oct 2008

You could easily write one.

A PHP User - 13 Oct 2008

i just want to know if theres such a code of ROT-13 that can be run on a visual basic 6.0??



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow