In this homework, you will be implementing the Caesar&Cipher encryption scheme. The Caesar
Cipher is a simple way of encrypting text so that it is not readable by the casual observer. The
encryption scheme gains its name from Julius Caesar who used several similar text substitution
algorithms to encrypt/decrypt important messages.
The Caesar Cipher is simple way to “encrypt” alphabetic(letters (aside: don t try encrypt
anything important with the Caesar Cipher). All remaining punctuation symbols, numeric digits,
or other characters (spaces) remain unchanged.
Encrypting a message using the Caesar Cipher involves replacing each letter in a message with
the letter k places further down the alphabet, wrapping around at the end of the alphabet when
necessary. With k&=&0, each letter is replaced by itself. With k&=&20, each letter is shifted 20 places
down the alphabet. To decode or decrypt the text you simply shift the encrypted letter in the
opposite direction by k places and wrap around as necessary.
The letters in the Caesar Cipher alphabet start at 0 and continue through 25, with the letter “A”
being 0 and “Z” being 25. If the user were to choose k&=&3, the letter “A” (0) would be replaced by
the letter “D” (3), while the letter “B” (1) would be replaced by the letter “E” (4). If a letter
appears towards the end of the alphabet, the alphabet simply wraps around and starts again. So
the letter “Z” (25) would be replaced by the letter “C” (25 + 3 = 28), which is 2 after wrapping
around. The “wrap around” math is accomplished simply using the “modulo” operator in Python
(%) which returns the remainder after integer division.
If x is the position of some letter we are trying to encrypt, it should be replaced by the letter at
the position denoted by (x&+&k)&%&26, which will be a number between 0 and 25. For the above
example, 25 + 3 = 28, and 28 % 26 = 2, or “C”. To decrypt you simply subtract k instead of adding:
(x&7&k)&%&26. To reverse the previous example we take the result “C” = 2, with a shift of 3, so (2[3)
% 26 = 25 giving us back the letter “Z”.&
Recall (from Chapter 4) that our alphabet in Python is encoded using UTF[8 (see the table in the
slides or book), from Python s perspective “A” is not 0. Instead, an upper case “A” is represented
by the number 65, “B” by 66, and so on, with capital “Z” represented by 90. While a lower case
“a” is represented by the number 97, “b” by 98, and so on, with “z” represented by 122. Your
program will have to account for this when shifting the letters using the formula above. Note:(
To obtain the UTF[8 values, you will need to use the built[in ord(char) function, which accepts
a string containing a single letter and returns the integer value of the letter. You will also need to
use the built[in chr(int) function which accepts an integer and returns the corresponding
Unicode/ASCII letter. The methods isupper() and islower() may also be useful.
Further, any remaining punctuation symbols, numeric digits, or other characters (spaces) should
be unmodified and remain unchanged in the message. Chapter 4 contains examples of how to
identify and skip these characters while encoding, as a hint you will need to import string.
Your program should begin by prompting the user for a message (a string), a shift amount (an
integer), and whether they would like to encrypt or decrypt, then our implementation will add
some variation to the basic algorithm to make it a little harder to decode. When encrypting:
(1) You ll replace each “e” character in the original message with the letters “zw”. If the user
enters “Hello World!”, the resulting message will be “Hzwllo World!”.
(2) After replacing the “e” characters, you ll add the word “hokie” to the beginning, middle
(the middle is length//2), and the end of the message. Continuing with example above the
message that s actually encrypted using the Caesar Cipher is “hokieHzwllohokie
Once you have the altered message then perform the Caesar Cipher encoding as described above,
printing the result.
When decrypting, perform each step in reverse order. Decrypt the message using the Caesar
Cipher, then remove the “hokie” strings, then change any occurrence of “zw” to “e”. You may
assume the all occurrences of “zw” in the result are really “e” characters, and further you may
assume the “hokie” strings are in the encrypted message at the appropriate place, however(
Finally, your program must be able to encrypt/decrypt an arbitrary number of messages. When
you ve completed the encrypting/decrypting a message the user should be asked if they want to
enter another message. Your program should continue until the user types “N”.
The user input is green:
Enter Message: Hello World!
Enter shift amount: 3
Encode (E) or Decode (D)? E
Result: krnlhKczoorkrnlh Zruog!krnlh
Go again? (Y/N): Y
Enter Message: krnlhKczoorkrnlh Zruog!krnlh
Enter shift amount: 3
Encode (E) or Decode (D)? D
Result: Hello World!
Go again? (Y/N): N
For this assignment you should submit your hw4.py file.
This assignment will be graded automatically. Test your programs thoroughly before submitting
them. Make sure that your programs produce correct results for every logically valid test case
you can think of. Do not waste submissions on untested code, or on code that does not run with
the supplied code from the course website.
Web[CAT will assign a score based on runtime testing of your submission; your best score will be
counted; the TAs will later verify that your best submission meets the stated restrictions, and
assess penalties if not.