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 a modified version of the Playfair cipher. (For more about the Playfair cipher, see the Wikipedia article.)
The standard Playfair cipher encodes only 25 letters (excluding 'J'), and omits all spacing, punctuation, and capitalization, so that a decoded message lookssomthinglikethis, but it's easy for a person to construct the cipher matrix. Our version will require a computer, or will require the human to memorize or look up the ASCII sequence of characters, but decoding will be almost perfect.
Name your files playfair.py and playfair_test.py. You may have additional Python files, if you wish.
Here's how to construct a modified Playfair cipher.
"Barack H. Obama" (not including the quotes)"Barack H. Obama"becomes"Barck H.Obm" .) | The matrix you are working with | The list representation of the matrix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
| The matrix you are working with | The list representation of the matrix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Programming notes:
chr(integer) will convert from integers to characters, while ord(character) goes the other way. Your program needs to fill in the matrix with the characters of the secret phrase and the character chr(0)=NUL, chr(9)='\t'=TAB, chr(10)=LF, chr(11)=VT, and chr(13)=CR, and after that you can use loops and chr to fill in the remaining characters.'\n', because different operating systems translate that in different ways. It may totally mess things up if, for example, you use Linux to encode and Windows to decode.Here's how to encode a message, using the matrix:
NUL as the second character. NUL instead of the second character.
(The second character will be the first character of the next group of two.) NUL, skip one of them (and use the next character along as the second letter) This is highly unlikely to happen, but we should allow for it.Bu become mB, while ab becomes rB. ht become ih,
while av becomes NULa. as becomes Oi. encode function. Example:
Programming in Python is fun!! # message to be enciphered
Pr og ra mNUL mi ng spacei nspace Py th on spacei sspace fu n! !NUL # characters two at a time
Gb t` cr NULTAB NULh t_ ao ok J~ hi po ao oO [} pVT "TAB # encoded two at a time
Gbt`crNULTABNULht_aookJ~hipoaooO[}pVT"TAB # enciphered message
Here's how to decode a message, using the matrix:
NUL characters. Example:
Gbt`crNULTABNULht_aookJ~hipoaooO[}pVT"TAB # enciphered message
Gb t` cr NULTAB NULh t_ ao ok J~ hi po ao oO [} pVT "TAB # pairs of characters
Pr og ra mNUL mi ng spacei nspace Py th on spacei sspace fu n! !NUL # decoded two at a time
Programming in Python is fun!! # deciphered message, after removing NULs
Using Test-Driven Design, create a program named playfair.py. It should have at least these two methods:
def encode(plainTextMessage, secretPhrase)chr(32) to chr(126), plus LF, etc.).def decode(encodedMessage, secretPhrase)NUL should be discarded.A main method is not required (the above functions can be called directly from the REPL), but you can write one if you wish; use the same if __name__ trick as in the other assignments.
Every method you write should have one or more unit tests, and none of them should do any I/O. [Possible exception: A main method is not required (the above functions can be called directly from the REPL), but you can write one if you wish; use the same if __name__ trick as in the other assignments.]
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 = 'Programming in Python is fun!!'
message2 = 'Amazingly few discotheques provide jukeboxes.'
secretPhrase = 'Barack H. Obama'
self.assertEquals('Gbt`cr\x00\t\x00ht_aookJ~hipoaooO[}p\x0b"\t',
playfair.encode(message1, secretPhrase))
self.assertEquals(':" vjo^tzkgfzr\\plOphq[h|fqHo javefroyBg.lzfqa"',
playfair.encode(message2, secretPhrase))
secretPhrase = "George Herbert Walker Bush"
self.assertEquals("MHr GB|&njftszitN{H\ttcsz$sy#c'&k",
playfair.encode(message1, secretPhrase))
self.assertEquals('Efsqjpeuzg`gzofjkirWlHya lWigrz``rHisBotrw l0s',
playfair.encode(message2, secretPhrase))
def testDecode(self):
message1 = 'Programming in Python is fun!!'
message2 = 'Amazingly few discotheques provide jukeboxes.'
secretPhrase = 'Barack H. Obama'
self.assertEquals(message1,
playfair.decode('Gbt`cr\x00\t\x00ht_aookJ~hipoaooO[}p\x0b"\t',
secretPhrase))
self.assertEquals(message2,
playfair.decode(':" vjo^tzkgfzr\\plOphq[h|fqHo javefroyBg.lzfqa"',
secretPhrase))
secretPhrase = "George Herbert Walker Bush"
self.assertEquals(message1,
playfair.decode("MHr GB|&njftszitN{H\ttcsz$sy#c'&k",
secretPhrase))
self.assertEquals(message2,
playfair.decode('Efsqjpeuzg`gzofjkirWlHya lWigrz``rHisBotrw l0s',
secretPhrase))
unittest.main()
.rar) and submit your .py files to Canvas. No other form of submission will be accepted.