Python/ciphers/rot13.py

37 lines
930 B
Python
Raw Normal View History

def dencrypt(s: str) -> str:
"""
Applies ROT13 encryption or decryption to the input string.
Example usage:
>>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!"
>>> encrypted = dencrypt(msg)
>>> encrypted
"Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!"
>>> dencrypt(encrypted) == msg
True
"""
result = []
2017-10-29 14:44:54 -03:00
for c in s:
if "A" <= c <= "Z":
result.append(chr(ord("A") + (ord(c) - ord("A") + 13) % 26))
elif "a" <= c <= "z":
result.append(chr(ord("a") + (ord(c) - ord("a") + 13) % 26))
2017-10-29 14:44:54 -03:00
else:
result.append(c)
return "".join(result)
2017-10-29 14:44:54 -03:00
def main() -> None:
s0 = input("Enter message: ")
2017-10-29 14:44:54 -03:00
s1 = dencrypt(s0)
print("Encryption:", s1)
2017-10-29 14:44:54 -03:00
s2 = dencrypt(s1)
print("Decryption: ", s2)
2017-10-29 14:44:54 -03:00
2019-10-05 01:14:13 -04:00
if __name__ == "__main__":
import doctest
doctest.testmod()
2017-10-29 14:44:54 -03:00
main()