Caesar Codes 2
Monoalphabetic ciphers
Mike May, S. J., 1998
(Worksheet 3 for Cryptography and coding theory)
As always, we start by having our standard message ready.
>
message1 :=`AZaz Good morning Mr. Phelps. Your mission for today, should you choose to accept it, is to encode this message. AZaz`;
temp[1] := convert(message1, bytes);
Recall the procedures we defined in the last worksheet:
>
caesarb := proc(letter, key)
#This procedure changes one letter with a ceasar code
local temp, temp2;
temp := letter:
if temp > 64 then
if temp < 91 then
temp := 65 + ((temp - 65 + key) mod 26):
fi:fi:
if temp > 96 then
if temp < 123 then
temp := 97 + ((temp - 97 + key) mod 26):
fi:fi:
temp:
end:
encodecaesarb := proc(message, key)
#This procedure uses ceasarb to uncode a string
local temp:
#first convert the message to numerical equivalents
temp[1] := convert(message, bytes):
#then add the key to each letter to scrable the letters
temp[2] := map(caesarb, temp[1], key):
#then convert back to the ASCII code
convert(temp[2], bytes);
end:
breakcaesarb := proc(message)
#This procedure uses encodeceasarb to try all the keys on a given message.
local temp, key;
for key from 1 to 25 do
temp := encodecaesarb(message, - key):
print(`The key of `||key,` produces - `,temp);
od;
end:
We would like to modify them so that we can use another monoalphabetic cipher rather than the simple Caesar cipher. To do that we would like the addition of the key mod 26 to be pulled out into a separate procedure. We first give the procedure that that we would feed the whol message into.
>
monoalph := proc(letter, rule, key)
#This procedure changes one letter with a monoalphabetic cipher named rule
local temp, temp2;
temp := letter:
if letter > 64 then
if letter < 91 then
temp := 64 + rule(temp - 64, key):
fi:fi:
if letter > 96 then
if letter < 123 then
temp := 96 + rule(temp - 96, key):
fi:fi:
temp:
end:
encodemonoalph := proc(message, rule, key)
#This procedure uses monoalph to uncode a string
local temp:
#first convert the message to numerical equivalents
temp[1] := convert(message, bytes):
#then uses monalph to scrable the letters
temp[2] := map(monoalph, temp[1], rule, key):
#then convert back to the ASCII code
convert(temp[2], bytes):
end:
Now we produce the rule for the Caesar cipher.
>
caesarrule := (letter, key) -> ((letter + key - 1) mod 26) + 1:
#This procedure is the rule used for the Caesar cipher.
Exercise
1) The rule given for the Caesar cipher seems needlessly complicated. Explain why we dont simplify the rule to:
> caesarrule := (letter, key) -> (letter + key) mod 26;
>
The test of the code is obviously that it correctly encodes and decodes a message.
>
mess[1] := encodemonoalph(message1, caesarrule, 5);
mess[2] := encodemonoalph(mess[1], caesarrule, -5);
Notice that if the encoding key is 5 we can use -5 as a decoding key.
Exercise
2) Type in the first line of your favorite poem as message2. Use the modified coded to encode it with your favorite key, saving the result as message3. Decode message3 to verify the process works.
>
The advantage of the modified approach is that we can set a new cipher by defining a new coding rule. The simplest rule is a scrambling rule where the key is a vector of length 26 whose entries are the numbers from 1 through 26 in some order.
>
scramblerule := (letter, key) -> key[letter];
Consider the following key which switches pairs of letters.
>
pairkey := [2,1,4,3,6,5,8,7,10,9,12,11,14,13,16,15,18,17,20,19,22,21,24,23,26,25];
encodemonoalph(message1,scramblerule,pairkey);
Exercises:
3) Write a rule that encodes by "inverting the letters", i.e., taking a to z, b to y, and so on.
>
4) Use the inversion rule to encode message2. Then apply the rule again to decode the message.
>
5) Write a rule that encodes to the alphabet arranged with the letters used in your name first, followed by the rest of the alphabet in reverse order.
>
6) Use the scrambling rule to encode message2.
>
7) Produce a rule to undo the scrabling rule of the previous exercise. Verify that it works.
>
8) Produce a rule to "multiply letters by key" for a key that is given as a parameter at time of execution.
>
9) Apply your rule to the alphabet with key taking the values of 3, 5, and 7,
>
10) Give the key values that undo the multiplicative rule for 3, 5, and 7.
>
Print a copy of thus worksheet for yourself. Then submit this worksheet electronically.