Untitled

 avatar
unknown
python
10 months ago
1.2 kB
5
Indexable
def caesar_cipher_encrypt(text, key):
    ciphertext = []
    for character in text:
        if character.isalpha():
            shifted = ord(character.upper()) - ord('A') + key
            shifted %= 26 
            encrypted_char = chr(shifted + ord('A'))
            ciphertext.append(encrypted_char)
        elif character == ' ':
            continue
        elif character == '.':
            ciphertext.append('X')
        else:
            print(f"Ignoring character: {character}. Only letters and '.' are processed.")
    
    return ''.join(ciphertext)


if __name__ == "__main__":
    plaintext = input("Enter your plaintext message: ")
    plaintext = plaintext.replace(' ', '').upper()
    while True:
        try:
            key = int(input("Enter the cipher key (0-25): "))
            if not (0 <= key <= 25):
                raise ValueError("Key must be between 0 and 25.")
            break 
        except ValueError as e:
            print(f"Error: {e}. Please enter a valid integer key between 0 and 25.")
    encrypted_message = caesar_cipher_encrypt(plaintext, key)
    print(f"Encrypted message: {encrypted_message}")
Editor is loading...
Leave a Comment