The Playfair cipher is a historical encryption algorithm that was invented in 1854 by Charles Wheatstone, but named after Lord Playfair who promoted its use. It is a manual symmetric encryption technique that encrypts pairs of letters (digraphs) instead of single letters, making it harder to break with frequency analysis. It was used by the British and Australian forces in the Second Boer War and World War I and II for tactical purposes, as it was fast and easy to use without special equipment.
To begin, generate a key square, which is a 5x5 grid of letters. It should consist of the unique letters of the key, followed by the remaining letters of the alphabet in order. Omit one letter, usually J, to fit the 25 letters in the grid.
R | O | C | K | A |
---|---|---|---|---|
B | D | E | F | G |
H | I / J | L | M | N |
P | Q | S | T | U |
V | W | X | Y | Z |
Next, split your plaintext into pairs of letters (digraphs).
- If there is an odd number of letters, add a filler letter, usually Z, at the end.
- If there are any repeated letters in a digraph, insert a filler letter between them.
Then, apply the encryption rules for each digraph according to its positions in the key square.
- If both letters are in the same column, take the letter below each one (wrapping around to the top if at the bottom).
- If both letters are in the same row, take the letter to the right of each one (wrapping around to the left if at the rightmost position).
- If neither of the above rules is true, form a rectangle with the two letters and take the letters on the opposite corners of the rectangle.
BA -> GR
SE -> XL
To decrypt the ciphertext using the Playfair cipher, you need to use the same key square and apply the inverse rules for each digraph.
First, split your ciphertext into pairs of letters (digraphs).
Next, apply the inverse rules for each digraph according to its positions in the key square.
- If both letters are in the same column, take the letter above each one (wrapping around to the bottom if at the top).
- If both letters are in the same row, take the letter to the left of each one (wrapping around to the right if at the leftmost position).
- If neither of the above rules is true, form a rectangle with the two letters and take the letters on the opposite corners of the rectangle.
- Then, join the decrypted digraphs together to get your plaintext.
- Finally, remove any filler letters that were added during encryption.
GRXL -> GR XL
GR -> BA
XL -> SE
BASE -> BASE
You can use this algorithm to encrypt the data between two users or to encrypt your database. This way, even if you get hacked, nobody can understand your data.
.
βββ ...
βββ PlayFair-Cipher_Algorithm # The main Folder That Contains our project.
β βββ cipher # Compiled File of our `cipher.c`.
β βββ cipher.c # This is where our source code has been written.
β βββ README.md # The explanation folder that you are reading right now π
.
βββ ...
I have made an explanation for every single method and line of code inside cipher.c
.
You can see that I have not distributed cipher.c
in sub-files because I see that there is no actual need for that!
- DATA STRUCTURES
- POINTERS AND MEMORY ALLOCATION
- STRUCTS
- NESTED CALLS METHODS
These approaches I take to make the code more (readable, and simple) so it can be understandable for everyone have read this algorithm. You might see that sometimes I am implementing similar code in a different way, This is just For "diversity" so there is nothing specific.
Another thing to mention is that my code can encrypt and decrypt more than one word, which means you can encrypt and decrypt clauses.
My code can also encrypt and decrypt files, all you need is to add the functionality of reading the files and send the words that you read from files word by word as an argument to theencrypt(char **word)
method if you want to encrypt and thedecrypt(char **word)
method for decryption, these methods will handle everything and store the encrypted and decrypted words inside the linked list you can after that callprint_list()
method to print the whole words.
The goal of my program is to use the Playfair algorithm to encrypt the data while providing a friendly CLI to interact with the users. This program is very useful for students who are studying various algorithms because it shows what the 5x5 matrix looks like and it displays the substitution for every single letter. This makes it easy to track and understand the encryption process. Also, using this program will save their time, because they can see how the key matrix and the cipher text are generated quickly. They can also understand the source code and see how they can apply this algorithm in various programs or applications, such as encrypting databases as I mentioned before.
I was curious about securing the data and I was reading about various algorithms. When I saw this algorithm and its story, it sounded really interesting to me and a bit challenging. At the same time, I wanted to improve my skills in C language because I love it and I thought that this algorithm was suitable. Another thing that I love is the things that are related to computer science and complexity, more than the commercial aspects.
- I improved my skills in dealing with arrays and iterating over them.
- I gained more practice in using malloc and allocating memory dynamically.
- I became more accurate and had a better understanding of pointers and memory management.
- I improved my understanding of structs and how to benefit from them.
- I enhanced my debugging skills and learned how to fix errors and bugs.
- I understood how to create a friendly CLI program that interacts with the users.
- I developed my logic and learned something new about securing the data and a bit about the history of this algorithm and some others like Caesar and Hill cipher algorithms.