[Optional Extension] Assignment 2 - Keeping Secrets
Due: Thursday, May 23, 2024, at 10pm
This assignment is completely optional. If you receive at least 80% of the points, you will gain one extra token. If you receive at least 50% of the points (but not quite 80%), you will gain half of an extra token.
You may work alone or with a partner, but you must type up the code yourself. You may also discuss the assignment at a high level with other students. You should list any student with whom you discussed each part, and the manner of discussion (high-level, partner, etc.) in a comment at the top of each file. You should only have one partner for an entire assignment.
If you worked with a partner for the base version of this assignment, you may not work with another partner for the extension unless neither of you plans to resubmit the base version.
You should submit your assignment as an a2_opt.zip
file on Moodle.
Parts of this assignment:
Comments and collaboration
As with all assignments in this course, for each file in this assignment, you are expected to provide top-level comments (lines that start with #
at the top of the file) with your name and a collaboration statement. For this assignment, you have multiple programs; each needs a similar prelude.
You need a collaboration statement, even if just to say that you worked alone.
Note on style:
The following style guidelines are expected:
- Variable names should be clear and easy to understand, should not start with a capital letter, and should only be a single letter when appropriate (usually for
i
,j
, andk
as indices, potentially forx
andy
as coordinates, and maybep
as a point,c
for a circle,r
for a rectangle, etc.). - It’s good to use empty lines to break code into logical chunks.
- Comments should be used for anything complex, and typically for chunks of 3-5 lines of code, but not every line.
- Don’t leave extra print statements in the code, even if you left them commented out.
- Make sure not to have code that computes the right answer by doing extra work (e.g., leaving a computation in a for loop when it could have occurred after the for loop, only once).
Note: The example triangle-drawing program on page 108 of the textbook demonstrates a great use of empty lines and comments, and has very clear variable names. It is a good model to follow for style.
Part 1: Encrypting multiple messages at once
In this part, you will extend your circular Caesar cipher to allow you to encrypt multiple messages from a single user input. You should start with your previous circular Caesar cipher code, and save this new program as circularCaesarExtension.py
.
Here is an example:
Please enter a sequence of messages to encrypt, separated by commas: apple,banana,CAT,DOG,elephant,FISH,GiRaFfe
Please enter a key to shift by (an integer): 40
The encrypted messages are:
OccZS
POaOaO
pnG
qBt
SZScVOag
svFu
tWEOsTS
Your program should ask the user for the sequence of messages, separated by commas. After encrypting each plaintext message, you should print them out, one per line.
Recall our regular for
loops in Python:
words = ["apple", "banana", "cat", "dog", "elephant"]
for word in words:
print(word)
This prints out:
apple
banana
cat
dog
elephant
This example of a “nested” for
loop might be of use to you:
for word in words:
print("Word:", word)
for ch in word:
print(ch)
This prints out:
Word: apple
a
p
p
l
e
Word: banana
b
a
n
a
n
a
Word: cat
c
a
t
Word: dog
d
o
g
Word: elephant
e
l
e
p
h
a
n
t
(Hint: For each message in the input (separated by commas), for each character in the message, you want to encrypt that character. Once you have all of the characters in the encrypted message, you want to store the result in some sort of data structure that keeps track of many things, and then move on to the next plaintext messsage to encrypt. After you’re done, for every message you encrypted, you want to print the encrypted message. You are also welcome to create a function to encrypt an individual word; ask me if you have questions about this!)
Part 2: A cipher wrapped in an enigma
We can extend our Caesar cipher even further by using pseudo-randomness to modify the key after each character is encrypted. This is based (somewhat loosely) on the idea of the Enigma machine, which was used by the Germans in World War II to encrypt their messages.
For this part, you only need to write the encryption function; save your code for this part as enigmaticCaesarEncrypter.py
. Rather than asking the user for the key, your code should ask the user for a random seed. You can build a list of possible keys (think about how many there could be for a given input message), and use a function in the random
library to randomly reorder the key list.
After each character your code enrypts, it should use the current key as an index into the key list. The value at that position will be the key for the next character.
Here is an example of using the current key to look up the next key. Make sure you have working code for this before trying to get the enigmatic cipher working!
listToWalk = [1,5,0,2,3,4]
key = listToWalk[0]
# your code here (go through the list, printing each element,
# but not in the original order...)
1
5
4
3
2
0
1
5
4
3
Here is an example of encrypting using an enigmatic Caesar cipher (your output may vary slightly depending on how you handle spaces and capital/lowercase letters):
Please enter a string to encrypt: Apple banana cat dog elephant fish
Please enter a pseudo-random seed (an integer): 111
The encrypted message is:
GQgvaExskulneSWFwkomAWvaUDskNYsnhc
You don’t have to do it for this assignment, but for fun, you could think about how to decrypt a message given only the encrypted message and the seed…
Reflection
Did you learn anything additional that you find interesting? Were there any particular issues or challenges you dealt with in completing this assignment? How long did you spend on this assignment? Write a brief discussion (a sentence or two is fine) in your readme.txt file.
Grading
This assignment will be graded out of 100 points, as follows:
-
5 points - submit a valid
a2_opt.zip
file with all files correctly named -
5 points - all code files contain top-level comments with file name, purpose, and author names
-
5 points - each code files’ top-level comments contain collaboration statement
-
10 points - code style enables readable programs
-
35 points -
circularCaesearExtension.py
program encrypts a comma-separated sequence of words using a circular Caesar cipher -
35 points -
enigmaticCaesarEncrypter.py
encrypts a message using an enigmatic Caesar cipher -
5 points -
readme.txt
file contains reflection
What you should submit
You should submit a single a2_opt.zip file on Moodle. It should contain the following files:
readme.txt
(reflection)circularCaesarExtension.py
(Part 1)enigmaticCaesarEncrypter.py
(Part 2)