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 24 - Simple Encryption


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


Lab Activity 1

Character Transpositions

One possible way to create a word puzzle is to take the characters of the word, and re-arrange them in some random fashion. This is known as a transposition cipher, and is typically used to create word jumbles that you have likely seen either in a newspaper (or more likely on a newspapers website) or at the movies, during the pre-roll they show before the previews. Today, you will write a function that can perform such a transposition on an input word.

Create a file called transpose.py in your lecture24 directory. In this file, you will write a function called transpose_characters, which takes a single parameter: a string of lowercase characters. Your function should also return a string. This returned string should contain the same characters from the input string, but in some random order. Recall that you can use the string slicing syntax (some_string[start : exclusive_end]) as a mechanism to remove a specific charater from a string, as demonstrated in class on Friday.

For example, if the input to transpose_characters is "williams", your function might return "lialimws" or "smailliw". It should never return "llllllll", or even "wiliamsz", since they do not contain the exact same characters (some characters are missing, or there are extra characters).


Lab Assignment 21

rot13

Cryptography is the science of hiding secrets. A typical cryptographic protocol takes some plaintext message P and some key k, and produces a ciphertext C that is unreadable by any entity that does not know the key k

One very easy (and surprisingly incredibly old) cryptographic protocol is know as rot13. In the early days of the Internet, rot13 was used to hide solutions to puzzles posted on message boards. rot13 is very simplistic, and is considered the Internet's version of printing the answers upside down on the same page.

The key in rot13 is always 13. To encrypt, you chose the letter 13 spaces further in the alphabet of the entered character. For example, the encryption of the character 'a' is the character 'n'. The end of the alphabet wraps around to the beginning of the alphabet. Thus, the character 'z' is actually encoded as 'm'. Recall that you can use the modulus (A.K.A. remainder, the % operator) to cause this cyclic process to occur. The nice thing about rot13 is that decryption is the same process, so you do not need to write a separate decrypt function.

Write a program rot13.py which reads a sentence from the user, and prints the rot13 encryption of the sentence the user typed in. Your program should only encode the lower-case alphabetic characters of the typed sentence. So, for example, "hello world!" would become "uryyb jbeyq!".

While there are no required functions for this assignment, you should still use functions where appropriate.

Make sure you test your program well. How many test cases do you need? Include your test cases in your program file. Comment them appropriately.

Challenge

As mentioned above, rot13 is a version of a very old cipher known as the Caesar cipher. This was obviously named after Juluis Caesar, who used 3 as his key. However, any integer in the range [0, 25] works as a key in the Caesar cipher. Unfortunately, for any key other than 13, you need a separate decryption function, which subtracts the key from each ciphertext character.

Alter your program so that it first reads a key from the user, and uses that key for encryption. You should also write a decryption function, and ask the user before they enter in text whether they are encrypting or decrypting.


Submission

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

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

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 24. 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: Sat Oct 26 22:28:07 EDT 2013