forked from rinovethamoses97/cryptogrpahy-lab-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair.cpp
140 lines (136 loc) · 2.58 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
#include<bits/stdc++.h>
using namespace std;
int get_indexi(char a,char key_matrix[5][5]){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(key_matrix[i][j]==a){
return i;
}
}
}
}
int get_indexj(char a,char key_matrix[5][5]){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(key_matrix[i][j]==a){
return j;
}
}
}
}
int main(){
cout<<"Enter the key";
string key;
cin>>key;
set<char> unique_key_set;
for(int i=0;i<key.length();i++){
unique_key_set.insert(key[i]);
};
char key_matrix[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
key_matrix[i][j]='-';
}
}
int length=unique_key_set.size();
set<char>::iterator irr;
char unique_key[length];
int k=0;
for(int i=0;i<key.length();i++){
if(unique_key_set.find(key[i])!=unique_key_set.end()){
unique_key[k]=*unique_key_set.find(key[i]);
unique_key_set.erase(unique_key_set.find(key[i]));
k++;
}
}
k=0;
int size=length;
for(int i=0;i<(size/5)+size%5;i++){
for(int j=0;j<5;j++){
if(length>0){
key_matrix[i][j]=unique_key[k];
length--;
k++;
}
else{
break;
}
}
}
char temp='a';
length=sizeof(unique_key)/sizeof(char);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(key_matrix[i][j]=='-'){
int flag=1;
for(int k=0;k<length;k++){
if(temp==unique_key[k]){
flag=0;
break;
}
}
if(flag==1){
key_matrix[i][j]=temp;
temp+=1;
if(temp=='q')
temp+=1;
}
else{
i--;
j--;
temp+=1;
if(temp=='q')
temp+=1;
}
}
}
}
cout<<"Key Matrix"<<endl;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<key_matrix[i][j];
}
cout<<endl;
}
string cipher="";
cout<<"Enter the plain text";
string plain;
cin>>plain;
if(plain.length()%2!=0){
plain+='z';
}
string t=plain;
plain="";
for(int i=0;i<t.length();i+=2){
if(t[i]==t[i+1]){
plain+=t[i];
plain+=t[i]+1;
plain+=t[i];
}
else{
plain+=t[i];
plain+=t[i+1];
}
}
for(int i=0;i<plain.length();i+=2){
int first_i=get_indexi(plain[i],key_matrix);
int first_j=get_indexj(plain[i],key_matrix);
int second_i=get_indexi(plain[i+1],key_matrix);
int second_j=get_indexj(plain[i+1],key_matrix);
if(first_j==second_j){
//same column
cipher+=key_matrix[(first_i+1)%5][first_j];
cipher+=key_matrix[(second_i+1)%5][first_j];
}
else if(first_i==second_i){
//same row
cipher+=key_matrix[first_i][(first_j+1)%5];
cipher+=key_matrix[first_i][(second_j+1)%5];
}
else{
cipher+=key_matrix[first_i][second_j];
cipher+=key_matrix[second_i][first_j];
}
}
cout<<"Encrypted Text"<<cipher;
}