Playfair Cipher:: Program
Playfair Cipher:: Program
Playfair Cipher:: Program
Program:
#include <bits/stdc++.h>
using namespace std;
typedef struct{
int row;
int col;
}position;
if(flag[key[i]-'a'] == 0)
{
mat[x][y++] = key[i];
flag[key[i]-'a'] = 1;
}
if(y==5)
{
x++;
y=0;
}
}
if(flag[ch - 'a'] == 0)
{
mat[x][y++] = ch;
flag[ch - 'a'] = 1 ;
}
if(y==5)
{
x++;
y=0;
}
}
}
if(message[i] == 'j')
{
message[i] = 'i';
}
}
if(message.length()%2 != 0)
{
message += "x";
}
return message;
}
if( x1 == x2 )
{
ctext.append(1, mat[x1][(y1+1)%5]);
ctext.append(1, mat[x2][(y2+1)%5]);
}
else if( y1 == y2 )
{
ctext.append(1, mat[ (x1+1)%5 ][ y1 ]);
ctext.append(1, mat[ (x2+1)%5 ][ y2 ]);
}
else
{
ctext.append(1, mat[ x1 ][ y2 ]);
ctext.append(1, mat[ x2 ][ y1 ]);
}
}
return ctext;
}
if( x1 == x2 )
{
msg.append(1, mat[x1][ --y1<0 ? 4: y1 ]);
msg.append(1, mat[x2][ --y2<0 ? 4: y2 ]);
}
else if( y1 == y2 )
{
msg.append(1, mat[ --x1<0 ? 4: x1 ][y1]);
msg.append(1, mat[ --x2<0 ? 4: x2 ][y2]);
}
else
{
msg.append(1, mat[ x1 ][ y2 ]);
msg.append(1, mat[ x2 ][ y1 ]);
}
}
return msg;
}
int main()
{
int n; // number of keys
string plaintext;
string key[n];
generateMatrix(key[i]);
cout << "Key " << i+1 << " Matrix:" << endl;
for(int k=0;k<5;k++)
{
for(int j=0;j<5;j++)
{
cout << mat[k][j] << " ";
}
cout << endl;
}
cout << "Actual Message \t\t: " << plaintext << endl;