Playfair Cipher Implementation in C
The Playfair cipher is a manual symmetric encryption technique and was the first literal diagram substitution cipher. The technique encrypts pairs of letters, instead of single letters as in the simple substitution cipher and rather more complex Vigenère cipher systems then in use. The Playfair is thus significantly harder to break since the frequency analysis used for simple substitution ciphers does not work with it.
This article does not cover the operation of the Playfair cipher. We suggest going through the simple explanation given on Wikipedia for a detailed step-by-step explanation.
Implementation:
Following is the implementation of Playfair cipher in C. The program expects two input files – Playfair.txt, which contains the plain text, and key.txt, which contains the key.
Use Playfair example as the key to encrypt the message HIDE THE GOLD IN THE TREE STUMP. After encryption, the message becomes BM OD ZB XD NA BE KU DM UI XM MO UV IF (Breaks included for ease of reading the ciphertext).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
#include <stdio.h> #include <stdlib.h> FILE *in, *kp; char input[100], key[50]; char matrix[5][5]; char arr[100]; int j = 0; int count = 0; void encrypt(char a, char b) { static int i = 0; arr[i] = a; arr[i + 1] = b; char c; if (a == b) { c = b; arr[i + 1] = 'X'; arr[i + 2] = c; } int p, q, r, s; for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { if (arr[j] == matrix[x][y]) { p = x; q = y; } } } for (int u = 0; u < 5; u++) { for (int t = 0; t < 5; t++) { if (arr[j + 1] == matrix[u][t]) { r = u; s = t; } } } if (q == s) { printf("%c %c -> %c %c", a, b, matrix[(p + 1) % 5][q], matrix[(r + 1) % 5][q]); } else if (p == r) { printf("%c %c -> %c %c", a, b, matrix[p][(q + 1) % 5], matrix[p][(s + 1) % 5]); } else { printf("%c %c -> %c %c", a, b, matrix[p][s], matrix[r][q]); } if (a == b) { i = i + 3; } else { i = i + 2; } j = j + 2; } void readkey() { int i = 0; char ch; while (!feof(kp)) { ch = getc(kp); key[i++] = ch; } printf("\n"); for (int k = 0; k < i; k++) { printf("%c", key[k]); } printf("\n"); int k = 0; char inp[26]; int flag[26] = { 0 }; int index = 0; int x = strlen(key) - 1; while (x) { if (key[k] != ' ') { if (flag[key[k] - 65] != 1) { inp[index] = key[k]; index++; flag[key[k] - 65] = 1; } } k++; x--; } for (int t = 0; t < 26; t++) { if (flag[t] != 1) { flag[t] = 1; if ((t + 65) != 'J') { inp[index] = t + 65; index++; } } } k = 0; printf("\n"); for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { matrix[x][y] = inp[k++]; printf("%c ", matrix[x][y]); } printf("\n"); } printf("\n"); } void readfile() { char ch; int k, i, n; int size; size = ftell(in); rewind(in); char a, b; k = size / 2; n = k; int result; char* ptr; while (k) { ptr = (char*)malloc(sizeof(char) * 2); if (ptr == NULL) { fputs("Memory Error", stderr); exit(2); } result = fread(ptr, 1, 2, in); if (result != 2) { exit(3); } a = (*ptr); b = *(ptr + 1); while (a == ' ') { a = b; b = getc(in); } while (b == ' ') { b = getc(in); } encrypt(a, b); printf("\n"); count = count + 2; free(ptr); k--; } } void padinput() { fseek(in, 0, SEEK_END); int size = ftell(in); if (size % 2) { fputc('X', in); } } int main() { // Use plain text "HIDE THE GOLD IN THE TREE STUMP" in = fopen("Playfair.txt", "r+"); if (in == NULL) { printf("Error while opening the file.\n"); exit(1); } // Use "Playfair example" as the key to encrypt the message kp = fopen("key.txt", "r"); if (kp == NULL) { printf("Error while opening the file.\n"); exit(1); } padinput(); readkey(); readfile(); while (j != count) { if (arr[j + 1] != '\0') { encrypt(arr[j], arr[j + 1]); } } fclose(in); return 0; } |
The above program is tested in the Windows environment using Code::Blocks 16.01.
That’s all about Playfair Cipher implementation in C.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)