Collaboration Weeks 4 & 5

Note: you need regular expressions to do the mapping here, but you can write a lot of this with placeholders for the regex until we cover it in class.

The ADFGX cipher is a cipher used by the German Army in WWI. You can read all about it and its extensions online, including a good Wikipedia Article.

For this assignment, you will implement a simple version of the ADFGX ciper to encrypt and decrypt messages. Here's how the cipher works.

The letters ADFGX are written across the top of a grid and along the side. This provides 5X5 spaces, or 25 total spaces. This is about the same number of letters in the alphabet. Thus, we can put one letter in each square. For now, we will leave out the letter "X" to make things easy. Here is a sample grid

  A D F G X
A Q W E R T
D Y U I O P
F A S D F G
G J K L Z C
X B N M H V
To encode a message, you look up the message in the table and replace it with the pair of letter that index it, starting with the letter on the left followed by the letter on the top of the grid. For example, In this grid, the letter "R" would be replaced with "AG". As another example, the letter "B" would be replaced with "XA". You encrypt each letter of the message this way. Generally, spaces are removed and are not encrypted with this method.

To decode a message, you take the two letter pair and look up the corresponding single letter. If you had the pair "DG" it would correspond to the letter "O". You take each pair of letters and replace them with the correct letter they encode.

So, if we had the message "Attack", it would be encrypted as "FAAXAXFAGXGD". If we have the encrypted message "FAAXFFFAADXF" it would decrypt to "AtDawn". I've included capitalization here just to highlight the words. There is no need to do that when you actually decrypt.

For this exercise, write a program that can encrypt and decrypt using the ADFGX cipher. Use the grid above, but realize that in general any organizaiton of letters can be used in the grid. I should be able to run the program from the command line like this:

perl adfgx.pl encrypt "Our Defenses are Weakened"

And your program should output the encrypted version of the message (with the spaces removed).

I should also be able to decrypt like this

perl adfgx.pl decrypt "XGAFGFGFDGADDGAGGFFF"

And it shoud print out the decrypted version (in this case, "HELLOWORLD").

We will try a few other exercises later in the semester using this cipher, so it will be important to have a working version.

Note that you can test your code by encrypting some plain text and then running the output through the decrypt command. It should produce the plain text you started with.