Vous êtes sur la page 1sur 2

Caesar cipher encoder/decoder

Your assignment is to create a Caesar cipher encoder/decoder with a Graphical User Interface (GUI).

“A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of the plaintext
message a fixed number of positions in the alphabet.” This “fixed number of positions” is called the key.
So, for example, the plaintext word “Hello” with a key value of 4 would be encoded “Lipps”. And the
ciphertext “O rubk Iusv Yio” with a cipher key of 6 would be decoded to “I love Comp Sci”. (Note that
when decoding here we shifted 6 positions backward in the alphabet.)
To complete this assignment, it is helpful to recall that each character has a corresponding ASCII code,
and that the letters in the ASCII table are conveniently arranged in alphabetical order. (The ASCII table
can easily be found online, say here: http://www.asciitable.com/. When reading this table you can
ignore the Hex, Octal, and HTML columns, just look at the decimal and character columns.)
There are two handy functions:
1. ord(c), which returns the numerical ASCII value that corresponds to the character c
2. chr(i), which returns the character that corresponds to the ASCII value i

Hence, to compute the numerical ASCII value for the character that is key positions away from a
character c in the alphabet, you can simply calculate
ord(c) + key
You can then take the resulting value and simply pass it as the argument to the chr() function to get
the corresponding character!
Note, however, that the space character (“ “) should be encoded/decoded as itself. In fact, only
letters (A-Z) should be shifted, and any other characters should be left alone. You can easily check
whether a character is in the alphabet by checking its ASCII value.
Your program should start with an animated title screen, along with an introduction to the program.
(You can be creative and come up with your own title for the program.) The user should then be
presented with two text fields (Entry objects), in which to enter the phrase to be encoded/decoded
and the key value to use for encoding/decoding.
You should complete this program in stages of increasing complexity. For the first stage, you will
complete the bulk of the assignment, barring some simplifying assumptions, and then the remaining
stages will be refinements and improvements.
Stage 1 (75%). Start by creating the GUI. I would suggest you have two input text fields (Entry objects),
one for the phrase to be encoded/decoded, and another for the cipher key value. Then
create two “buttons” that the user can click, one for Encode and one for Decode. You can just draw
these using Rectangle objects if you’d like. If the user clicks the “Encode” button, then your program
should encode the plaintext phrase inputted by the user and output the result. If the user clicks the
“Decode” button, then that means the user typed in an encoded phrase, so your program should
decode the ciphertext and output the plaintext. Either way, the output should appear in the GUI, say in
a Text object. (If you’d like, you can set size, font fact, style, and color for Text objects. For this initial
stage, you may simply take the input text and immediately uppercase all of it (using the function
.upper()), so that you don’t need to worry about mixed-case input. You may output the
encoded/decoded message in all uppercase. At this stage, you may also assume that the user never
inputs a phrase and a key that causes you to shift off the end of the alphabet (past the letter Z or before
the letter A).
Before proceeding to stage 2, I encourage you to ensure your stage 1 program is fully functional. I
would also save a copy of the Stage 1 program so that you can easily revert to it if necessary.
Stage 2 (15%). At this stage you should remove the restrictive assumption we made in Stage 1 that the
user will not need your program to “wrap-around” the alphabet. For example, if the user wants to
encode the letter “Z” with a cipher key of 2, the output of your program should be the letter “B.” And,
accordingly, if the user wants to decode the letter “C” with a cipher key of 6, your program should
output the letter “W.”
Before proceeding to stage 3, I would again save out a copy of your fully-functional Stage 2 program.
Stage 3 (10%). At this stage you should modify your program so that it retains the upper/lower case of
the characters of the input. So rather than immediately converting the input to all uppercase, you
should modify your program so that an uppercase input character yields an uppercase output character
and a lowercase input character yields a lowercase output character. (See the “I love Comp Sci”
example above.)
Stage 4 (Bonus). Add a feature to your program so that rather than just inputting a phrase from the
user, it allows the user to input an entire text file. The entire text file should be encoded/decoded again
based on the user-provided key. Have the encoded/decoded text be written to a new text file.

Vous aimerez peut-être aussi