rot13 Algorithm

The ROT13 algorithm, also known as the Caesar cipher, is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. This encryption technique is used primarily for obscuring text by making it unreadable, yet easily reversible. The algorithm works by shifting the letters in the text 13 places forward or backward in the alphabet, wrapping around to the beginning of the alphabet when necessary. For example, the letter 'A' would be replaced by 'N', 'B' by 'O', and so on. Due to the 26-letter structure of the English alphabet, applying the ROT13 algorithm twice on the same text will return the original text, making it a straightforward and convenient method for simple encryption and decryption. While the ROT13 algorithm is easy to understand and implement, it is not considered secure for protecting sensitive information. Its simplicity makes it vulnerable to various attacks, such as frequency analysis, which is a technique for breaking substitution ciphers by analyzing the frequency of letters in the encrypted text. Despite its lack of security, ROT13 is still used today in specific contexts, such as online forums or comment sections, to hide spoilers or offensive language from casual readers. In these cases, ROT13 serves as a basic method to obscure text without the need for complex encryption algorithms.
def dencrypt(s: str, n: int = 13):
    """
    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()

LANGUAGE:

DARK MODE: