No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

ROT13 Python Code

by Khaleel O.
January 7, 2022
in Python
Reading Time: 3 mins read
A A
ROT13 Python Code
ROT13 Python Code

Today we’ll show you some ROT13 Python code and explain how it works. We’ll be using Python 3.8.10. Let’s go! ✨⚡

ROT13 or ROT-13 is a letter substitution cipher that replaces each letter of the message to be encoded, with the 13th after it in the alphabet. ROT is actually an acronym for “rotate by 13 places”. Simple, right?

Let’s write some code:


abc = "abcdefghijklmnopqrstuvwxyz"
m = "xgetxbetterxbyxcodingxdaily"

rt13 = lambda x: "".join([abc[(abc.find(c) + 13) % 26] for c in x])

print(rt13(m))

#output
#ktrgkorggrekolkpbqvatkqnvyl

Let’s explain what is going on here:

  1. We declare the alphabet as abc. We include all letters from A to Z.
  2. We declare our message m which is the plaintext. The actual message is “get better by coding” but we include a leading and trailing x as well as include x in the whitespace. Remember that a whitespace is technically a character but it doesn’t exist in the alphabet so it’ll confuse the algorithm.
  3. Recall that a lambda function in Python is an anonymous inline function with one expression. In this case the expression is substituting each letter of our plaintext with the 13th letter to the right of it in abc. The expression will also wrap around and start counting at the beginning of abc if the 13th letter reaches beyond the end of the string.
  4. We pass our original plaintext m to the lambda function rt13 and we get our ciphertext which we print to the screen i.e. the encrypted plaintext after the substitution is applied.

Nice! If all goes well, you should see a message printed to the screen that looks like gibberish. This is exactly what we want to see.

It turns out that the ROT13 algorithm is very very easy to crack. All we have to do is apply the same algorithm to the ciphertext, once we know that ROT13 was used to encrypt the data, and we can crack it:

print(rt13(rt13(s)))

#output
#xgetxbetterxbyxcodingxdaily

See how easy that was? We simply apply the same algorithm to the ciphertext to undo the encryption and voila, we have the original message!

Here’s the entire script:

abc = "abcdefghijklmnopqrstuvwxyz"
m = "xgetxbetterxbyxcodingxdaily"

rt13 = lambda x: "".join([abc[(abc.find(c) + 13) % 26] for c in x])

#encrypt plaintext
print(rt13(m))

#decrypt ciphertext
print(rt13(rt13(m)))

Note that ROT13 is not a good way to secure data, obviously. It is a well known algorithm that is easily cracked once one knows that it was used to encrypt the original data. The above code is for education purposes only. Use at your own risk!

Thanks for reading! Find another great tutorial on encryption HERE. Good luck! 👌👌👌

Tags: encryptionrot-13rot13
Previous Post

Python LRU Cache Example

Next Post

Sieve of Eratosthenes In Python

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy