Part 1: Caesar Cipher
Suppose you want to send a message to a friend of yours. However, you have a problem: you can not be sure that some bad guy would not read the message. So you want to figure out some way of “scrambling” your message, so that it can be understood only by your friend. This is the main idea of ciphering a message. One way to do that is by using Caesar Cipher. Caesar Cipher is also known as the shift cipher. It is a type of substitution cipher in which each letter in the text message is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on.
Ciphering Example:
>>> caesarCipher("AYMRBSUQ",3)
'DBPUEVXT'
To understand your scrambled message, your friend has to do the inverse of what you did, that is she has to decipher the message. How can she do that? She needs to know only one thing, that is the fixed number of positions you used to shift the letters of your message.
Deciphering Example:
>>> caesarDecipher('DBPUEVXT',3)
'AYMRBSUQ'
TODO 1.1: Reading text message from a file
You are given the text file “message.txt” that contains the message that you will cipher.
You have to write a function getTextFromFile(filelName) that takes the file name as input parameter, and does the following:
It opens the file for reading.
It reads the file content into a string.
It returns the string that has been created.
TODO 1.2: Cleaning text message from non alphanumeric characters
Write a function stripNonLetters(textMessage) which should do the following:
The function takes a text message as a parameter.
It cleans the text message from non letters.
It returns the cleaned text message.
TODO 1.3: Ciphering text message using Caesar Cipher
Write a function caesarCipher(textMessage, shift) which should do the following:
The function takes a text message and a shift value as parameters.
Convert every letter in the text message into upper case.
Substitute every letter in the text message with its equivalent cipher letter using Caesar Cipher as follows:
Calculate the shifted position as: letter position in alphabet + shift value
Replace the letter with the letter located at the shifted position. Example: with shift = 3, A will be substituted with D and Y will substituted with B
Return the ciphered text message.
Note: Remember that the ASCII value of A is 65 and Z is 90. Therefore, you need to check when the ASCII of the shifted value is greater than 90 so you can tune your code to shift from 90 back to 65.
TODO 1.4: Deciphering text message
Write a function caesarDecipher(cipher, shift) which should do the following:
The function takes text message and shift value as parameters.
Substitute every letter in the ciphered message with its equivalent letter using Caesar Cipher as follows:
Calculate the shifted position as: letter position in alphabet - shift value
Replace the letter with the letter located at the shifted position. Example: with shift = 3, D will be substituted with A and B will substituted with Y
Return the ciphered text message.
Note: Remember that the ASCII value of A is 65 and Z is 90. Therefore, you need to check when the ASCII of the shifted value is less than 65 so you can tune your code to shift from 65 back to 90.
Part 2: Columnar Transposition Cipher
The columnar transposition cipher takes a message and a key (a string of characters) and creates a matrix populated with the message's characters. The number of columns in the matrix is determined by the length of the key. For example, assuming that the message is “this is a top secret message” and the key is “purdue”, the matrix would look like the following:
t h i s i
s a t o
p s e c r
e t m e s
s a g e
Note that the number of columns in the table is determined by the length of the key string. In the example above, the number of columns is six because the number of characters in the key “purdue” is six.
After the message is stored in the table, the ciphertext is created by traversing the columns one after the other from top to bottom, and storing each character in a new string. So in our example, the ciphertext would be:
tspesh taias gs eme tceior
TODO 2.1: Ciphering text using Columnar Transposition
Write a function columnEncipher(msg, key) which does the following:
The function takes a text message and a string of characters (the key) as input parameters.
It calculates the number of the columns of the matrix (see the example above)
It calculates the number of rows of the matrix. Note that the number of rows cannot obtained simply by dividing the length of the message by the length of the key! If you look at the example above, you will see that the message is 28 characters long, and the key is six character long; the number of rows of the matrix is five. We leave to you how to solve this (it is not so difficult!)
It initialize the table elements with a blank string, and then fills the table elements with the characters of the message (see the example above)
It generates the ciphered message by reading the matrix above column after column
It returns the ciphered text message.
TODO 2.2: Deciphering text using Columnar Transposition
Write a function columnDecipher(cipher_text, key) which does the following:
The function takes a cipher text and a string of characters (the key) as input parameters.
It calculates the number_of_columns of the matrix (see the example above)
It calculates the number of rows as you did in TODO 2.1 step 3.
It fills the matrix with the cipher_text by column.
It generates the ciphered message by reading the matrix above column after column.
It returns the text message.
Part 3: Frequency Analysis
In this part of the project, you will perform analysis on ciphered message. You will count how many times each letter appeared in the ciphered message.
TODO 3: Frequency Analysis
Write a function frequencyAnalysis(cipher) which should do the following:
The function take a cipher text as a parameter.
Count how many times every letter appeared in the cipher text. NOTE: a certain letter may appear in the cipher text both in lower case and in upper case. You must not count them separately!
Print the analysis that you calculated in the command area.
The output produced must look like the following one:
>>> frequencyAnalysis2("ABCCRA")
A : 2
B : 1
C : 2
R : 1
Part 4: Gluing it all together
Write a function main() , which does the following,
TODO 4 main() function
Use pickAFile to get the name of the file that contains the text message (message.txt) .
Call getTextFromFile(fileName) to get the text message.
Call stripNonLetters(textMessage) in order to clean the text message and save the result in a variable called cleanText.
Request the user to input the shift value using the requestInteger(message) function.
Call caesarEncipher(message, shift). You must use the cleanText message produced by stripNonLetters as input parameter to this function. Use the integer value entered by the user in step 4 above. You need to store ciphered message produced by caesarEncipher in a variable called ceasarCipher.
Print the ceasarCipher.
Call caesarDecipher(cipher, shift). Use the ceasarCipher and shift you obtained in the previous steps as input arguments to caesarDecipher. You need to store the deciphered message produced by caesarDecipher in a variable called ceasarDec.
Print ceasarDec.
Call columnCipher(message, shift). You must use the cleanText message produced by stripNonLetters as input parameter to this function. Use the integer value entered by the user in step 4 above. You need to store ciphered message produced by caesarEncipher in a variable called columnCipher.
Print the columnCipher.
Call columnDecipher(cipher, shift). Use the ceasarCipher and shift you obtained in the previous steps as input arguments to caesarDecipher. You need to store the deciphered message produced by caesarDecipher in a variable called ceasarDec.
Print columnDec.
Call frequencyAnalysis(cipher) use ceasarCipher as a parameter.