Assignment #02#
Today’s exercises have to be done with help of the python standard library alone! No external module can be used.
Tip
Can you remember what the “standard library” is? Here is a list of all the modules and built-ins it provides: so quite a lot!
#02-01: cryptography for Roman Emperors#
A very simple encryption technique is called the Caesar cipher. The basic idea is that each letter is replaced by a letter that is a certain number of letters away, so for example if the shift was 2, then A would become C, B would become D, and Z will become B.
A. Write a function that given a string and a shift, will produce the encrypted string for that shift. The rules are:
you should accept lowercase and uppercase letters, and return letters of the same case
spaces or other punctuation characters should not be changed.
There are several ways to reach this result. Just pick the one which makes more sense to you. Then, decrypt the following message, which was encrypted with a shift of 13:
Pbatenghyngvbaf, lbh unir fhpprrqrq va qrpelcgvat gur fgevat.
B. Now write a decoding script caesar.py
which, when run in the command line, prints the decoded phrase after asking the user to enter a phrase and a shift with which it was encrypted. Test your script on the sentence above.
Tip
Make use of python’s input() function
C. Now try to decrypt this sentence, for which the shift is unknown:
Gwc uivioml bw nqvl bpm zqopb apqnb.
One way to solve this problem involves human decision, but another way can be fully automated (and implies more work). Pick the one you want! You can also add the decryption option to your script if you want.
#02-02: the substitution cipher#
Since we managed to crack the code above so easily, maybe we should use a better cryptographic algorithm from now on.
The Caesar cipher is a special case of the more general substitution cipher. In this cipher, any new combined alphabet (or sequence of bits, characters, numbers…) can be used in place of the regular alphabet.
To simplify things a bit, we will use python’s random
module and in particular it’s shuffle function to do the job. To generate a random (but predictable) substitution table we can use the mechanism of “seeds”, which can be seen as a secret key that you’ll have to (privately) give to your forbidden lover for him/her to decipher your message. This works as following:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
seed = 42 # the secret key known by both parties
import random
random.seed(seed)
new_alphabet = list(alphabet)
random.shuffle(new_alphabet)
print(new_alphabet)
['q', 'm', 'j', 'z', 't', 'g', 'f', 'k', 'p', 'w', 'l', 's', 'b', 'o', 'x', 'n', 'c', 'r', 'y', 'e', 'v', 'h', 'i', 'a', 'd', 'u']
A. Building upon the template from the previous exercise, create a script substitute.py
which now asks:
1. for a phrase to encrypt or decrypt
2. whether the script should Encrypt (E) or Decrypt (D) the phrase
3. which seed should be used (it should always be an integer, but can be any integer)
B. Since your cipher still preserves the location of punctuation (space, points, exclamation marks, etc.), it is easier to crack. Can you think of a very simple way to substitute these characters as well for better encoding? (minimal change in code)