2016 September 30,

# CS 111: Assignment: Ciphers

## Introduction

In this assignment, you will practice with some basic ciphers for encrypting data. For simplicity, we assume that the data are strings containing only spaces and upper-case Roman letters A, B, C, ..., Z. The file ciphers.py gets you started; download it to your computer.

You must work on this assignment individually, and hand in your own solutions, although as usual you are encouraged to talk with other students. (See the syllabus or ask questions, if you're unsure of what's allowed.) This assignment is due Tuesday at 11:59 PM. You will submit your edited version of ciphers.py, electronically as usual.

## Finish Implementing the Substitution Cipher

The following code implements part of a substitution cipher. The substitution itself is represented as a string of the 26 upper-case Roman letters A, B, C, ..., Z in some mixed-up order. Each entry indicates how to encrypt a certain letter of the alphabet, by its position in the list. For example, the following substitution says that A encrypts as T, B encrypts as H, C encrypts as E, and so on.

`sub = "THEQUICKSLVRFOXJMPDAZYBWNG"`

Any given substitution has a corresponding inverse substitution, which is simply the substitution that decrypts each letter. In the example above, the inverse substitution is

`inv = "TWGSCMZBFPHJQYNRDLIAEKXOVU"`

Why? Well, for example, look at the end of the original substitution. It says that Z encrypts as G. Now look at inv[6]. It says that letter #6, which is G, decrypts as Z. Work out some more examples like this for yourself, to understand this idea. (In this example, why do both sub and inv begin with T? Is that just a coincidence?) Then implement substitutionInverse in ciphers.py.

## Re-implement the Caesar Cipher Using Substitution

In ciphers.py there is a place where you are asked to rewrite caesarEncryption using substitutionEncryption. This is possible because the Caesar cipher is a special case of the substitution cipher. That is, you can convert the Caesar key n into a substitution sub, such that substitution-encrypting with sub has the same effect as Caesar-encrypting with n. The heart of the matter is: How do you build sub from n?

## Implement the Repeated Pad Cipher

In this part of the assignment, you will implement the repeated pad cipher discussed in class. This is much like the Caesar cipher, but it uses a key string instead of a single key character. You add the message to the key word to get the encrypted message. If the key word is not long enough, then you repeat the key word as many times as necessary. As always, spaces are encrypted as spaces.

Your task in this part of the assignment is to implement the functions repeatedPadEncryption and repeatedPadDecryption in ciphers.py. Here is an example transcript:

```>>> plaintext = "SECRET DOLPHIN ATTACK AT NOON"
>>> key = "GNIJIEB"
'YRKAMX JBTYPMO NBCIGL NB VSPT'
'SECRET DOLPHIN ATTACK AT NOON'```

## Break the Caesar Cipher

In ciphers.py, write a function caesarBreak to decrypt a Caesar-ciphered string without knowing the key. As input it takes a Caesar-encrypted string. It returns None, but it prints (not returns) a bunch of information, that a human user can use to decide what the key was. For example, it might try a bunch of keys, for each one printing out the key and an attempted decrypted string.

## Break the Substitution Cipher

I will post more instructions on this part later. You will use theWarOfTheWorlds.txt as your corpus for a frequency analysis.

Update: I have decided to cancel this part of the assignment, because I will be giving you other work to complete for Friday.

## Demonstrate

In ciphers.py, look at the main function. It demonstrates how a couple of ciphers work. You should augment it to demonstrate the other ciphers in this assignment. It should also demonstrate your attempts to break the Caesar cipher. I'm not telling you exactly how to write these demonstrations; you should figure that out for yourself. If it helps, imagine that you are trying to sell your software to a customer who is not easily impressed.