Share your repls and programming experiences

← Back to all posts
##### Rot13 cypher

Function to convert a string to its rot13 equivelant.

Voters
Alto2 (1)
hotnewtop
malvoliothegood (851)

Interesting. How do you decipher enciphered text? Also, you could make your code slightly more efficient, like so:

``````for letter in inputStr:

letter_value = ord(letter)

if 64 < letter_value < 78 or 96 < letter_value < 110:
message += chr(letter_value + 13)
elif 91 > letter_value > 77 or 123 > letter_value > 109:
message += chr(letter_value-13)
else:
message += letter

return message``````
Alto2 (1)

@malvoliothegood Thanks for the feedback! I appreciate it. The renderer I typically use is a bit weird and sometimes += doesn't want to work. To answer your question, you can decipher encoded text by putting it back through the function. ROT13 shifts each letter 13 places in the alphabet, so shifting 13 more would restore it to the original.

malvoliothegood (851)

@Alto2 Thanks for the explanation. I guess you have heard of the Caesar Cipher as well.

Alto2 (1)

@malvoliothegood I have not! Mind explaining it?

malvoliothegood (851)

@Alto2 The cipher is named after the Roman Emperor Julius Caesar you reportedly used it to communicate with his generals in the field of battle. Method:
1. Each letter in the alphabet has a number assigned to it a = 1, b= 2...z = 26.
2. A key is decided upon. Historically this was 3, so key = 3
3. Add the key value to the letter value then use the resulting number to lookup the corresponding letter e.g. a (1) -> (1 + key) -> (1+ 3 = 4) -> d
4. If adding the key value to a letter's value results in a number greater than 26 then 'wrap around' to the beginning of the alphabet and proceed as normal
5. Another example: key = 3, word to encode = 'zoo'
z = 26, o = 15, o = 15. Add the key to get 29, 18, 18. Wrap the 29 by subtracting 26 from it 29 - 26 = 3 to get 3, 18, 18. Look up the equivalent letters which are: c, r, r, so 'zoo' becomes 'crr'
6. The total key space for the cipher is 26 as there are that may possible shifts. This makes it quite a weak one. The rule of thumb for ciphers is the bigger the key space the stronger the cipher.

Here is a repl showing the cipher in action: https://replit.com/@malvoliothegood/caesarcipher#main.py

Alto2 (1)

@malvoliothegood Nice! Although, (and I’m being nitpicky here) you can use triple single quotes (‘’’) for multi line comments.

malvoliothegood (851)

@Alto2 Yes. You can see that I used them for one string of text.