Lecture notes for the Introduction to Computer Science I by James Tam   Return to the course web page

CPSC 231 Assignment 4

New Concepts

String manipulation, exceptions and testing. (Note that you do not need to use lists for this assignment. Nor will the use of lists make this assignment easier.)

Pre-written Python functions that you can use in this assignment include the follow: [Allowed functions]

Introduction

For you tutorial exercise you were asked to implement a Ceasar cipher, which is very easy to break, both for humans and computers. For this assignment you will be asked to create a program that encrypts text using the Vigenère cipher.

The Vigenère cipher is similar to the Caesar cipher because both use shifting to encrypt a character. But in the Vigenère cipher the amount shifted is different for each character. This is done by requiring that the key is a word containing only characters from the English alphabet. For each letter in the text to encrypt, we use a different letter from the key. The position of the key's letter in the English alphabet indicates the amount the character to be encrypted should be shifted. So in essence we're providing a different key for each letter in plain text (the text to encrypt). Ideally, the key used is at least the same length as the text to encrypt. However, if this is not the case, the characters in the key are repeated and reused.

An explanation of the Vigenère cipher can be found at http://www.counton.org/explorer/codebreaking/vigenere-cipher.php. This website also allows you to build the key table and to encrypt text using the cipher, which will allow you to create test cases.

Below is an example implementing the cipher.

For example, let the text that we want to encrypt be 'Hello. I want to tell you a secret!' and the key that we'll use for encryption 'tigerish'. For the remainder of this document, we'll use the term plain text to denote the text to encrypt.

  1. To encrypt the first letter of the plain text 'H' use the first letter from the key, namely 't'. To figure out which character to encrypt 'H' to, consider the English alphabet as if it started from the key character 't':

    Key ordering: t u v w x y z a b c d e f g h i j k l m n o p q r s

    Line this up with the usual ordering of the English alphabet:

    Key ordering: t u v w x y z a b c d e f g h i j k l m n o p q r s
    Text Ordering: 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

    Look at the 'h' from the plain text in the text ordering. It lines up with the 'a' in the key ordering, so 'a' is the encrypted character and the first letter in the encrypted text is 'A'. (Note that case of the letter is preserved in the encryption.)

    You can also view this as shifting the character 'h' over by 19, since the key character 't' is at index 19 in the English alphabet. (The character 'a' is at index 0.)

  2. We do the same for subsequent characters in the text. The second character in the key is 'i', so the key ordering of the English alphabet becomes:

    Key Ordering: i j k l m n o p q r s t u v w x y z a b c d e f g h

    The second letter in the plain text is an 'e', so we find, using the table below, that 'e' is encrypted to 'm'. In this case we shifted by 8 since the key character 'i' is at index 8 in the English alphabet.

    Key Ordering: i j k l m n o p q r s t u v w x y z a b c d e f g h
    Text Ordering: 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

    We have now encrypted two characters and the encrypted text so far is: 'Am'.

  3. The third character in the plain text is 'l' and in the key the third character is 'g'. This gives us the following table for encrypting 'l'.

    Key Ordering: g h i j k l m n o p q r s t u v w x y z a b c d e f
    Text Ordering: 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

    So we now have encrypted text 'Amr'.

  4. The fourth character in the plain text is 'l' and in the key it is 'e', giving us the following table.

    Key Ordering: 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 d
    Text Ordering: 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

    And we have encrypted text 'Amrp'.

  5. Doing the same for the fifth character, we get the table

    Key Ordering: r s t u v w x y z a b c d e f g h i j k l m n o p q
    Text Ordering: 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

    and encrypted text 'Amrpf'.

  6. The sixth character in the plain text is a period which is not an alphabetic character and thus remains unchanged, giving us encrypted text 'Amrpf.'.
  7. The seventh character in the plain text is a space, which is also not an alphabetic character. So our encrypted text becomes 'Amrpf. '.
  8. The eight character in the plain text is an 'I' and in the key is an 'h' and using the following table we see that the encrypted text becomes 'Amrpf. P'.

    Key Ordering: h i j k l m n o p q r s t u v w x y z a b c d e f g
    Text Ordering: 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

  9. Since there is no ninth character in the key, we start from the beginning of the key again and use the first character in the key for the ninth character in the plain text. Since the ninth character in the plain text is not an alphabetic character, we do not encrypt it and we have encrypted text 'Amrpf. P '.
  10. For the tenth character in the plain text, 'w', we use the second character in the key, 'i', and use the following table to get encrypted text 'Amrpf. P e'.

    Key Ordering: i j k l m n o p q r s t u v w x y z a b c d e f g h
    Text Ordering: 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

We continue doing this with the remaining letters of the plain text to get encrypted text 'Amrpf. P egrk lv bkpc qvn g jmuyxb!'.

Coding Requirements

Write a program that will encrypt a string using the Vigenère cypher.

Prompt the user for a key and validate that the user entered a string that only contains alphabetic characters. If the user entered an invalid key, raise a KeyError.

Then prompt the user to enter text to encrypt. Note that any text is considered valid here.

Finally display the encrypted text to the user.

Documentation Requirements

Besides the usual documentation in your code, also provide a separate text document that describes how to test you code. This should include the input that should be provided to your code and what the expected result is. For each test case, indicate what is being tested.

Your tests should be thorough and should ensure that all lines of code are executed in some test case.