How to Make ROT-13 in "Python"

How to Make ROT-13 in "Python" thumbnail
ROT-13 works by rearranging letters of the alphabet.

ROT-13 is a low-level encryption method that uses a swapping mechanism to change the appearance of character data. In it, the first 13 letters of the alphabet - A through M - exchange places with the next 13 letters – N through Z. Employing this encryption method causes a word such as “this” to display as “guvf”or the word “simple” to display as “fvzcyr.” All versions of Python programming provide an easy to use string function called “str.maketrans” in which you first set the stage for ROT-13, then process text for encryption.

Instructions

    • 1

      Tell Python to use ROT-13 and add the translation characters as arguments – or parameters – to the str.maketrans() function. Because this function works in a “from-to” fashion, add the alphabet as normal, then again using ROT-13 translation. ROT-13 works best if you tell Python to use only upper or lower-case letters but if necessary, your arguments can include both:

      rot13_trans = str.upper.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ’, 'NOPQRSTUVWXYZABCDEFGHIJKLM’)

      rot13_trans = str.lower.maketrans(‘abcdefghijklmnopqrstuvwxyz',‘nopqrstuvwxyzabcdefghijklm')

      rot13_trans = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')

    • 2

      Add the text string you want to translate as an argument to the ROT-13 function definition. Type in the name you gave the string when adding it to your Python program:

      def rot13 (sampleMessage)

    • 3

      Make the ROT-13 translation and display the results:

      return sampleMessage.translate(rot13_trans)

Tips & Warnings

  • Understand that an ROT-13 translation is not a secure encryption method. Use it for making messages difficult to read rather than to ensure data security.

  • Because this method focuses on letters of the alphabet, ROT-13 excludes numbers, white space and punctuation marks from the encryption process.

Related Searches:

References

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

Related Ads

Featured