Python/ciphers/rot13.py

38 lines
833 B
Python
Raw Normal View History

def dencrypt(s: str, n: int = 13) -> str:
"""
https://en.wikipedia.org/wiki/ROT13
>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
>>> s = dencrypt(msg)
>>> s
"Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!"
>>> dencrypt(s) == msg
True
"""
2019-10-05 05:14:13 +00:00
out = ""
2017-10-29 17:44:54 +00:00
for c in s:
if "A" <= c <= "Z":
2019-10-05 05:14:13 +00:00
out += chr(ord("A") + (ord(c) - ord("A") + n) % 26)
elif "a" <= c <= "z":
2019-10-05 05:14:13 +00:00
out += chr(ord("a") + (ord(c) - ord("a") + n) % 26)
2017-10-29 17:44:54 +00:00
else:
out += c
return out
def main():
s0 = input("Enter message: ")
2017-10-29 17:44:54 +00:00
s1 = dencrypt(s0, 13)
print("Encryption:", s1)
2017-10-29 17:44:54 +00:00
s2 = dencrypt(s1, 13)
print("Decryption: ", s2)
2017-10-29 17:44:54 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
import doctest
doctest.testmod()
2017-10-29 17:44:54 +00:00
main()