Lecture notes for the Introduction to Computer Science I by James Tam | Return to the course web page |
Pre-written Python functions that you can use in this assignment include the follow: [Allowed functions]
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.
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.)
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'.
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'.
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'.
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'.
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 |
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 |
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.
Your tests should be thorough and should ensure that all lines of code are executed in some test case.