Caesar Codes 2
Monoalphabetic ciphers
İMike May, S. J., 2002
(Worksheet 2 for Cryptography)
| > | restart: |
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); |
Redoing Caesar to make it more robust
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 we would feed the whole 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 := 65 + rule(temp - 65, key): fi:fi: if letter > 96 then if letter < 123 then temp := 97 + rule(temp - 97, 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.
(In producing our coding rules we send a to 0, b to 1, and so on, up to z to 25.)
| > | caesarrule := (letter, key) -> ((letter + key) mod 26): #This procedure is the rule used for the Caesar cipher. |
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:
1) 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.
| > |
General monoalphabetic ciphers
The advantage of the modified approach is that we can set a new cipher by defining a new coding rule. One of the simplest rules is one that permutes the letters. The key is then a vector of length 26 whose entries are the numbers from 0 through 25 in some order. Rules of this sort permute the 26 letters
| > | permutationRule := (letter, key) -> key[letter+1]; |
Consider the following key which causes the permutationRule to switch pairs of letters. (A goes to B and B goes to A, C goes to D and D goes to C, etc.)
| > | pairkey := [1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14,17,16,19,18,21,20,23,22,25,24]; code1 := encodemonoalph(message1,permutationRule,pairkey); |
This key has the feature that the key to encode is the same as the key to decode.
| > | code2 := encodemonoalph(code1, permutationRule, pairkey); |
Exercises:
2) Write a key that causes the permutationRule to "invert the letters", i.e., take a to z, b to y, and so on.
| > |
3) Use the inversion rule to encode message2. Then apply the rule again to decode the message.
| > |
4) Write a key that permutes the alphabet to the alphabet arranged with the letters used in your name first, followed by the rest of the alphabet in reverse order.
| > |
5) Use the key from exercise 4 to encode message2.
| > |
6) Produce a rule to undo the scrambling key of the previous exercise. Verify that it works.
| > |
7) Can all permutation rules be undone by another permutations? Either produce a counterexample or explain how to produce the rule that undoes the permutation.
| > |
Another kind of rule we can easily think of is a multiplication rule that multiplies the letters by some key mod 26. We can also produce a linear rule that multiplies by a first key and adds a second key mod 26.
Exercises:
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.
| > |
11) What multiplicative keys cannot be undone? Give an example of what happens if we try to use one of these keys.
| > |
12) Produce a rule to "multiply letters by the first key, then adds the second key" with the two keys given as parameters at the time of execution. Show that your rule works with a nontrivial key, and that is then undone by using another key.
| > |
13) Explain how to produce the key to undo in the previous exercise.
| > |