In my previous article I explained the Hill Cipher, in today’s module we will learn one more substitution cipher technique that is – Playfair Cipher.
Playfair cipher is a multi- alphabet letter encryption cipher, which deals with letters in plaintext as single units and renders these units into Ciphertext letters. The Playfair algorithm is based on the use of a 5X5 matrix of letters built using a keyword.
5x5 Matrix
Now the question is how to fill that 5x5 matrix? - To fill that, we need a keyword or a message, after that you fill the letters of the keyword into the 5x5 matrix from left to right and from top to bottom and then fill the remainders of the matrix with the remaining letters in alphabetic order.
The letter I and J are treated as one letter and they are settled in the same box of the matrix like this - I/J. Plaintext is encrypted two letters at a time, so initially you have to pair the plaintext.
Rules to encrypt the message
Step1
Reoccurring plaintext letters that are in the same pair have to come apart with a filler such as “X”, let’s say Balloon is there for the message, so how are we going to proceed?
Here you can see we paired the plaintext, and we also can see that repeated letters are paired with a filler, like a pair in “Balloon” - -> “LL” is replaced with filler -- > “X” à “LX” and “LO”.
Step2
Two plaintext letters that drop in the same [ROW] in the matrix, have to be replaced with the letter to the right. Let’s say for example = “Balloon” is your plaintext.
Now if you want to encrypt the pair of [X, O] - -> than it will be like this - > as [X, O] falls in the same row, then you have to replace the letter to the right, that is [O, B].
[X, O] - - > [O, B]
Step 3
The above step is for the Row Matrix, this step will show you the process of Column matrix. Let’s say two plaintext letters that drop in the same [Column] in the matrix have to be replaced with the letter beneath, with the last element of the column circularly follow the top element.
The step shows how to encrypt a pair - > [B, U], the step says you have to replace the letter beneath the letter you want to encrypt, with the last element of the column circularly following the top element.
[B, U] - -> [N, B]
Step4
Otherwise you just have to find the corners of the letter to encrypt, let’s you want to encrypt [H, S] letter, then just find out the corner, you get your encrypted letter.
Example based on Playfair Cipher
Let’s say we want to encrypt the plaintext – “MONARCHY”.
Now you have encrypt the pair [M, O] || [N, A] || [R, C] || [H, Y]. Apply all the steps of Playfair cipher that we discuss above, you get the cipher text as shown below:
Implementation of Playfair Cipher
- #
- include < stdio.h > #include < conio.h > #include < string.h > #include < ctype.h > #define MX 5
- void playfair(char ch1, char ch2, char key[MX][MX]) {
- int i, j, w, x, y, z;
- FILE * out;
- if ((out = fopen("cipher.txt", "a+")) == NULL) {
- printf("File Currupted.");
- }
- for (i = 0; i < MX; i++) {
- for (j = 0; j < MX; j++) {
- if (ch1 == key[i][j]) {
- w = i;
- x = j;
- } else if (ch2 == key[i][j]) {
- y = i;
- z = j;
- }
- }
- }
-
- if (w == y) {
- x = (x + 1) % 5;
- z = (z + 1) % 5;
- printf("%c%c", key[w][x], key[y][z]);
- fprintf(out, "%c%c", key[w][x], key[y][z]);
- } else if (x == z) {
- w = (w + 1) % 5;
- y = (y + 1) % 5;
- printf("%c%c", key[w][x], key[y][z]);
- fprintf(out, "%c%c", key[w][x], key[y][z]);
- } else {
- printf("%c%c", key[w][z], key[y][x]);
- fprintf(out, "%c%c", key[w][z], key[y][x]);
- }
- fclose(out);
- }
- void main() {
- int i, j, k = 0, l, m = 0, n;
- char key[MX][MX], keyminus[25], keystr[10], str[25] = {
- 0
- };
- char alpa[26] = {
- 'A',
- 'B',
- 'C',
- 'D',
- 'E',
- 'F',
- 'G',
- 'H',
- 'I',
- 'J',
- 'K',
- 'L',
- 'M',
- 'N',
- 'O',
- 'P',
- 'Q',
- 'R',
- 'S',
- 'T',
- 'U',
- 'V',
- 'W',
- 'X',
- 'Y ',
- 'Z'
- };
- printf("\nEnter key:");
- gets(keystr);
- printf("\nEnter the plain text:");
- gets(str);
- n = strlen(keystr);
-
- for (i = 0; i < n; i++) {
- if (keystr[i] == 'j') keystr[i] = 'i';
- else if (keystr[i] == 'J') keystr[i] = 'I';
- keystr[i] = toupper(keystr[i]);
- }
-
- for (i = 0; i < strlen(str); i++) {
- if (str[i] == 'j') str[i] = 'i';
- else if (str[i] == 'J') str[i] = 'I';
- str[i] = toupper(str[i]);
- }
-
- j = 0;
- for (i = 0; i < 26; i++) {
- for (k = 0; k < n; k++) {
- if (keystr[k] == alpa[i]) break;
- else if (alpa[i] == 'J') break;
- }
- if (k == n) {
- keyminus[j] = alpa[i];
- j++;
- }
- }
-
- k = 0;
- for (i = 0; i < MX; i++) {
- for (j = 0; j < MX; j++) {
- if (k < n) {
- key[i][j] = keystr[k];
- k++;
- } else {
- key[i][j] = keyminus[m];
- m++;
- }
- printf("%c ", key[i][j]);
- }
- printf("\n");
- }
-
- printf("\n\nEntered text :%s\nCipher Text :", str);
- for (i = 0; i < strlen(str); i++) {
- if (str[i] == 'J') str[i] = 'I';
- if (str[i + 1] == '\0') playfair(str[i], 'X', key);
- else {
- if (str[i + 1] == 'J') str[i + 1] = 'I';
- if (str[i] == str[i + 1]) playfair(str[i], 'X', key);
- else {
- playfair(str[i], str[i + 1], key);
- i++;
- }
- }
- }
- getch();
- }
Output window Hope you like it, Thank you for Reading, Have a good Day!