Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2021

Homework Assignment 2

Background

In this assignment, you will build on loop and list skills.

Caesar Cipher

Codebreaking played a very important role in the allied victory against Germany. The machine named Colossus, a forerunner of modern electronic computers, was used by the British at Bletchley Park to crack the German enigma code during WWII. A dramatized version of this story was told in the 2014 movie The Imitation Game. The ablility to transmit secret messages is vital to modern electronic banking and commerce and national security. Computers are now used every day to encrypt information sent over the internet.

In this project, you will write a program that will allow a user to encode and decode strings using a substitution cipher. These ciphers are now considered highly insecure, but still fun to play with. Much more complicated encryption techniques are used in modern computers.

A substitution cipher allows one to convert one string (called the plaintext) into another string (called the ciphertext) by subtituting one letter for another. A classic example of a substitution cipher is the Caesar cipher. This cipher is attributed to Julius Caesar who used it to transmit secret messages. With the Caesar cipher, each letter is shifted up by three positions and wrapped around to the beginning of the alphabet if needed (A becomes D, B becomes E, ..., X becomes A, ...).

  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z 
  D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A   B   C 
For example, the plaintext "ATTACK AT DAWN" becomes the ciphertext "DWWDFN DW GDZQ". To decode the message, one simply applies the process in reverse. For example, the string "FRPSXWHU VFLHQFH" is decoded as "COMPUTER SCIENCE".

This technique can be generalized to allow an arbitrary integer shift (mod 26). A shift of 0 does not change any characters, so it is an identity cipher. A shift of 13 is used in the linix rot13 (rotate by 13) command, which was used do encode offensive jokes sent via email when I was in college. This allowed you to read these with fair warning that you might be offended. An example dirty joke for you to decode is "V SRYY VAGB N ZHQ CHQQYR". The cipher with a shift of 13 is shown below.


  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z 
  N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A   B   C   D   E   F   G   H   I   J   K   L   M 
Interestingly, a Casear cipher with shift of 13 is its will decode its encoded message.

Another technique to generate a substitution cipher is called a keyword cipher. In this cipher, a keyword is given instead of an integer. The letters of the keyword are removed from the alphabet and matched with the original alphabet. For example, with a keword of "KEYWORD", the substitution cipher becomes:


  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z 
  K   E   Y   W   O   R   D   A   B   C   F   G   H   I   J   L   M   N   P   Q   S   T   U   V   X   Z 
If we use "MICHIGAN" as a keyword, then the substitution cipher becomes:

  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25 
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z 
  M   I   C   H   G   A   N   B   D   E   F   J   K   L   O   P   Q   R   S   T   U   V   W   X   Y   Z 
Note in this case "MICHIGAN" has 2 "I" characters, but only the first is used. In general, only the first occurence of a character is used and the remaining duplicated characters are ignored. A better keyword would include the letter "Z", for example "ZEBRA".

The most general for of the substitution cipher uses a keyword of exactly 26 unique charaters that have been randomly generated. In this case, each plaintext character is uniquely matched to a ciphertext character.

Historically the plaintext messages have all spaces and punctuation removed and are encoded using the cipher. The ciphertext messages are converted to a form where letters are put ito groups of 5 characters. For example, the encoding of "ATTACK AT DAWN" would be "DWWDF NDWGD ZQ". Random characters can are typically added to the end of the ciphertext to make a complete 5 letter block, such as "DWWDF NDWGD ZQIGJ" While blocking helps disguise the message, some word sequences may create ambiguous readings. Some amusing examples of this type of ambiguity occur with domain names of websites created from strings such as "Pen Island", "Experts Exchange", "Who Represents", "Therapist Finder", and "Speed of Art". Likewise, punctuation can drastically change the meaning of sentences. For example the sentences "Let's eat grandpa." and "Let's eat, grandpa." have different meanings because of the comma.

Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!

Task (50 points total)

Prep (5 points)

Create a method named prep that takes a single string parameter and returns a version of the string has all non-letters removed and all letters converted to upper case.

Pad (5 points)

Create a method named pad that takes a string parameter and returns a version of the string which is padded to have a length that is a multiple n. The padding should be randomly generated letters. For example, "ZQ" and 5 might produce "ZQIGJ". Hint use the % operator to help obtain the number of characters needed in the padding.

Block (5 points)

Create a function named block that takes a single string parameter and returns a version of the string segmented into groups of n characters.

Caesar (10 points)

Create a method caesar that takes a single String parameter (say S) representing the plaintext and encodes S into ciphertext using a Caesar cipher with the given key. Assume the input string contains only letters. To encode the ith character of S, one simply needs to determine its position, say p, in the original alphabet and obtain the character at position p in the cipher.

encrypt (5 points)

Encrypt s using a caesar cipher with key k and a blocksize of n. Prep, pad, apply the Caesar cipher, block the result, then return that string.

decrypt (5 points)

Encrypt s using a caesar cipher with key k and a blocksize of n. Prep and apply the Caesar cipher, then return that string.

Keyword (10 points)

Create a function keyword that takes a single String parameter (say S) representing the plaintext and encodes S into ciphertext using a keyword cipher with the given keyword. Assume the input string contains only letters. Note that in a substitution cipher, there is a correspondence between the plaintext alphabet and the ciphertext alphabet.

Stub Code

import string import random # remove all punctuation and convert to upper case def prep(s): return None # pad input string s to a multiple of n characters def pad(s, n): return None # block input string s have n characters followed by a space def block(s, n): return None # encrypt input string using a caesar cipher with shift k # consider using the ord() function def caesar(s, k): return None # encrypt s using a caesar cipher with key k and a blocksize of n # use your above functions def encrypt(s, n, k): return None # decrypt s using a caesar cipher with key k # use your above functions # note: spaces and punctuation are lost def decrypt(s, k): return None # encrypt s using a keyword cipher # consider using the ord() function def cipherAlphabet(keyword): return None # encrypt s using a keyword cipher # consider using the ord() function def keyword(s, keyword): return None def main(): s = "Pack my box with five dozen liquor jugs." print(s) # Pack my box with five dozen liquor jugs s1 = prep(s) print(s1) # PACKMYBOXWITHFIVEDOZENLIQUORJUGS s2 = pad(s1, 5) print(s2) # PACKMYBOXWITHFIVEDOZENLIQUORJUGSFOC (last 2 characters are random) s3 = caesar(s2, 5) print(s3) # UFHPRDGTCBNYMKNAJITEJSQNVZTWOZLXKTH s4 = block(s3, 5) print(s4) # UFHPR DGTCB NYMKN AJITE JSQNV ZTWOZ LXKTH for i in range(50): print(i%6, end="") print() s5 = encrypt(s, 5, 2) # RCEMO ADQZY KVJHK XGFQB GPNKS WQTLW IUZZR print(s5) print(decrypt(s5, 2)) # PACKMYBOXWITHFIVEDOZENLIQUORJUGSXXP print(cipherAlphabet('alphabetizer')) # ALPHBETIZRCDFGJKMNOQSUVWXY s6 = keyword(s2, 'alphabetizer') print(s6) main()

Deliverables

Send me (dreimann@albion.edu) your Python .py file as an attachment to an email message with Homework 2 as the subject line.