Assignment 8: Keyed Transposition

Due by: Friday, November 10, 2023 at 11:59 p.m.

Although we discussed the Rail Fence cipher in class, there are more interesting variations such as the following.

Instead of having only the number of rails as the key, a string can be used. For this purpose, the string should contain all unique characters. Thus, 'orange' would be a legal key, but neither 'apple' nor 'banana' would be legal since both contain repeated letters. To simplify the encryption and decryption process, we will also require that the length of the message is evenly divisible by the length of the key.

Given such a key and such a plaintext, the encryption is done as follows: The number of rails is the length of the plaintext divided by the length of the key. Thus, the matrix will have the same number of columns as the length of the key. As the matrix is generated, the columns are associated with the characters in the keyword. Given the plaintext 'Where are the Snowdens of yesteryear' and the key 'john', this process will generate a matrix like the one below.

Key: j o h n
Row 0: W d e
Row 1: h t e s
Row 2: e h n t
Row 3: r e s e
Row 4: e r
Row 5: S o y
Row 6: a n f e
Row 7: r o a
Row 8: e w y r

The order of the columns is then sorted so that they appear according to the alphabetical order of the key's letters. Doing so changes the matrix above into the following matrix.

Key: h j n o
Row 0: d W e
Row 1: e h s t
Row 2: n e t h
Row 3: s r e e
Row 4: e r
Row 5: o y S
Row 6: f a e n
Row 7: r a o
Row 8: y e r w

Just as with the normal Rail Fence cipher, the letters are read row by row, yielding a final ciphertext of 'dWe ehstnethsree er o ySfaen raoyerw' in this example.

Specification

Your mission is to complete two Python functions that perform both encryption and decryption using this system. All the code should be stored in a file called assignment8.py.

Encryption

The first function you should write performs the encryption, according to the description above.

Give this function the following header:

def keyedRailEncrypt(plaintext, key):

Decryption

The second function you should write performs the decryption. There are many mechanical similarities to the encryption, but the approach has some differences as well. First, you should sort the letters of the key. Then, you should create columns associated with each letter of the sorted key that correspond to the ciphertext. Last, you will use the key in the original, unsorted order to retrieve the appropriate columns and reconstruct the plaintext in that way.

Give this function the following header:

def keyedRailDecrypt(ciphertext, key):

Turn In

Upload assignment8.py to Blackboard.

All work must be submitted before Friday, November 10, 2023 at 11:59 p.m. unless you are going to use a grace day.

All work must be done individually. You may discuss general concepts with your classmates, but it is never acceptable for you to look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Grading

Your grade will be determined by the following weights:

Category Weight
Correct encryption 50%
Correct decryption 50%

Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.