In programming, there will be times when you may need to encrypt or obscure some text. Typically you'll encrypt text that you don't want others to see, maybe personal data or otherwise. Obscuring text is done when the need to protect the data is less of a concern. This might include obscuring spoilers or passwords in a configuration file. Learning how to obscure code is a great way to learn cryptography using a simple algorithm such as ROT13 and with PHP's str_rot13 function.
What is ROT13 in programming?
ROT13 or rotate by 13 places, is a simple substitution cipher that's used to obscure text. It works by replacing each letter in a body of text with a letter that is 13 positions ahead of it in the English alphabet. It's used as a quick way to scramble text and requires little prior knowledge of cryptography, and therefore is a great way to get started with ciphers in programming. Let's look at an example of ROT13 in action.
If we applied ROT13 to the letter 'A', it would be replaced with 'N', because 'N' is 13 places from the letter 'A'. Similarly, if we took 'B' and applied ROT13, it becomes 'O'. Because we always rotate by 13 places, the loose "encryption" is symmetrical, meaning that, the same algorithm is used to encode and decode the text. With that in mind, it should not be used to protect sensitive data, but instead used as a learning tool or for a bit of fun.
ROT13 is a simple substitution cipher that is often used for obscuring text. It works by replacing each letter in the original text with a letter that is 13 positions ahead of it in the alphabet. For example, the letter 'A' is replaced with 'N', 'B' is replaced with 'O', and so on. The encryption is symmetrical, so the same algorithm can be used to decode the encrypted text.
In programming, ROT13 is often used as a quick and simple way to obscure text, such as obscuring spoilers or obscuring passwords in configuration files. The ROT13 algorithm can be implemented in most programming languages and is a good way to get started with simple cryptography. However, it is not a secure encryption algorithm and should not be used for sensitive data.
Why does ROT13 rotate by 13 places?
That's simple. It's because rotating a letter 13 places to obscure and then rotating again 13 places returns it to the original text. That's due to the number of letters in the alphabet (26 letters). It rotates backward and forwards because half of 26 is, you guessed it, 13. Because of this, it's easy for someone (even without a handy PHP function) to covert your text back to its original state.
Oscursing text with ROT13 algorithm with the PHP function str_rot13
With PHP it is possible to rotate text and obscure using a built-in function called str_rot13. This function does exactly what the algorithm is set out to do by rotating each letter by 13 places. Let's explore some PHP ROT13 examples with the str_rot13 function.
# Results in: Sbb one
str_rot13("Foo bar");
We can also decrypt this text using the same algorithm, that's because the ROT13 algorithm rotates by 13 places, and if we rotate again by 13 places we return to the original text. If we take the result from the previous str_rot13 function and re-run through str_rot13 again, it should rotate the letters another 13 places, and therefore back to the original text, "Foo bar".
# Results in: Foo bar
str_rot13("Sbb one");
It is also possible to manually rotate the letters without using str_rot13 and using the PHP "strtr" function. But given there is a dedicated function in PHP for use with the ROT13, you are better off using that instead. The str_rot13 function only accepts one parameter of type string and will return a rotate string. Numbers or non-alpha characters will not get rotated, and subsequently, only letters "a-z" and "A-Z" will get rotated. Capitals will also get rotated to the same letter as lowercase letters.
Fancy playing with the ROT13 algorithm yourself? We've built a tool to allow you to do just that. Our free-to-use ROT13 decoder lets you enter an encrypted ROT13 string, and our tool will automatically decrypt it, following the rules that we've mentioned in this guide. Try using our example ROT13 text if you can't find or have one of your own to decrypt.
Conclusion
Using PHP's str_rot13 function is fun to learn the basics of cryptography and how changing text from one form can be converted back to its original state.
- Don't use str_rot13 to protect any personal or sensitive information or data, especially not passwords!
- Only letters in the English alphabet will be rotated