Cryptography ( Due 30 Mar 2016 )

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, digits and spaces 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 should 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():
  # open file for reading
  in_file = open ("./cipher.txt", "r")

  # print header for substitution cipher
  print ("Substitution Cipher")
  print ()

  # read line to be encoded
  line = in_file.readline()
  line = line.strip()

  # encode using substitution cipher
  encoded_str = substitution_encode (line)

  # print result
  print ("Plain Text to be Encoded: " + line)
  print ("Encoded Text: " + encoded_str)
  print ()

  # read line to be decoded
  line = in_file.readline()
  line = line.strip()

  # decode using substitution cipher
  decoded_str = substitution_decode (line)

  # print result
  print ("Encoded Text to be Decoded: " + line)
  print ("Decoded Plain Text: " + decoded_str)
  print ()

  # print header for vigenere cipher
  print ("Vigenere Cipher")
  print ()

  # read line to be encoded and pass phrase
  line = in_file.readline()
  line = line.strip()
  passwd = in_file.readline()
  passwd = passwd.strip()

  # encode using vigenere cipher
  encoded_str = vigenere_encode (line, passwd)

  # print result
  print ("Plain Text to be Encoded: " + line)
  print ("Pass Phrase (no spaces allowed): " + passwd)
  print ("Encoded Text: " + encoded_str)
  print ()

  # read line to be decoded and pass phrase
  line = in_file.readline()
  line = line.strip()
  passwd = in_file.readline()
  passwd = passwd.strip()

  # decode using vigenere cipher
  decoded_str = vigenere_decode (line, passwd)

  # print result
  print ("Encoded Text to be Decoded: " + line)
  print ("Pass Phrase (no spaces allowed): " + passwd)
  print ("Decoded Plain Text: " + decoded_str)
  print ()

  # close file
  in_file.close()

main()

Your TestCipher program will test the encoding and decoding methods of the two Cipher algorithms. You will read the input from a file cipher.txt . Assume that the strings for encoding and decoding and the pass phrase are no more than 80 characters. You may have more functions than specified. Your output will look something like this:

Substitution Cipher

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

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

Vigenere Cipher

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

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

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming. .

The file that you will be uploading will be called TestCipher.py. We are looking for clean and structured design. The file will have a header of the following form:

#  File: TestCipher.py

#  Description:

#  Student Name:

#  Student UT EID:

#  Partner Name:

#  Partner UT EID:

#  Course Name: CS 313E

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

If you are working with a partner both of you will be submitting a single program (from either account) but make sure that you have your partner's name and eid in your program. If you are working alone, then remove the two lines that has the partner's name and eid in the header.

Use the Canvas system to submit your TestCipher.py file. We should receive your work by 11 PM on Wednesday, 30 Mar 2016. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.

References