﻿

# Caesar Shift Ciphers

Over the years there have been various attempt to encipher secret messages so that other people can't read them. One of the simplest methods is called the caesar shift cipher - it involves looking at each letter of the message and swapping it for another letter that is higher or lower in the alphabet by a fixed amount. For example, if you enciphered the word hello by swapping each letter for the letter three higher in the alphabet, you'd get khoor. To decode a message, all you need to know is the offset - i.e. how many letters up or down the alphabet the sender shifted the original message - you could then turn khoor back into hello.

## Commands

Here is a reminder of the commands you might need for this task.

• input - e.g. name = input("What is your name?) will ask the question and store the answer in the variable called name
• for - repeats a section of your program a number of times - e.g. for n in range(10) will repeat 10 times, with n becoming each of the values from 0 to 9 in turn. If you don't want to start at 0 or count up in 1s you can use two or three arguments for range(), e.g. for n in range(-5,6) would count from -5 to 5, and for n in range(0,101,10) would count from 0 to 100 in tens.  To count down, make the third argument negative.  NB. You don't need to use n - it's just a programmers habit, like using x in algebra; any valid variable name will do.

You can also use for with any "iterable" (e.g. a string or list), e.g. for char in "Hello" would make the variable char become each letter in the word Hello in turn.
• len - returns the number of characters in a fixed or string variable, e.g. len("Hello") returns the value 5.
• % - does a division and gives you the remainder, e.g. 23 % 10 would return the result 3. This useful for cycling through a fixed range of numbers, e.g. x % 10 would always give you a value in the range 0 - 9 (because a number that's divisible by 10 would give a remainder of zero).
• string-slicing - returns a given number of characters from a string, starting at a specified character, e.g. "Hello"[1,3] would return two characters from the string Hello, starting at the second character (which is character 1 because Python starts counting at zero).
• ord - gives you the ASCII code equivalent of a character, e.g. ord("A") would return the value 65.
• chr - gives you the character represented by an ASCII code, e.g. chr(65) would return the character A.

Read about Caesar Shift Ciphers in Wikipedia or try the substitution cipher page on this site.

If you are interested in cryptography, you could also read about the Enigma machine and try my Enigma machine simulator.

Your task is to produce a program to encipher or decipher text using the caesar shift cipher method. Your program should:

1. ask the user for some text and an offset - i.e. the number of letters by which to shift the message
2. encipher the message using the offset
3. be clear and easy to use

The minimum output is the enciphered/deciphered message, but you can add extra text to make your program easier to understand. Don't forget that you can use your program to encipher and decipher messages, e.g. if you use an offset of 3 to code your text, you can use an offset of -3 to decode it.

### Extension

See if you can get the alphabet to "wrap-around", e.g. if you encipher xyz with an offset of 1 you'll get yza, or if you encipher abc with an offset of -1 you'll get zab. Check also that your program treats upper and lower case correctly.

In the version of the program that I wrote when creating this assignment, only letters are enciphered, and spaces and punctuation are unchanged. Do you think that that's a good idea? Think about the benefits and disadvantages.

### Challenge

See if you can use your finished program to decode the following message: Tbii alkb - buzbiibkq moldoxjjfkd tloh! (remember that my program only changed the letters).