Python/ciphers/rot13.py
Jenia Dysin 9d745b6156
Add typehints ciphers and bool alg (#3264)
* updating DIRECTORY.md

* updating DIRECTORY.md

* Fixed accidental commit of file I have't touched

* fixup! Format Python code with psf/black push

* updating DIRECTORY.md

* updating DIRECTORY.md

* Fixed some suggested coding style issues

* Update rsa_key_generator.py

* Update rsa_key_generator.py

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
2020-10-16 14:11:52 +08:00

38 lines
833 B
Python

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
"""
out = ""
for c in s:
if "A" <= c <= "Z":
out += chr(ord("A") + (ord(c) - ord("A") + n) % 26)
elif "a" <= c <= "z":
out += chr(ord("a") + (ord(c) - ord("a") + n) % 26)
else:
out += c
return out
def main():
s0 = input("Enter message: ")
s1 = dencrypt(s0, 13)
print("Encryption:", s1)
s2 = dencrypt(s1, 13)
print("Decryption: ", s2)
if __name__ == "__main__":
import doctest
doctest.testmod()
main()