Caesar Cipher in Python
Julius Caesar was a Roman dictator who came up with a clever way of passing confidential information to his generals without having to worry about it falling into the wrong hands. He shifted each letter in the message by a fixed shift value i.e. if the shift value was 3, a became d, f became i and so on. He would leave the numbers and other characters untouched. His generals would only need the shift value to decode their emperor's message. Later on, this cryptography technique came to be known as Caesar Cipher.
You can implement Caesar Cipher in Python using the builtin ord(), chr() functions, a for loop, a couple of if-else constructs.
The ord() function returns the Unicode value of the character supplied to it. It is inverse of builtin chr() function which returns the character in the Unicode character set, denoted by the number provided to it.
Expand the following code snippet for the script. You can run it online here.
## Implement a Caesar Cipher that shifts all the letters in a given message by an given number of places. ## Example of Caesar Cipher: 'Hello' becomes 'Khoor' when characters are shifted by 3 places. 'Can you read this?' becomes 'Kiv gwc zmil bpqa?' when characters are shifted by 8 places. 'Can you read this?' becomes 'Zxk vlr obxa qefp?' when characters are shifted by 3 places in the backwards direction. ## THOUGHT PROCESS: Prompt the user for the message to encrypted and the number of places each character should be shifted by in the encrypted message -> initialize the encrypted message to an empty string -> begin a for loop to process each character in the entered message -> branch the control into three code blocks, to process uppercase, lowercase and non-alphabet characters separately -> for uppercase and lowercase characters, calculate the position of the character by subtracting Unicode value of the first letter i.e. 'a' or 'A' from the Unicode value of the character -> calculate its new positon by adding the shift value and obtaining the remainder on dividing by 26 -> convert the new position into character form using the builtin chr() function -> append the new character to the encrypted message -> for non-alphabet characters, append them to the encrypted message without shifting -> output the encrypted message. ## INPUT message = input("Enter the message you want to encrypt: ") shiftValue = int(input("Enter the places you want the characters to be shifted by: ")) ## PSEUDO CODE / ALGORITHM: # Initialize the newMessage to an empty string # Using a for loop, process each letter in the following way: # Determine whether it's lowercase or uppercase using an if statement, proceed accordingly: # using ord(), determine its position in the alphabet(0-25) # calculate its new offset by adding the shiftValue and getting the remainder on dividing by 26(for taking care of shiftValue > 26) # convert the position to character by using chr() # append the new character to the newMessage # If the character is not an alphabet, append it to newMessage as it is, without shifting. newMessage = "" for character in message: if character >= "a" and character <= "z": position = ord(character) - ord("a") position = (position + shiftValue) % 26 newCharacter = chr(position + ord("a")) newMessage = newMessage + newCharacter elif character >= "A" and character <= "Z": position = ord(character) - ord("A") position = (position + shiftValue) % 26 newCharacter = chr(position + ord("A")) newMessage = newMessage + newCharacter else: newMessage = newMessage + character ## OUTPUTTING RESULT print("\nHere is the encypted message: \n\n" + newMessage)