atbash Algorithm

The Atbash algorithm is a simple yet ancient substitution cipher that was originally used for the Hebrew alphabet but can also be applied to any alphabetic writing system. It is a type of monoalphabetic cipher, which means that each letter in the alphabet is replaced with a single, fixed substitution throughout the encryption process. The key feature of the Atbash cipher is that it involves replacing the first letter of the alphabet with the last, the second with the second to last, and so on. In other words, it reverses the alphabet for encryption and decryption, making it a symmetrical cipher. The Atbash cipher is considered to be a special case of the more general affine cipher, which is a more complex substitution cipher that employs mathematical formulas. While the Atbash algorithm is relatively simple and easy to understand, it is not considered to be a secure method of encryption by modern standards. This is because it can be easily broken using frequency analysis, a technique that involves analyzing the frequency of letters in the encrypted text and comparing it to the letter frequency of the language in which the message was originally written. Despite its lack of security, the Atbash cipher has historical significance and has been featured in various works of literature and popular culture, such as the Bible and Dan Brown's "The Da Vinci Code." Today, the Atbash cipher serves as an interesting example of early encryption techniques and a stepping stone to understanding more advanced cryptographic methods.
""" https://en.wikipedia.org/wiki/Atbash """
import string


def atbash_slow(sequence: str) -> str:
    """
    >>> atbash_slow("ABCDEFG")
    'ZYXWVUT'

    >>> atbash_slow("aW;;123BX")
    'zD;;123YC'
    """
    output = ""
    for i in sequence:
        extract = ord(i)
        if 65 <= extract <= 90:
            output += chr(155 - extract)
        elif 97 <= extract <= 122:
            output += chr(219 - extract)
        else:
            output += i
    return output


def atbash(sequence: str) -> str:
    """
    >>> atbash("ABCDEFG")
    'ZYXWVUT'

    >>> atbash("aW;;123BX")
    'zD;;123YC'
    """
    letters = string.ascii_letters
    letters_reversed = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
    return "".join(
        letters_reversed[letters.index(c)] if c in letters else c for c in sequence
    )


def benchmark() -> None:
    """Let's benchmark them side-by-side..."""
    from timeit import timeit

    print("Running performance benchmarks...")
    print(
        "> atbash_slow()",
        timeit(
            "atbash_slow(printable)",
            setup="from string import printable ; from __main__ import atbash_slow",
        ),
        "seconds",
    )
    print(
        ">      atbash()",
        timeit(
            "atbash(printable)",
            setup="from string import printable ; from __main__ import atbash",
        ),
        "seconds",
    )


if __name__ == "__main__":
    for sequence in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
        print(f"{sequence} encrypted in atbash: {atbash(sequence)}")
    benchmark()

LANGUAGE:

DARK MODE: