Case Study

Let's take a walk through test-driven development by writing a small, tested, cryptography application. Don't worry, you won't need to understand the mathematics behind complicated modern encryption algorithms such as Threefish or RSA. Instead, we'll be implementing a 16th century algorithm known as the Vigenere cipher. The application simply needs to be able to encode and decode a message, given an encoding keyword, using this cipher.

First we need to understand how the cipher works if we apply it manually (without a computer). We start with a table like this:

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Given a keyword TRAIN, we encode the message ENCODED IN PYTHON as follows:

1. Repeat the keyword and message together such that it is easy to map letters from one to the other:

E N C O D E D I N P Y T H O N T R A I N T R A I N T R A I N

2. For each letter in the plaintext, find the row that begins with that letter in the table.

3. Find the column with the letter associated with the keyword letter for the chosen plaintext letter.

4. The encoded character is at the intersection of this row and column.

For example, the row starting with E intersects the column starting with T at the character X. So the first letter in the ciphertext is X. The row starting with N intersects the column starting with R at the character E, leading to the ciphertext XE. C intersects A at C, and O intersects I at W. D and N map to Q while E and T map to X. The full encoded message is XECWQXUIVCRKHWA.

Decoding basically follows the opposite procedure. First find the row with the character for the shared keyword (the T row) then find the location in that row where the encoded character (the X) is located. The plaintext character is at the top of the column for that row (the E).