To turn your work in, go
to mycourses.unh.edu
,
find CS414, and find the assignment. Then click "Submit
Assignment" to upload your main file,
cryptogram.py
,
plus any other files you have created.
I am providing you with a very minimal skeleton to get you
going:
cryptogram.py
.
I am also providing you with a file with quotes for the game:
quotes.txt
.
Encryption converts readable information (the plaintext) into unreadable information (the cyphertext). There are many encryption algorithms, but the simplest is a substitution cypher. In a substitution cypher, there is a mapping between letters in the plaintext and letters in the cyphertext.
The Caesar cypher is the simplest substitution cypher (Julius Caesar used it). Each letter in the plaintext is replaced with the letter 3 spaces after it in the alphabet:
plaintext letter | cyphertext letter |
---|---|
A | D |
B | E |
C | F |
D | G |
... | ... |
V | Y |
W | Z |
X | A |
Y | B |
Z | C |
BABY WAX
is mapped to the
cyphertext EDEB ZDA
. Notice that in a substitution
cypher a given letter in the plaintext
(e.g., B
) is always replaced the same way
(in this case E
).
Usually substitution cyphers don't simply shift a letter forward in the alphabet, as the caesar cypher does; the replacement from plain letter to cypher letter is usually more arbitrary.
Substitution cyphers are easy to break, if the cyphertext is
long enough, because some letters occur more frequently. All we
need is to count the frequency of letters in the cyphertext, and
make some educated guesses. This is possible because English
uses some letters much more often than others. In fact, the
most common letters in English text are
E
,
T
,
A
,
O
,
I
,
N
, in order of decreasing frequency. Also, some
letter pairs and triples occur very frequently.
For example, if we are given a three-letter
word QXW
, and we have guessed that T
maps to Q
and W
maps to E
, we should then guess that X
maps to H
, because the triple THE
is
very frequent in English text.
Write a program, called cryptogram.py
, which does the
following:
Break your work into parts, using functions. Here are some functions you could include in your program. Notice that many of these functions use a list of single letters, rather than a string. You can't replace individual letters in a string (strings are immutable), but you can replace entries in a list.
main()
: this controls the game, from
beginning to end.
choose_quote()
: this should open a file
called quotes.txt
, and get a list with all its
lines, with one line per list entry. Then, it should pick
one of the lines, at random, and return it.
encrypt(letters)
: this function is passed a
list of letters. It should return a new list, which has the
letters replaced using a substitution cypher. Proceed as
follows:
alphabet = list(string.lowercase)
.
random.shuffle()
to make a scrambled
version of the alphabet.
substitution =
dict()
, and initialize it such
that substitution[letter]
is
the letter
's replacement.
print_wrapped(letters)
: this function is
passed a list of letters, and
uses "".join(letters)
to assemble them into a
single string. Then it breaks the string into words, and
prints them out. As it does so, it checks how long the
printed line would be, with each word. If the line would be
longer than 72 characters, issue a print
to
start a new line.
exchange(letters, x, y)
: this function is
passed a list of letters, and two letters, stored
in x
and y
. It replaces every
occurrence of x
with y
, and vice
versa.
show_frequencies(letters)
: this function is
passed a list of letters, and prints a table of frequencies,
indicating how often each letter occurs. To do this, create
a dictionary counts = dict()
, and visit each
letter. If it's in counts
, increment its
entry.
Finally, print the counts.