Python track: lab 3: fun with cryptography


This lab, you're going to be implementing a very rudimentary cryptography program in python. Your program will have to generate a random substitution code for all the letters in the range a-z, mapping them to other letters in a-z such that the letters can be translated uniquely back to the original ones. All other characters will be passed through unchanged. Your program will use this code to translate a text file (which you supply, though it should work correctly regardless of the contents of the file) and then translate it back into the original form. The point is not so much to do world-class cryptography, but rather to give you some more experience working with objects and modules in python.

We are supplying you with a test script called test_crypto. Note: this will only work on a Unix system, so please use a Unix system to develop this (or at least to test it).

You have to write two files:

  1. char_translator.py   This file will contain code to generate the random substitution code and to translate and untranslate individual characters based on that code. NOTE: python now contains the string methods string.maketrans() and string.translate() which pretty much do this for you. So don't use either of these methods, or the problem becomes totally trivial.

  2. crypto.py   This file will do the actual translation and untranslation of files.

Here are the classes and methods that should be in each file. Feel free to add more classes or methods if it's convenient for you. In the file "char_translator.py", you should have:

In the file "crypto.py", you should have a class called Coder which has these methods:

Once you've developed your code, run the file test_crypto with some file name as its argument. If your modules are correct the program will encode the file, decode it, and test to see if the decoded version is the same as the encoded version. Try to make sure that your test file has all the letters from a-z and A-Z as well as some non-alphabetic characters. Program source code is a good input to use.


Hints

This is an easy lab, so I expect perfect programs ;-) Here are a few things to keep in mind while you write this program.