Cryptography ( Due 24 Jan 2014 )

Cryptography is an ancient study of secret writing. There is a wealth of literature in this field. An extremely readable book on this subject is The Code Book by Simon Singh. This is a field of study that is of particular relevance in Computer Science. Given the widespread use of computers, one of the things people are interested in is making transactions over the internet more secure.

For the purposes of this programming assignment convert the message that you are asked to encode or decode to lower case. You will only be encoding or decoding the letters of the alphabet. Punctuation marks and numerals remain untouched.

Substitution Cipher: In this method you will replace the letters using the following scheme.

Plain Text:  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
Cipher Text: q a z w s x e d c r f v t g b y h n u j m i k o l p
Save the Cipher letters in a list of characters.
cipher = [ 'q', 'a', 'z', ...., 'o', 'l', 'p' ]
To find the substitution for the character d first convert d into an index for the array cipher as follows:
idx = ord ('d') - ord ('a')
Then use that index (whose value in this case is 3) to access the character that will be a substitute for d (in this case cipher[3], which is w. The Caesar Cipher is also a substitution cipher that can be easily decoded.

Vigenere Cipher: Substitution ciphers can be broken by frequency analysis. To make the cipher more secure you can use two or more letters to encrypt. The Vignere cipher uses a pass phrase to encrypt and decrypt.


        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

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

Let us say we want to encrypt hello world and we use as our pass phrase seal. Here is how we would lay the phrases out. The pass phrase is repeated over and over again in order with the text that needs to be encrypted. The letter from the pass phrase marks the row and the letter from the plain text marks the column.

       Pass Phrase: seals ealse
        Plain Text: hello world
    Encrypted Text: zilwg aocdh

To decrypt, we would reverse the process. Use the same pass phrase seal repeatedly with the encrypted text. This time, the letter from the pass phrase marks the column. Within that column search for the encrypted letter. That row indicates the plain text.

       Pass Phrase: seals ealse
    Encrypted Text: zilwg aocdh
        Plain Text: hello world

There are several functions that you will be writing for this assignment. But they will all be in one file called TestCipher.py. The template for your program is as follows. The parameter strng is the string to be encoded or decoded. The parameter passwd is the pass phrase for the Vigenere cipher.

For the substitution cipher you will create two lists of the letters of the alphabet - one for encoding and the other for decoding. For the Vignere cipher I do not want you to create a 2-D list. Instead I want you to come up with a formula that will take as input two characters and return the encrypted cipher. For decryption you come with a similar formula.

def substitution_encode ( strng ):
  ...

def substitution_decode ( strng ):
  ...

def vigenere_encode ( strng, passwd ):
  ...

def vigenere_decode ( strng, passwd ):
  ...

def main():
  ...

main()

Your TestCipher program will test the encoding and decoding methods of the two Cipher algorithms Your output will look something like this:

Substitution Cipher

Enter Plain Text to be Encoded: hello world
Encoded Text: dsvvb kbnvw

Enter Encoded Text to be Decoded: dsvvb kbnvw
Decoded Plain Text: hello world

Vigenere Cipher

Enter Plain Text to be Encoded: hello world
Enter Pass Phrase (no spaces allowed): seal
Encoded Text: zilwg aocdh

Enter Encoded Text to be Decoded: zilwg aocdh
Enter Pass Phrase (no spaces allowed): seal
Decoded Plain Text: hello world

The file that you will be turning in will be called TestCipher.py. The file will have a header of the following form:

#  File: TestCipher.py

#  Description:

#  Student's Name:

#  Student's UT EID:

#  Course Name: CS 313E 

#  Unique Number: 53580

#  Date Created:

#  Date Last Modified:

Use the turnin program to submit your TestCipher.py file. The proctors should receive your work by 11 PM on Friday, 24 January 2014. We will be looking for clean logic and good documentation. There will be substantial penalties if you do not adhere to the guidelines. The student assistant in charge of this assignment is Devin Sandhu (devin.sandhu@gmail.com).

References