forked from rakshitsharmaa/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair.cpp
293 lines (267 loc) · 5.41 KB
/
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
#include<iostream>
#include<string>
using namespace std;
class Playfair{
public:
char keyMatrix[5][5];
string getKey(){
string k;
cout<<"Enter the key:\n";
cin>>k;
return k;
}
string getMessage(){
string m;
cout<<"Enter the message:\n";
cin>>m;
return m;
}
//function for encryption
void encrypt(string msg,string key){
makeKeyMatrix(key);
cout<<"----------Encryption Process------------"<<endl;
string bg = getBigrams(msg);
int n = bg.length();
char ct[n];//to store cipher text
int x=0;
while(x<n){
int a_row=0,a_col=0,b_row=0,b_col=0;
char ca=' ',cb=' ';
getPosition(bg[x],bg[x+1],a_row,a_col,b_row,b_col);
//cout<<a_row<<","<<a_col<<","<<b_row<<","<<b_col;
if(a_row==b_row){
ca= keyMatrix[a_row][mod((a_col+1),5)];
cb= keyMatrix[b_row][mod((b_col+1),5)];
}
else if(a_col==b_col){
ca= keyMatrix[mod((a_row+1),5)][a_col];
cb= keyMatrix[mod((b_row+1),5)][b_col];
}
else{
ca= keyMatrix[a_row][b_col];
cb= keyMatrix[b_row][a_col];
}
cout<<bg[x]<<bg[x+1]<<"--->"<<ca<<cb<<endl;
ct[x]=ca;
ct[x+1]=cb;
x+=2;
}
cout<<"\n\n The cipher text is: \n" ;
for(int i=0;i<n;i++){
cout<<ct[i];
}
}
//function for decryption
void decrypt(string msg, string key){
makeKeyMatrix(key);
cout<<"----------Decryption Process------------"<<endl;
//string bg = getBigrams(msg);
int n = msg.length();
char pt[n];//to store plain text
int x=0;
while(x<n){
int a_row=0,a_col=0,b_row=0,b_col=0;
char pa=' ',pb=' ';
char c1 = toupper(msg[x]);
char c2 = toupper(msg[x+1]);
getPosition(c1,c2,a_row,a_col,b_row,b_col);
//cout<<a_row<<","<<a_col<<","<<b_row<<","<<b_col;
if(a_row==b_row){
pa= keyMatrix[a_row][mod((a_col-1),5)];
pb= keyMatrix[b_row][mod((b_col-1),5)];
}
else if(a_col==b_col){
pa= keyMatrix[mod((a_row-1),5)][a_col];
pb= keyMatrix[mod((b_row-1),5)][b_col];
}
else{
pa= keyMatrix[a_row][b_col];
pb= keyMatrix[b_row][a_col];
}
cout<<msg[x]<<msg[x+1]<<"--->"<<pa<<pb<<endl;
pt[x]=pa;
pt[x+1]=pb;
x+=2;
}
cout<<"\n\n The plain text is: \n" ;
for(int i=0;i<n;i++){
cout<<pt[i];
}
}
private:
string getBigrams(string msg){
int len = msg.length();
char a[len]; //character array of given text
int n=0;
char bigrams[len];
for(int i =0;i<len;i++){
if(msg[i]==' '){ //ignore space
continue;
}
else if(msg[i]=='j'||msg[i]=='J'){
a[n]= 'I'; //replace j or J with I
n++;
}else{
a[n]= toupper(msg[i]);
n++;
}
}
// to handle like balloon == ba lx lo on , and more
int k=0;
char filler='X';
bool filled=false;
for(int i=0;i<n;i+=2 ){
bigrams[k]=a[i];
k++;
if(i+1!=n){
if(a[i]==a[i+1] && filled== false){
bigrams[k]=filler;
k++;
bigrams[k]=a[i+1];
k++;
filled=true;
continue;
}else{
bigrams[k]=a[i+1];
k++;
}
}
}
//cout<<k;
if(k%2!=0) {
bigrams[k]=filler;
bigrams[k+1]='\0';
}else{
bigrams[k]='\0';
}
return bigrams;
}
//function to generate key matrix
void makeKeyMatrix(string text){
int len = text.length();
char ca[len]; //character array of given string excluding space
int n=0;
for(int i =0;i<len;i++){
if(text[i]==' '){
continue;
}
ca[n]= toupper(text[i]);
n++;
}
//array of alphabets
char alphabets[26];
for(int i=0;i<26;i++){
alphabets[i]=i+65;
}
char oneD[26];
int p=0;
oneD[0]=ca[0];
for(int i=1; i<n;i++){
bool duplicate=false;
for(int j=0;j<i;j++){
if(ca[i]==ca[j]){//avoid duplicates
duplicate=true;
break;
}
}
if(duplicate==false){
oneD[++p]=ca[i];
}
}
if(p<26){
for(int i = 0;i<26;i++){
bool duplicate=false;
for(int j=0;j<p;j++){
if(alphabets[i]==oneD[j]){
duplicate=true;
break;
}
}
if(duplicate==false){
oneD[++p]=alphabets[i];
}
}
//to store in keyMatrix
p=0;
for(int i= 0;i<5;i++){
for(int j= 0;j<5;j++){
if(oneD[p]=='J'){ //exclude J
p++;
}
keyMatrix[i][j]=oneD[p];
p++;
}
}
cout<<"The key matrix for Playfair Cipher for given key is: \n";
//to print keyMatrix
for(int i= 0;i<5;i++){
for(int j= 0;j<5;j++){
cout<<keyMatrix[i][j]<<" ";
}
cout<<"\n";
}
}
}
void getPosition(char a,char b,int &a_row,int &a_column,int &b_row, int &b_column) {
bool match_a =false;
bool match_b =false;
for(int i=0;i<5;i++){
for(int j =0;j<5;j++){
if(keyMatrix[i][j]==a && match_a!=true){
a_row= i;
a_column=j;
//cout<<keyMatrix[i][j];
match_a = true;
}
if(keyMatrix[i][j]==b && match_b!=true){
b_row= i;
b_column=j;
//cout<<keyMatrix[i][j];
match_b = true;
}
}
if(match_a && match_b){
break;
}
}
}
int mod(int a, int b){ // to handle modulo operation
int c = a%b;
if(c>=0)
return c;
else
return b+c;
}
};
int main(){
cout<<"=======PALYFAIR CIPHER=========="<<endl<<endl;
int choice;
char more='y';
Playfair p;
string m,k;
while(more=='y'){
cout<<"Enter your choice as following :\n";
cout<<"1. Press 1 for encryption "<<endl;
cout<<"2. Press 2 for decryption "<<endl;
cout<<"3. Press 3 for exit "<<endl;
cin>>choice;
switch(choice){
case 1:
k= p.getKey();
m= p.getMessage();
p.encrypt(m,k);
break;
case 2:
k= p.getKey();
m= p.getMessage();
p.decrypt(m,k);
break;
case 3:
exit(1);
default:
cout<<"Invalid choice!!"<<endl;
}
cout<<"\n\n Do you want to continue?(press y if you want)"<<endl;
cin>>more;
}
}