# Python Casear Cipher Functions # By Eliot Ball 05/11/2010 # Function to encrypt a message using the Casear Cipher with a specific shift # Takes in a string Plaintext and an integer Shift as arguments # Returns a string containing the ciphertext def CaesarEncrypt(Plaintext, Shift): # Convert the Plaintext to upper case Plaintext = Plaintext.upper() # Start with a blank ciphertext Ciphertext = "" # Load the alphabet into a variable ready to use for shifting Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Iterate through each character in the plaintext for Character in Plaintext: # Check for this being a letter (it appears in the alphabet) if Alphabet.find(Character) != -1: # Find the position in the alphabet of this letter, add the shift to that, # find the remainder when dividing by 26 and take that letter from the # alphabet and append it to the ciphertext Ciphertext += Alphabet[(Alphabet.find(Character) + Shift) % len(Alphabet)] # Otherwise it is not a letter (ie a space or punctuation etc) else: # Just append the character as it is to the ciphertext Ciphertext += Character # Return the ciphertext return Ciphertext # Function to decrypt a message using the Caesar Cipher with a specific shift # Takes in a string Ciphertext and an integer Shift as arguents # Returns a string containing the plaintext # Algorithm is identical to CasearEncrypt but the shift value is subtracted # rather than added def CaesarDecrypt(Ciphertext, Shift): Ciphertext = Ciphertext.upper() Plaintext = "" Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for Character in Ciphertext: if Alphabet.find(Character) != -1: Plaintext += Alphabet[(Alphabet.find(Character) - Shift) % len(Alphabet)] else: Plaintext += Character return Plaintext