Repository navigation
Expand file tree
/
Copy pathPlayfairCipher.cpp
More file actions
191 lines (163 loc) · 4.52 KB
/
Copy pathPlayfairCipher.cpp
File metadata and controls
191 lines (163 loc) · 4.52 KB
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
#include<bits/stdc++.h>
using namespace std;
#define DIM_SIZE 5
template<typename T>
void printMatrix(vector<vector<T>> matrix){
cout<<"=============Printing Matrix====================="<<endl;
for(int i = 0; i < matrix.size(); i++){
for(int j = 0; j < matrix[0].size(); j++){
cout<<(char)(matrix[i][j]+'a')<<" ";
}
cout<<endl;
}
cout<<"================================================="<<endl;
}
class PlayfairCipher{
private:
string key;
vector<vector<int>> keyMatrix;
void preprocessInput();
void generateKeyMatrix();
vector<pair<int, int>> createDigraph(const string &);
pair<int, int> getPos(int );
public:
PlayfairCipher(string key){
this->key = key;
generateKeyMatrix();
}
string encrypt(const string &);
string decrypt(const string &);
void setKey(string key){
this->key = key;
generateKeyMatrix();
}
};
/**
Currently the key matrix skips 'j' as it is traditionally followed.
*/
void PlayfairCipher::generateKeyMatrix(){
vector<int> hsh(26, 0);
preprocessInput();
keyMatrix = vector<vector<int>>(DIM_SIZE, vector<int>(DIM_SIZE));
int row = 0, col = 0;
for(auto& it: key){
if(hsh[it-'a'] == 0){
keyMatrix[row][col++] = it-'a';
hsh[it-'a']++;
if(col == DIM_SIZE){
col = 0;
row++;
}
}
}
for(char c = 'a'; c <= 'z'; c++){
if(c=='j' and !hsh[c-'a']) continue;
else if(c=='i' and hsh['j'-'a']) continue;
if(hsh[c-'a'] == 0){
keyMatrix[row][col++] = c - 'a';
if(col == DIM_SIZE){
col = 0;
row++;
if(row == DIM_SIZE) break;
}
}
}
printMatrix(keyMatrix);
}
/**
Preprocessing of the input strings plainText and key.
1. if length of plainText is odd, then it is made even by appending 'z' at the end.
2. Converting uppercase letters to lowercase.
*/
void PlayfairCipher::preprocessInput(){
for(auto& it: key){
if(it>='A' and it<='Z'){
it += ('a' - 'A');
}
}
}
vector<pair<int, int>> PlayfairCipher::createDigraph(const string &plainText){
string textCache(plainText.begin(), plainText.end());
if(textCache.size() & 1) textCache += 'z';
vector<pair<int, int>> digraphList;
for(int i = 0; i < plainText.size(); i += 2){
digraphList.push_back({textCache[i], textCache[i+1]});
}
return digraphList;
}
pair<int, int> PlayfairCipher::getPos(int c){
for(int i = 0; i < DIM_SIZE; i++){
for(int j = 0; j < DIM_SIZE; j++){
if(keyMatrix[i][j] == c){
return {i, j};
}
}
}
return {-1, -1};
}
/**
Encryption routine of Playfair algorithm
*/
string PlayfairCipher::encrypt(const string &plainText){
vector<pair<int, int>> digraphList = createDigraph(plainText);
char enc1, enc2;
string encryptedText = "";
for(auto& digraph: digraphList){
pair<int, int> pos1 = getPos(digraph.first - 'a');
pair<int, int> pos2 = getPos(digraph.second - 'a');
if(pos1.first==pos2.first){
//same row
enc1 = keyMatrix[pos1.first][(pos1.second+1)%DIM_SIZE];
enc2 = keyMatrix[pos2.first][(pos2.second+1)%DIM_SIZE];
}
else if(pos1.second == pos2.second){
//same column
enc1 = keyMatrix[(pos1.first+1)%DIM_SIZE][pos1.second];
enc2 = keyMatrix[(pos2.first+1)%DIM_SIZE][pos2.second];
}
else{
enc1 = keyMatrix[pos1.first][pos2.second];
enc2 = keyMatrix[pos2.first][pos1.second];
}
encryptedText += (enc1 + 'a'); encryptedText += (enc2+'a');
}
return encryptedText;
}
string PlayfairCipher::decrypt(const string &encryptedText){
vector<pair<int, int>> digraphList = createDigraph(encryptedText);
char dec1, dec2;
string decryptedText = "";
for(auto& digraph: digraphList){
pair<int, int> pos1 = getPos(digraph.first - 'a');
pair<int, int> pos2 = getPos(digraph.second - 'a');
if(pos1.first == pos2.first){
dec1 = keyMatrix[pos1.first][(pos1.second-1+DIM_SIZE)%DIM_SIZE];
dec2 = keyMatrix[pos2.first][(pos2.second-1+DIM_SIZE)%DIM_SIZE];
}
else if(pos1.second == pos2.second){
dec1 = keyMatrix[(pos1.first-1+DIM_SIZE)%DIM_SIZE][pos1.second];
dec2 = keyMatrix[(pos2.first-1+DIM_SIZE)%DIM_SIZE][pos2.second];
}
else{
dec1 = keyMatrix[pos1.first][pos2.second];
dec2 = keyMatrix[pos2.first][pos1.second];
}
decryptedText += (dec1 + 'a'); decryptedText += (dec2 + 'a');
}
return decryptedText;
}
int main(){
string plainText, key;
cout<<"Enter plainText: ";
cin>>plainText;
cout<<"Enter key: ";
cin>>key;
bool isOdd = (plainText.size() & 1);
PlayfairCipher cipher(key);
string encryptedText = cipher.encrypt(plainText);
cout<<"Encrypted text: "<<encryptedText<<endl;
string decryptedText = cipher.decrypt(encryptedText);
if(isOdd) decryptedText.pop_back();
cout<<"Decrypted Text: "<<decryptedText<<endl;
return 0;
}