Homework: Caesar Cipher

Q1. Ciphers and Statistics

A Caesar cipher is a very simple method of encoding and decoding data. The cipher simply replaces characters with the character offset by \(k\) places. For example, if the offset is 3, we replace a with d, b with e etc. The cipher wraps around so we replace y with b, z with c and so on. Punctuation, spaces and numbers are left unchanged.

  • (25 points) Write a function encode that takes as arguments a string and an integer offset and returns the encoded cipher.
In [ ]:

  • (5 points) Write a function decode that takes as arguments a cipher and an integer offset and returns the decoded string.
In [ ]:

  • (50 points) Write a function auto_decode that takes as argument a cipher and uses a statistical method to guess the optimal offset to decode the cipher, assuming the original string is in English which has the following letter frequency:
freq = {
 'a': 0.08167,
 'b': 0.01492,
 'c': 0.02782,
 'd': 0.04253,
 'e': 0.12702,
 'f': 0.02228,
 'g': 0.02015,
 'h': 0.06094,
 'i': 0.06966,
 'j': 0.00153,
 'k': 0.00772,
 'l': 0.04025,
 'm': 0.02406,
 'n': 0.06749,
 'o': 0.07507,
 'p': 0.01929,
 'q': 0.00095,
 'r': 0.05987,
 's': 0.06327,
 't': 0.09056,
 'u': 0.02758,
 'v': 0.00978,
 'w': 0.0236,
 'x': 0.0015,
 'y': 0.01974,
 'z': 0.00074
}
In [ ]:

  • (20 points) Encode the following nursery rhyme using a random offset from 10 to 20, then recover the original using auto_decode:
Baa, baa, black sheep,
Have you any wool?
Yes, sir, yes, sir,
Three bags full;
One for the master,
And one for the dame,
And one for the little boy
Who lives down the lane.
In [62]: