﻿

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. INPUT "What is your name? "; A\$ will ask the question and store the answer in the variable called A\$

• FOR - repeats a section of your program a number of times - e.g. FOR N = 1 TO 10 will repeat 10 times, counting with N from 1 to 10. If you don't want to count in 1s, you can add STEP - e.g. FOR N = 5 TO 100 STEP 5 (to count up to 100 in 5s) or FOR N = 10 TO 1 STEP -1 (to count from 10 down to 1). You don't need to use N - it's just a programmers habit, like using x in algebra; any valid variable name will do.

• NEXT - goes after FOR, at the end of the section that you want to repeat.

• LEN - returns the number of characters in a fixed or string variable, e.g. LEN("Hello") returns the value 5.

• MOD - does a division and gives you the remainder, e.g. 23 MOD 10 would return the result 3. This useful for cycling through a fixed range of numbers, e.g. x MOD 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).

• MID\$ - returns a given number of characters from a string, starting at a specified character, e.g. MID\$("Hello", 2, 3) would return three characters from the string Hello, starting at the second character.

• ASC - gives you the ASCII code equivalent of a character, e.g. ASC("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).