CS 101 Second Cryptography Lab: Symmetric Cryptography

Symmetric Cryptography involves encryption algorithms that use a shared key to encrypt and decrypt a message between parties.

For instance, the Pigpen Cipher is a mono-alphabetic substitution cipher used by the Freemasons back in the 18th century. The Pigpen cipher does not substitute one letter for another, rather it substitutes each letter for a symbol according to the patterns in Figure 1.

Figure 1: Pigpen Cipher Key

Someone using the Pigpen Cipher would use the key above to encrypt a message, knowing their recipient would have this same cipher key to decipher any messages sent to them. This is an example of a symmetric cryptographic algorithm because both parties have and use the same key for encryption and decryption. For instance,

[ - For Cipher]

a =

b =

..

..

z =

A sample encrypted message would look like:

Based on the cipher key, above what is the encrypted message?

Now what would the cipher text of “the force is strong with this one” look like?

Another symmetric cryptographic algorithm is the ADFGVX cipher. This cipher features both substitution and transposition. Encryption beings by drawing up a 6 x 6 grid, and filling the 36 squares with a random arrangement of the 26 letters (a - z) and the 10 digits (0-9). Each row and column of the grid is identified by one of the six letters A,D,F,G,V, or X. The arrangement of the elements in the grid acts as part of the key, so the receiver needs to know the details of the grid in order to decipher messages.

For instance a possible grid could look like:

A / D / F / G / V / X
A / 8 / p / 3 / d / 1 / n
D / l / t / 4 / o / a / H
F / 7 / k / b / c / 5 / Z
G / j / u / 6 / w / g / M
V / x / s / v / i / r / 2
X / 9 / e / y / 0 / f / Q

The first stage of encryption is to take each letter of the message, locate its position in the grid and substitute it with the letters that label its row and column. For example, 8 would be substituted by AA, and p would be replaced by AD. Here is a sort message encrypted according to the system:

Message attack at 10 pm

Plaintexta t t a c k a t 1 0 p m

Stage 1 Ciphertext DV DD DD DV FG FD DV DD AV XG AD GX

So far this is a simple mono-alphabetic substitution cipher, and frequency analysis would be enough to crack it. However, the second stage of the ADFGVX is a transposition, which makes cypranalysis much harder. The transposition depends on a keyword, which in this case happens to be the word MARK, and which must be shared with the receiver. The key MARK and the table above would be the symmetric key shared between parties. Transposition is carried out according to the following recipe. First, the letters of the keyword are written in the top row of a fresh grid. Next, the stage 1 ciphertext is written (horizontally) underneath it in a series of rows, as shown below. The columns of the grid are then rearranged so that the letters of the keyword are in alphabetical order. The final cipher text is achieved by going down each column and then writing out the letters in this new order.

M / A / R / K
D / V / D / D
D / D / D / V
F / G / F / D
D / V / D / D
A / V / X / G
A / D / G / X

Rearranged columns alphabetically

A / K / M / R
V / D / D / D
D / V / D / D
G / D / F / F
V / D / D / D
V / G / A / V
D / X / A / G

Final Ciphertext V D G V V D D V D D G X D D F D A A D D F D X G

The final cipher text would then be transmitted in Morse code, and the receiver would reverse the encryption process in order to retrieve the original text. The entire ciphertext is made up of just six letters, because these are the labels of the rows and columns of the initial 6 x 6 grid. The reason why these particular six letters were chosen is because the letters when transmitted into Morse code are highly dissimilar from one another. This choice minimizes the risk of errors during transmitting.

How would an eavesdropper go out attempting to crack this cipher? Any guesses? (Hint: Think about key length and previously mentioned techniques for simple mono-alphabetic substitution ciphers)

Let’s suppose an eavesdropper was able to obtain the alphabetic version of the key used to for transposition. Let’s say the alphabetic version of the key is A C R. Any guesses what the word is?

How could you strengthen the key used for transposition so that it is harder to guess?

Which is a stronger key, A C R or A A C H I L N O R S T U? Why?

Vigenère Cipher

Now it’s time to try and make the computer do all of the work for us. We are going to create a Python application that will encrypt and decrypt messages for us using the Vigenère Cipher.

Let’s start by creating a new Python script called “VigenereEncrypt.py.” First things first, we need to create a string variable to contain our plaintext message and another string variable to contain our key.

We should also create another empty string variable for our soon to be generated ciphertext.

Now it’s time to apply the Vigenère cipher. As a very brief refresher, the Vigenère cipher applies a letter shift based on the values of each individual character in the key. So in translating this into terms the computer will understand we need to create a loop that will cycle through the plaintext message.

In this loop we need to get the ASCII value of each character in the plaintext message and in the key. Since this is a little tricky and advanced the code is provided here:

plainLetterValue = bytearray(plaintext)[i] - 96

keyLetterValue = bytearray(key)[i % len(key)] - 96

This code is assuming your plaintext message is contained in the variable plaintext,key is contained in a variable named key, and the index of your loop is i.

The code above converts the character to ASCII code and then subtracts 96 from the ASCII character to make later manipulation easier. (The reason why we subtract 96 is so that the letter values will range from 1 to 26, which is how we as humans like to number the letters themselves.) When generating the keyLetterValue in order the cipher correctly works, we need to make sure the cipher keeps cycling over the key as the cipher is being applied to the entire message. To do this we modulus (find the remainder) the index of the loop by the length of the key which “cycle” the index through the key’s characters.

Now we need to calculate the cipher letter value. We can do this by simply adding plainLetterValue and keyLetterValue together.

Since adding these to values together can cause the cipher letter value from going above the limit of lower case letters we need to subtract 26 from the cipher letter value, if and only if the value of the cipher letter value is greater than 26.

Now to make matters easier when dealing with ASCII characters the following line of code should be included after dealing with the previous step.

cipherLetterValue += 96

At this point all we need to do is to convert the cipher letter value back to character form and add it to the ciphertext. We can use the built in Python function chr() to do this. To use chr(), place the name of the variable you want to convert from ASCII code to a character inside the parentheses. For instance, chr(cipherLetterValue) would return the converted character.

Now we just need to add the cipher character to the ciphertext string and we are done with the cipher code. Just do not forget to print out the completed ciphertext after your loop!

Go bug Dr. Healy or the lab TA to check if your cipher works properly at this point!

Phew... Now that we can encypt code, let’s decrypt the code.

This fortunately will be easier since we have the algorithm already working. Let’s create another Python file called “VigenereDecrypt.py”. Copy and paste your code from VigenereEncrypt.py into this new file.

All we need to do is reverse the cipher and it will decrypt code for us. To reverse the cipher we need to subtract the key value from the plaintext value and we need to add 26 to the cipher letter value if the cipher letter value is below 0 instead of greater than 26.

At this point, get Dr. Healy or the lab TA to double check your decryption cipher.