You are on page 1of 5

Melbourne School of Engineering Engineering Systems Design 2 Assignment Six (Due in Workshop 7)

Programming: Data Processing and Encryption


The maximum number of marks for this assignment is 40. It will contribute towards 2% of your assessment in this subject. In this assignment you will develop a series of functions that perform encryption and decryption of text using various methods that have been in use in history. There is a script le the_secret_code.m that will be needed for Question 3 (b). It is available on the LMS under the Assignments Programming Assignments Assignment 6 Assignment 6 MATLAB les link. Your submission must include a hard-copy of your code. Ensure that the code is well commented - your comments should be detailed enough to show your understanding of how your functions work and this forms part of how you will be marked for this assignment. You will also be marked on the neatness and eciency of your code (i.e. using loops where appropriate). Be sure to include any screen shots that are requested in each question. 1. [4 marks in total] One of the simplest methods used for encrypting a message in medieval times was to use mirror writing 1 . Coded messages are written in a mirror image so that when seen in a mirror they appear decoded - similarly to how the word AMBULANCE is written in a mirror image to appear the correct way when seen in the rear-view mirror of a car. Mirror writing cant be easily done in MATLAB, so you will instead emulate it by simply coding a message in reverse. Write a function reverse_cipher(message) that takes a plaintext message stored in a string message and prints the message encrypted by reversing it in the same format as below. Note : You must NOT use the fliplr function. Show a screenshot of your function working correctly by replicating the following encryption >> reverse_cipher(Leonardo da Vinci);
1

The notes on Leonardo da Vincis famous Vitruvian Man image are actually in mirror writing.

The coded message is : icniV ad odranoeL 2. [16 marks in total] Mirror or reverse writing (see Question 1) can be fairly easy to decode - in fact, most people can do it on sight (which doesnt make it a very secret code!). A more advanced way of enciphering a message is to use the Caesar cipher 2 . The Caesar cipher is a type of monoalphabetic substitution cipher in which each letter in the message is replaced by a letter some xed number of positions down the alphabet. For example, with a shift of three, a would be replaced by d, b would become e, and so on; the end of the alphabet wraps around to the start. Below is the translation of each plain message character to a cipher character for a shift of three: Plain: Cipher: abcdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyzabc

Coded messages are then formed by taking each character from the original message, looking up its cipher version and inserting it into the coded message. Plaintext: the quick brown fox jumps over the lazy dog Ciphertext: wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj (a) Draw a owchart to represent an algorithm describing a function caesar_cipher(message,shift) that takes a plaintext message of lowercase alphabetical characters and spaces stored in a string message, and shift, a value for the amount to shift each character by and returns the message encrypted using the Caesar cipher in a string. Space characters should REMAIN UNTOUCHED (as per the example above). (b) Write the function caesar_cipher(message,shift) using your owchart from part (a) to guide you. Show a screenshot of your function encoding the following message using the sum of the last digit of each group members student number as the value of the shift : the quick brown fox jumps over the lazy dog For example, with student numbers 12345, 56789 and 54321 in your group, you should use 5 + 9 + 1 = 15 as the shift amount. If you have a shift amounts greater than or equal to 26 you should subtract 25 from the amount. Hints : The MATLAB functions char and double may be useful. Also an ASCII code table may come in handy.
The Caesar cipher is named after Julius Caesar, who apparently used it with a shift of three to protect messages of military signicance.
2

(c) While being rather eective for its time, the Caesar cipher is relatively easy to break using modern computers. Two methods of breaking the cipher are Frequency analysis - where the frequency of each letter appearing in a coded message is compared with average values for the frequency of appearance of letters in typical English text. For instance, if g was the most commonly used character in a Caesar cipher coded message, it would be a reasonable assumption that it represents the original message letter e 3 . This implies a Caesar shift of 2 which can be applied to the message to test if the decoded message makes sense with a shift of 2. Brute force - where all 25 shifts are tried and printed out and the user looks at each decoded message to determine the shift used. Write a function caesar_break(ciphertext) that applies the brute force method to a lowercase message coded using the Caesar cipher and stored in the string ciphertext to print out all 25 possibilities of the shift value so that a user can easily determine the shift used in the encoding process. Your output should match the formatting below : >> ciphertext = mz qjmybxq omqemd oubtqd; >> caesar_break(ciphertext) Shift | Decoded Text ------|------------------------------------------------1 | ly pilxawp nlpdlc ntaspc 2 | kx ohkwzvo mkockb mszrob 3 | jw ngjvyun ljnbja lryqna . 12 | an example caesar cipher . 25 | na rknzcyr pnrfne pvcure where not all the shifts have been shown here to save space. Note : Your function DOES NOT need to nd the actual shift value. Use the following text below as input to your function and show a screenshot of the output it produces. mabl vbiaxk bl ptr mhh xtlr mh ukxtd What is the shift value that was used to encode the message? What is the original message?
Based on a collection of over 100, 000 characters taken from various novels and newspapers, the letter e comprises around 12.7% of characters, with the next highest being t with 9.1% and a with 8.2%.
3

3. [20 marks in total] As was explained in Question 2, monoalphabetic ciphers are prone to attack by two methods - frequency analysis and brute force. One way of reducing the vulnerability of a cipher to a frequency analysis attack is to use a homophonic substitution cipher. Basically, each message character is replaced with a variety of substitutes, the number of potential substitutes being proportional to the frequency of the letter. Oering several substitution options for popular characters helps balance out the frequencies of symbols in the coded message - thus making breaking via frequency analysis more dicult. As we are now using multiple cipher characters for a single message character, we will need to expand our cipher alphabet - in this case we will use numbers to represent the message characters. An example of a homophonic substitution cipher is given in Table 1. The rst column represents each character from the plain message alphabet, while the numbers in each row represent the cipher alphabet for each character, with several options for frequently occurring characters. (a) Write a function homophonic_cipher(message) that takes a plaintext lowercase message stored in a string message, and returns the message encrypted using the homophonic substitution cipher in Table 1 as a row vector of integers. Note : You need to ensure that you cycle through the alternative symbols in the order from the table for a message character that appears several times. For instance, the message uuuu should be encoded as [8 61 63 8]. Show a screenshot of your function encoding the following message : the attack will begin at dawn. make sure all forces are fully prepared. Hint: Dene the symbol table in Table 1 as a 28 10 matrix, where blank entries are marked as a 1. (b) Write a function homophonic_decipher(ciphertext) that takes a coded lowercase message stored in a row vector of integers ciphertext, and returns the decrypted message as a string using the homophonic substitution cipher from Table 1 to decipher. Download the script le the_secret_code.m from the LMS and put it into the same directory as your homophonic_decipher function. This script contains a secret message and when run will use your homophonic_decipher function to decipher it line by line and print it to the screen. Show a screenshot of the output of the script the_secret_code.m - i.e. the decoded secret message. What does the message say?

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 space .

9 12 33 47 53 67 78 48 81 13 41 62 1 3 45 79 14 16 24 44 46 55 57 10 31 6 25 23 39 50 56 65 68 32 70 73 83 88 93 15 4 26 37 51 84 22 27 18 58 59 66 71 91 0 5 7 54 72 90 99 38 95 94 29 35 40 42 77 80 11 19 36 76 86 96 17 20 30 43 49 69 75 8 61 63 34 60 89 28 21 52 2 92 98 97 87

64

74

82

85

Table 1: Symbol table for homophonic substitution cipher.

You might also like