CS8 Programming Assignment 3

CS8 Programming Assignment 3

Description

In this programming assignment, you will implement the Vigenère encryption algorithm (cipher). Refer to the Wikipedia page about the algorithmic details of the Vigenère cipher. You should pay special attention to:
  1. Section "Description" for a real example;
  2. Section "Algebraic description" for mathematical illustration
The Vigenère algorithm explained in this Wikipedia page uses the input alphabet as 26 capital letters from A to Z, and it is based on mod 26 arithmetic.

In our project, we will use the lower case letters "a" to "z", the numbers "0" to "9", and the space " ", the dot ".", the comma ",", the question mark "?", and the exclamation mark "!" characters. Our input alphabet is

alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 .,?!"
which has 26+10+5=41 elements, and thus, we will use mod 41 arithmetic. Therefore the lower case letter ("a" to "z") indices are 0 to 25, the number ("0" to "9") indices are 26 to 35, and the indices for the space, dot, comma, question mark, exclamation mark characters are respectively 36, 37, 38, 39, and 40.

Requirements

You need to write two functions, as described below. Function names and parameter properties must be as specified.
  • The encryption function definition is
    def encVigenere(key, plaintext)
    
    Both key and plaintext are string arguments, such as
    encVigenere("my key for today","attack at dawn!")
    
    The function accepts an plaintext message of any length (a string of at least 1 character) and encrypts it using the key of any length (which is another string of at least 1 character), and returns the encrypted text (called ciphertext). The length of the ciphertext and plaintext will be the same. The key length may be shorter or longer than the plaintext.

  • Similarly, the decryption function definition is
    def decVigenere(key, ciphertext)
    
    Both key and ciphertext are string arguments. The function accepts the ciphertext as input, and decrypts it using the key, obtains back the original message (the plaintext) and returns it.

An Encryption Example

Key: lemonade
Plaintext: meet me at caje at 2pm!
According to the variable alphabet (above), the indices of the (repeated) key and text letters are:
Key :  l  e  m  o  n  a  d  e  l  e  m  o  n  a  d  e  l  e  m  o  n  a  d
Key : 11 04 12 14 13 00 03 04 11 04 12 14 13 00 03 04 11 04 12 14 13 00 03

Text:  m  e  e  t     m  e     a  t     c  a  j  e     a  t     2  p  m  !
Text: 12 04 04 19 36 12 04 36 00 19 36 02 00 09 04 36 00 19 36 28 15 12 40 
The encrypted text is obtained using mod 41 addition as
Key : 11 04 12 14 13 00 03 04 11 04 12 14 13 00 03 04 11 04 12 14 13 00 03
Text: 12 04 04 19 36 12 04 36 00 19 36 02 00 09 04 36 00 19 36 28 15 12 40
--------------------------------------------------------------------------
Ciph: 23 08 16 33 08 12 07 40 11 23 07 16 13 09 07 40 11 23 07 01 28 12 02
Ciph:  x  i  q  7  i  m  h  !  l  x  h  q  n  j  h  !  l  x  h  b  2  m  c
Therefore, the resulting ciphertext is xiq7imh!lxhqnjh!lxhb2mc

What To Submit

Write both of your functions encVigenere and decVigenere in a Python text file named pa3.py, and submit your file using the Dropbox link. Your functions will be tested using random values of key, plaintext, and ciphertext.

Submit only the functions; no test programs outside the functions are needed. Also, you may write and include in your pa3.py file as many helper functions as you want. Note that your functions encVigenere and decVigenere return values, not print them! Furthermore, your functions should work with any length key, plaintext, and ciphertext, each of each is at least 1 character.

This programming assignment is graded out of 15 points, i.e., it has 5 points of extra credit.

Important: Add your full name and email address as a comment in the first line of your file, such as:
# Alice Smartpants (smartpants@umail.ucsb.edu)


cs8 web page