CIT 591 Assignment 3: Playfair Cipher
Fall 2009, David Matuszek
unittest and doing TDD (Test Driven Development) You are probably familiar with the idea of "secret codes," such as the cryptograms published in some newspapers. A cipher is a more secure way of making secret messages. In this assignment you will write a program to encipher and decipher secret messages according to the Playfair cipher. (For more about the Playfair cipher, see the Wikipedia article.)
Here's how to construct a Playfair cipher.
university. j occurs in the keyword, change it to i. (So university is not changed.) university becomes universty.) | The array you are working with | The list representation of the array | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
j.
| The array you are working with | The list representation of the array | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Here's how to encode a message, using the array:
j to i.
x as the second letter. x instead of the second letter.
(The second letter will be the first letter of the next group of two.) x, skip one of them (and use the next letter along as the second letter).id become tl, while up becomes ru. st become ty,
while lo becomes mh. sm becomes yk. Example:
Programming in Python is fun! # message to be enciphered
programminginpythonisfun # lowercased, no j's, no non-letters
pr og ra mx mi ng in py th on is fu nx # letters two at a time
ub zo sr xv lv ec vi xr rl ke nt bv vq # encoded two at a time
ubzosrxvlvecvixrrlkentbvvq # enciphered message
Here's how to decode a message, using the array:
Example:
ubzosrxvlvecvixrrlkentbvvq # enciphered message
ub zo sr xv lv ec vi xr rl ke nt bv vq
pr og ra mx mi ng in py th on is fu nx
programxminginpythonisfunx # the best we can do at deciphering it
This is as far as you can get in decoding the message. The message is readable, with a little effort on the human's part.
Using Test-Driven Design, create a program named playfair.py. It should have at least these two methods:
def encode(plainTextMessage, secretPhrase)def decode(encodedMessage, secretPhrase)Every method you write should have a unit test. I have two "top level" tests for you; these will be the last tests you will be able to pass, since they depend on everything else working perfectly.
import playfair import unittest class TestPlayfair(unittest.TestCase): def testEncode(self): message1 = 'Test message ONE.' message2 = 'Boo, Exxon Oil!' secretPhrase = 'abc' self.assertEquals('udtupbxcqckbpocz', playfair.encode(message1, secretPhrase)) self.assertEquals('dmpdynopfo', playfair.encode(message2, secretPhrase)) secretPhrase = "Amazingly Few Discotheques Provide Jukeboxes!" self.assertEquals('pgwqnlokwzlgpeon', playfair.encode(message1, secretPhrase)) self.assertEquals('xcponpepmf', playfair.encode(message2, secretPhrase)) def testDecode(self): message1 = 'Test message ONE.' message2 = 'Boo, Exxon Oil!' secretPhrase = 'abc' self.assertEquals('testmesxsageonex', playfair.decode('udtupbxcqckbpocz', secretPhrase)) self.assertEquals('booexonoil', playfair.decode('dmpdynopfo', secretPhrase)) secretPhrase = "Amazingly Few Discotheques Provide Jukeboxes!" self.assertEquals('testmesxsageonex', playfair.decode('pgwqnlokwzlgpeon', secretPhrase)) self.assertEquals('booexonoil', playfair.decode('xcponpepmf', secretPhrase)) unittest.main()