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