# ROT13 Algorithm Using Python

ROT13 (“rotate by 13 places”, sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome.(Source Wiki). Here Is the new version I have written to apply ROT 13 for alphabets only, special characters are not touched they will remain as it is.

I have tried to implement this Algorithm Using Python. Here it goes..

# ROT13 Algorithm

def rot(text):
arr = list(text)
for i in range(len(arr)):
if (ord(arr[i]) > 64 and ord(arr[i]) <91) or (ord(arr[i])>96 and ord(arr[i])<123): #check for alphabets only
nextCharValue = (ord(arr[i]) + 13) #sum
asciValue = ord(arr[i]) #ascii value
# print “ascii “,asci, ” sum”, sum
if asciValue > 64 and asciValue < 91:
if nextCharValue > 90:
newValue = ord(arr[i]) – 13
else:
newValue = ord(arr[i]) + 13
elif asciValue> 96 and asciValue < 123:
if nextCharvalue > 122:
newValue = ord(arr[i]) – 13
else:
newValue = ord(arr[i]) + 13

arr[i] = chr(newValue)

return ”.join(arr)

str = rot(‘How can you tell an extrovert from an introvert at NSA? ‘)
print “ROT13:-“, str
print “Back to original:- “, rot(str)

Please share your feedback, I am open for any suggestions.(you can also have a look on the code where I have tested this http://www.codeskulptor.org/#user16_LTWEIBBe36nu7xC.py)

Cheers!!

Thanks
R Vashi

This site uses Akismet to reduce spam. Learn how your comment data is processed.