CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 26 - Hacking Secret Ciphers and Test Review


As usual, create two directories for today's class. Create a directory called lecture26 under activities, and a directory called lab26 under labs.


Randomizer!

Time to see what fate has in store for your quiz submission! Including activities from Lectures 23, 24, and 25.


Lab Activity 1

Test Review

Answer the questions on the handout provided. Once you have completed the on paper portion of the review, continue on to answering the following programming questions.


Hacking Secret Ciphers (With Python!)

On Wednesday, we saw how easy it was to break the Caesar cipher. While the substitution cipher is much more complex to break than Caesar, it still isn't unbreakable. As a matter of fact, We were able to to it pretty easily the first day of class. Today, I'll describe a formal way to go about breaking the substitution cipher.


Lab Assignment 26

Frequency "Analysis"

In order to break the Substitution Cipher you wrote on Wednesday, we need to be able to figure out which character is the most frequently occuring in the ciphertext. You can easily get a count of how often a specific character occurs using the count method that the String class provides.

Consider the following ciphertext:

uikjloweiriouhgoisndfglkxcjnbpoiuhqouyhdalkfkdjhnfakehwoaliundf

What is the most common character in this seemingly random sequence of characters? Your process is likely: Count all the a's, count all the b's, count all the c's, ... then, figure out which one was the highest. However, you can't store some arbitrary number of counts. So, you should keep track of the letter with the highest count, and update that letter when you find one of a higher count.

In a file called frequency_analysis.py, write a function called get_maximum_character. This function should take a single parameter: a string of lowercase, alphabetic characters. It should return the lowercase, alphabetic character that occured most often in the input string.

Don't forget to test your function! Make sure that you sanely handle the case where multiple letters have the same counts.

Challenge

The function above will give us the likely candidate that was substituted for the letter 'e'. However, since the substitution cipher is non-linear, we still need to figure out candidates for the rest of the substitutions.

We could follow the same process to find these likely substitutions. Just find the letter with the second most occurances, and that is likely the substituion for 't', the third most is the likely substituion for 'a', and so on.

Alter your program so that it prints ALL of the letters, ordered by their occurances. You should use your get_maximum_character function here. However, you will have to modify the input to that function as you find likely matches.


Submission

When you have finished, create a tar file of your lab26 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab26.tgz lab26/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 26. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Fri Nov 1 10:47:45 EDT 2013