-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path2_Playfair.cpp
318 lines (274 loc) · 6.86 KB
/
2_Playfair.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* C++ program to implement Playfair Cipher
*/
#include <bits/stdc++.h>
#include <cctype>
#include <algorithm>
// #include "./returnName.h"
using namespace std;
#define SIZE 30
void toLowerCase(char[], int);
int removeSpaces(char *, int);
void generateKeyTable(char[], int, char[][5]);
void search(char[][5], char, char, int[]);
int mod5encrypt(int);
int mod5decrypt(int);
int prepare(char[], int);
void encrypt(char[], char[][5], int);
void encryptByPlayfairCipher(char[], char[]);
void decrypt(char[], char[][5], int);
void decryptByPlayfairCipher(char[], char[]);
void displayKeyMatrix(char[][5]);
int main()
{
// generateHeader("Playfair Cipher Program");
char str[SIZE], key[SIZE];
char keyT[5][5];
int choice;
do
{
cout << "Menu:\n";
cout << "1. Encrypt\n";
cout << "2. Decrypt\n";
cout << "3. Exit\n";
cout << "Enter your choice (1/2/3): ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter key text: ";
cin >> key;
cout << "Enter plain text: ";
cin >> str;
// Generate and display the key matrix
generateKeyTable(key, strlen(key), keyT);
displayKeyMatrix(keyT);
// Encrypt and display the cipher text
encryptByPlayfairCipher(str, key);
transform(str, str + strlen(str), str, ::toupper);
cout << endl
<< "Cipher text: " << str << endl;
break;
case 2:
cout << "Enter key text: ";
cin >> key;
cout << "Enter cipher text: ";
cin >> str;
// Generate and display the key matrix
generateKeyTable(key, strlen(key), keyT);
displayKeyMatrix(keyT);
// Decrypt and display the plain text
decryptByPlayfairCipher(str, key);
transform(str, str + strlen(str), str, ::toupper);
cout << endl
<< "Decrypted text: " << str << endl;
break;
case 3:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice. Please enter 1, 2, or 3.\n";
break;
}
} while (choice != 3);
cin.get();
return 0;
}
// Function to convert the string to lowercase
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++)
{
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
// Function to remove all spaces in a string
int removeSpaces(char *plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}
// Function to generate the 5x5 key square
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0;
// a 26 character hashmap to store count of the alphabet
int dicty[26] = {0};
for (i = 0; i < ks; i++)
{
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++)
{
if (dicty[key[k] - 97] == 2)
{
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5)
{
i++;
j = 0;
}
}
}
for (k = 0; k < 26; k++)
{
if (dicty[k] == 0)
{
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5)
{
i++;
j = 0;
}
}
}
}
// Function to search for the characters of a digraph in the key square and return their position
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (keyT[i][j] == a)
{
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b)
{
arr[2] = i;
arr[3] = j;
}
}
}
}
// Function to find the modulus with 5
int mod5encrypt(int a) { return (a % 5); }
int mod5decrypt(int a)
{
if (a < 0)
a += 5;
return (a % 5);
}
// Function to make the plain text length to be even
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0)
{
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
// Function for performing the encryption
void encrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2)
{
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2])
{
str[i] = keyT[a[0]][mod5encrypt(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5encrypt(a[3] + 1)];
}
else if (a[1] == a[3])
{
str[i] = keyT[mod5encrypt(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5encrypt(a[2] + 1)][a[1]];
}
else
{
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// Function to encrypt using Playfair Cipher
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// Plaintext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
// Function to decrypt
void decrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2)
{
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2])
{
str[i] = keyT[a[0]][mod5decrypt(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5decrypt(a[3] - 1)];
}
else if (a[1] == a[3])
{
str[i] = keyT[mod5decrypt(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5decrypt(a[2] - 1)][a[1]];
}
else
{
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// Function to call decrypt
void decryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// ciphertext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
generateKeyTable(key, ks, keyT);
decrypt(str, keyT, ps);
}
// Function to display the key matrix
void displayKeyMatrix(char keyT[5][5])
{
cout << "Key Matrix:\n";
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
char capitalized = toupper(keyT[i][j]);
cout << capitalized << " ";
}
cout << endl;
}
}