exercise 5 4

Assignment: Exercise 5
Windows App: Modified Playfair Cipher


The objectives of this lab assignment are as follows:

  • Create a fully functioning desktop application
  • Convert plain text to encrypted text using a modified version of the Playfair Cipher

Skills Required

To properly complete this assignment, you will need to apply the following skills:

  • Construct Windows desktop GUI application
  • Work with strings as arrays of characters
  • Work with 2D arrays
  • Implement logic in methods
  • Implement nested loops (this is a must when working with 2D arrays)

Supplementary Information

Understanding the Playfair Cipher:

You might want to take a look at the following websites to gain a better understanding of this cipher:

  • Wikipedia Article
  • Rumkin.com

Please note, however, that what you read at the above sites will be the rules for the cipher as it exists, not as we are going to implement it in this assignment. We are going to modify the “rules” of the cipher, making our work a little easier.

Understanding 2D-Arrays:

  • Dotnetperls.com (make sure to understand how to define a 2D array with “no initializer.” You will need to create the array as an empty 5×5 matrix and then fill it in.)
  • ICT.social
  • Youtube–Working with 2D arrays and nested loops

Assignment User Story

As a user, I want a new Windows application that is capable of performing encryption and decryption using a modified Playfair Cipher.

Assignment Requirements

“For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them.” – taken from The Code Book: The Science of Secrecy from Ancient Egypt to Quantum Cryptography by Simon Singh.

The idea for this problem comes from this book. You will encrypt and decrypt some messages using a simplified version of code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case letters. We will follow this convention for this problem. We will only encrypt/decrypt letters. Any other characters including numbers and punctuation will be left as is (i.e. 1 2 3 . , – etc).

From the user, you need only two pieces of information:

  1. A keyword (no spaces, no non-letter characters, no punctuation).
  2. Text that you wish either to encrypt or decrypt.

The Playfair Cipher

The Playfair Cipher is a substitution cipher. It is a little more complex than what we are going to attempt, but the lesson here isn’t about recreating a cipher that was developed in the mid-19th century. This is all about arrays (both single and 2-dimensional) and treating strings of text as arrays of char characters.

The original Playfair uses a digraph substitution method. This means that it encrypts pairs of letters rather than single letters used by simpler substitution methods. Ours will be more like a simple substitution method.

The Playfair cipher is not capable of encrypting numbers or punctuation—only alphabetic letters. Therefore, this typically was used for important but not critical information or to relay information about events that were immanent knowing that it would take opposing forces at least a couple of hours to crack the code if they could at all.

The Playfair Matrix

The Playfair cipher uses a 5×5 table or matrix into which 25 of the 26 letters of the alphabet are loaded. This means that one letter will have to represent two letters in the alphabet. For example, i and j can be combined as the letter i.

The order in which the letters are loaded into the matrix is determined by a keyword that must be known to the sender and the receiver of the message. The non-duplicating letters of the keyword are loaded into the matrix first, from left-to-right, starting at the top row. Then all the remaining unused letters of the alphabet are loaded into the matrix in their normal order. This means that you must drop duplicate letters.

Example Playfair Cipher Matrix

The Wikipedia article on the Playfair Cipher shows the matrix with the keyword PLAYFAIR EXAMPLE. This is a long key, but it doesn’t matter because we need to get all the letters of the alphabet into the matrix anyway. We must drop the duplicate letters in this keyword and eliminate the space between the two words. Hence, our matrix would look as shown here.

Example Playfair Cipher Matrix

If, however, the keyword is WINTER, then the matrix should be loaded as shown to the left.

Example Playfair Cipher Matrix

In one further example, if our keyword is FERRET, then the matrix should be loaded as shown to the right. Notice that we drop the duplicate letters R and E, resulting in the first four letters of our keyword being FERT. Then, the remainder of the alphabet is loaded into the matrix ensuring we don’t duplicate any letters.

Note: While my images show the letters I and J joined together, we basically change the letter J to I when handling our encryption.

Encrypting a Phrase

Now that we have created the matrix we can both encrypt and decrypt a string of text/phrase. The way we are constructing our matrix is identical with the mechanics of the Playfair Cipher. What we are changing for our assignment is the way a string is encrypted. Here, the rules in the actual cipher are a little more complex than in our assignment.

Let’s suppose that we wish to encrypt the following phrase: we attack at zero six hundred hours.

We move through this phrase one letter/character at a time. First, we start with the letter w and note where in the matrix this letter is found. In this case, using the previous matrix example containing the keyword FERRET, we note the first letter of our phrase is found in row 4 and column 1 [4, 1]. We reverse these values such that we have [1, 4] and the letter at that location is H. Hence, we change the letter w to the letter H.

Continuing in such fashion our phrase ” we attack at zero six hundred hours. ” is translated to:


Additional Technical Rules

  1. The letter j must be treated as though it were the letter i.
  2. Any character that cannot be found in the matrix, such as punctuation and numeric values, will simply be passed into the encrypted string unchanged.
  3. Following rule #2 above, spaces will be maintained from the original phrase to the encrypted string. In the traditional Playfair Cipher, spaces, punctuation, and numbers would be skipped (dropped from the message).

Technical/Grading Requirements

You must ensure that the application you submit meets all technical/grading requirements. Your grade will be based on how well your application follows the application requirements plus the following:

  • A Windows Forms (desktop) application will be developed.
  • Both the keyword and phrase text boxes will be validated to ensure they are not empty.
  • In addition to the button to perform the translation you will also have a button to clear the text from the textbox controls and a separate button to end the application.
  • Internally, the matrix must be represented as a 2-Dimensional array in C#.
  • You must break your code into meaningful methods each that performs only one function. For example, you might have a method that loads the matrix with its letters, a separate method that iterates through the phrase, and another to which is passed a letter and returns to the previous method the translated letter. These are only examples, and your implementation will depend on how you decide to tackle this problem.

As a final note to the above requirements, because this is a Windows desktop application you must submit a zip of the entire project folder for grading purposes. A desktop application contains multiple files beyond just the code file that must be kept together.

How the application looks and how it presents its information to the user is up to you. But it must meet the minimum requirements noted above.

Final Thoughts

This entire exercise is intended to be an exercise in arrays (both single and 2-Dimensional), as well as in handling strings and individual char letters.

In addition, we want to begin to think about best programming practices including the use of methods and adherence to two additional principles:

  1. DRY: Don’t Repeat Yourself.
  2. Single Responsibility Pattern.

Certainly, get your code working, and then work to refactor it to observe the above two principles.

The first principle means that you don’t want duplicated code; this is the basis of the DRY principle. If you find yourself repeating the same or very similar code in two or more places in your application, then this might be code that belongs in one or more methods.

With respect to your methods, you want to observe the second principle. This actually is a pattern and is one of the bedrocks of object-oriented programming. Any method should have only one reason for change. This generally is interpreted as meaning that any method should have only one purpose. While this isn’t technically accurate we aren’t at the place in the semester where we can adequately explain what “reason for change” means so we will stick to this pattern meaning “single purpose.”

Example Output

The example output provided below is simply an example. You do not need to format your output to look like this example. You simply need to ensure that you have met the technical requirements for this application.

Following is example output for this application:

Example Application Output 1

In this example, we have taken the output text from above and are using it as the input text demonstrating that the same application and methods can encrypt as well as decrypt messages:

Example Application Output 2


"Is this question part of your assignment? We Can Help!"

Hi there! Click one of our representatives below and we will get back to you as soon as possible.

Chat with us on WhatsApp