First Bytes

Caesar Cipher Lab

Introduction: In this lab activity you will use MatLab and write functions to decrypt a message that is encoded with a Caesar cipher.

This lab is available at www.cs.utexas.edu/users/scottm/FirstBytes/caesarLab.htm

A Caesar Cipher  is a very simple method for encoding a message. As in any cipher there is a key that is used to encrypt and decrypt messages. The key in a Caesar cipher is based on a shift. The shift is the number of characters from a clear unencrypted character forward in the alphabet to the encrypted character.

For example if the shift is 5 the key is:

Clear Letter  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Encode Letter F G H I J K L M N O P Q R S T U V W X Y Z A B C D E

So to encrypt a message we just go through characters in the message and substitute the Encode letter. For all the labs this week will be making some simplifying assumptions. 

So to encrypt the message First Bytes! using the cipher above we go through character by character.

F is converted to K
i is converted to I then N
r is converted to R then W
s is converted to S then X
t is coverted to T then Y
the space stays the same
B is converted to G
y is converted to Y then D
t is converted to T then Y
e is converted to E then J
s is converted to S then X
! stays the same

So the encrypted message is KNWXY GDYJX!

To decrypt the message the receiver would need the above key. Any letter is decoded by going back 5 letters in the alphabet.

Use the code above to decode this message:

TQNANF NX 9.


The reason a Caesar cipher is so easy to break is that there are only 25 possible keys. You can only shift forward between 1 and 25 spots so this limits the number of possible keys. If you have a message and you suspect it has been encrypted using a Caesar cipher then you could attempt to break it by trying all 25 shifts, one at a time, until you have a recognizable message. This is a brute force approach well suited to the computer.

You will some standard functions from MatLab as well as some specialized ones.

Function Name Description
loadtext(filename) This is a nonstandard MatLab function that you must load into your work directory to use.

loads all data from the file with name filename. 
Filename is a string. 
Returns a string

Example:
message = loadtext('caesarCoded.txt')

length(X) determines how many elements are in each column of a matrix
X is any variable
returns an integer

Example:
len = length(message)

blanks(num) creates a string consisting of num spaces
num is an integer
returns a string with num spaces

Example:
result = blanks(len)
isletter(ch) tests if ch is an upper or lower case letter
ch is any string
returns true of the first character of ch, ch(1), is an upper or lower case letter, false otherwise

Example:
if( isletter(message(1) )

Approach: Write a function that takes in two inputs. The first is the encoded message, the second is the shift to apply when decoding.

  1. Create a result the same length as the message

  2. Loop through every character in the message

  3. If a character is a letter convert it to a number and shift it forward. Note, all letters in the messages you will be decrypting this week will be upper case letters. 

    The ASCII code for 'A' is 65. The ASCII code for 'Z' is 90. If you are testing a shift of 13 spaces then applying a shift to 'A' is simple:

    'A' = 65
    'A' + 13 = 78
    78 = 'N'

     But if we apply a shift of 13 there is a slight complication. 

    'Z' = 90
    'Z' + 13 = 103
    103 = 'g' 

    Shifting Z by 13 should result in 'M' not 'g'!

    There is a decision to make. If the result of the shift is greater than 90 then we must subtract 26 from the result before we get the character.

    'Z' = 90
    'Z' + 13 = 103
    103 > 90
    90 - 26 = 77
    77 = 'M'

    Excellent!

  4. Convert this number back to a character

  5. Place the character in the result

File Description Link - Download all files to your work folder in MatLab
M file to load data from text files into a String variable. loadtext.m
A small file encoded with a Caesar cipher. smallQuest.txt
Another small file encoded with a Caesar cipher caesarCoded.txt
 The files are also available in the cs303e/firstbytes folder

The Extra Mile: If you only use the approach recommended above you would have to call your function 25 times. This is a bit cumbersome. One way to avoid this and harness the power of the computer is to get the function above working and then write another function that calls the one above 25 times, once for each possible shift.