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.

Related Reading

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

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).

Type the answer into the Comments box when you submit your assignment.