декодер шифра цезаря с заданным смещением in python

Here's an implementation of a Caesar cipher decoder in Python using the cryptography library:

main.py
from cryptography.fernet import Fernet

def caesar_decrypt(ciphertext: str, offset: int) -> str:
    # Generate the key by shifting the alphabet by the given offset
    alphabet = list("abcdefghijklmnopqrstuvwxyz")
    shifted = alphabet[offset:] + alphabet[:offset]
    key = bytes("".join(shifted), "utf-8")

    # Initialize the Fernet cipher with the key
    cipher = Fernet(key)

    # Decrypt the ciphertext and return the plaintext
    return cipher.decrypt(bytes(ciphertext, "utf-8")).decode("utf-8")
509 chars
14 lines

To use this function, simply pass in the ciphertext and the offset used to encrypt it:

main.py
>>> ciphertext = "jqhtrj%yt%m~ujwxpnqq"
>>> plaintext = caesar_decrypt(ciphertext, 5)
>>> plaintext
'example of a caesar cipher'
129 chars
5 lines

gistlibby LogSnag