#!/usr/bin/python3 # -*- coding: utf-8 -*- # Caesar Cipher # https://www.nostarch.com/crackingcodes (BSD Licensed) # The string to be encrypted/decrypted: message: str = "All human beings have three lives: public, private, and secret. Gabriel Garcia Marquez" # The encryption/decryption key: key: int = 13 # Whether the program encrypts or decrypts: # Set to either 'encrypt' or 'decrypt'. mode: str = "encrypt" # Every possible symbol that can be encrypted: SYMBOLS: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?." # Stores the encrypted/decrypted form of the message: translated: str = "" for symbol in message: # Note: Only symbols in the `SYMBOLS` string can be encrypted/decrypted. if symbol in SYMBOLS: symbolIndex = SYMBOLS.find(symbol) # Perform encryption/decryption: if mode == "encrypt": translatedIndex = symbolIndex + key elif mode == "decrypt": translatedIndex = symbolIndex - key # Handle wrap-around, if needed: What is this computing? if translatedIndex >= len(SYMBOLS): translatedIndex = translatedIndex - len(SYMBOLS) elif translatedIndex < 0: translatedIndex = translatedIndex + len(SYMBOLS) translated = translated + SYMBOLS[translatedIndex] else: # Append the symbol without encrypting/decrypting: translated = translated + symbol # Output the translated string: print(translated)