First Bytes at the Honors Colloquium
Caesar Cipher Lab - Part 1
Introduction: In this lab activity you will use MatLab and a function to decrypt a message that is encoded with a Caesar cipher with a shift of 5.
This lab is available at www.cs.utexas.edu/users/scottm/FirstBytes/caesarLabPart1.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 you know the following changes are made when a message is encrypted .
First, if there is a character in the original message that is not a letter it is a letter it is removed. This makes the message harder to read, but also makes it harder to break.
Second, any lower case letters in the original message are converted to upper case and encrypted as an upper case letter. This simplification is so you don't have to differentiate between upper and lower case letters
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 converted to T then Y
the space is removed
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
! is removed
So the encrypted message is KNWXYGDYJX
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:
TQNANFNXSNSJ
In this part of the lab you know the message has been encoded with a shift of 5.
Approach: Write a function that takes in one input, the encoded
message. A possible header for the function would be:
function result = shift5(message)
Create a result the same length as the message. The result
should be the same length as the incoming message. You can obtain the length
of a string or array variable using the length message.
length(someVariable)
returns the length of someVariable
To create a place to store the result use the zeros
function. This creates an array of the specified size
result = zeros(1,12)
would create an array of 12 zeros
result = zeros( 1, length(message) )
would create an array of zeros equal to the length of message.
Loop through every character in the message
For each element of the message subtract 5 from each
character in the original message (more specifically the number representing
that represents that character.)
The ASCII code for 'A' is 65. The ASCII code for 'Z' is 90. We are undoing a
shift of 5 on an encoded character that is a 'Z' when get the following.
'Z' = 90
'Z' - 5 = 85
char(85) = 'U'
But when undoing an encoded 'A' there is a problem
'A' = 65
'A' - 5 = 60
char(60) = '<'
Shifting 'A' back by 5 should result in 'V' not '<'!
This means there is a decision to make. If the result of the shift is less than
65 then we must add 26 to the result before we get the character.
'A' = 65
'A' - 5 = 60
60 < 90
60 + 26 = 86
86 = 'V'
Excellent!
Place the character in the correct spot in the result. (Same as the loop control variable)
When all done convert the result back to chars. This can be
done with the chars function.
result = chars(result)
would convert result from numbers to characters.
Encoded the algorithm described above yields the following MatLab function:
function result = shift5(message)
% decodes a message encoded with a Caesar shift
% cipher of 5. Assumes message is all upper
% case letters.
len = length(message);
result = zeros(1,len);
temp = ' ';
for ch = 1:len
temp = message(ch) - 5;
if (temp < 65)
temp = temp + 26;
end
result(ch) = temp;
end
result = char(result);
This function should be in the work directory already. If not you can download it from the First Bytes Web site.
www.cs.utexas.edu/users/scottm/FirstBytes/
The message for this lab is
'YMJWJSJAJWBNQQGJHTRUQJYJJVZFQNYDZSYNQBTRJSYMJRXJQAJXMJQUYTRFPJQFBXFSIJQJHYQFBRFPJWXXZXFSGFSYMTSD'
You can download this from the web at
www.cs.utexas.edu/users/scottm/FirstBytes/caesar1.txt
You could open the above file in a web browser, copy it, then paste it into your MatLab command window.