#include<iostream>
#include<string.h>
using namespace std;
class RailFence
{
char plain[55],cipher[55],matrix[9][9];
int pl,kl,i,j,k,l,n,m,temp,key[11];
public:
RailFence(){}
void GetPlain()
{
cout<<endl<<"\t\t ***ENTER PLAIN TEXT***"<<endl;
gets(plain);
pl=strlen(plain);
}
void GetKey()
{
cout<<endl<<"\t\t ***ENTER KEY SIZE***"<<endl;
cin>>kl;
cout<<endl<<"\t\t ***ENTER KEY***"<<endl;
for(i=0;i<kl;i++)
{
cin>>key[i];
}
cout<<"KEY IS::";
for(i=0;i<kl;i++)
{
cout<<"\t"<<key[i];
}
cout<<endl;
}
void PlainManipulation()
{
if(pl%kl!=0)
{
temp=((pl/kl)+1)*kl;
for(j=pl;j<temp;j++)
{
plain[j]='x';
}
plain[temp]='\0';
}
else
{
temp=pl;
}
}
void MakeMatrix()
{
k=0;
cout<<"\t";
for(i=0;i<(temp/kl);i++)
{
for(j=0;j<kl;j++,k++)
{
matrix[i][j]=plain[k];
cout<<"\t"<<matrix[i][j];
}
cout<<endl;
cout<<"\t";
}
}
void Encryption()
{
k=1;
m=0;
for(i=0;i<kl;i++)
{
for(j=0;j<kl;j++)
{
if(key[j]==k)
{
k++;
for(l=0;l<temp/kl;l++)
{
for(n=0;n<kl;n++)
{
if(j==n)
{
cipher[m]=matrix[l][n];
m++;
}
}
}
}
}
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t ***CIPHER TEXT IS***"<<endl<<endl<<"\t\t";
for(i=0;i<temp;i++)
{
cout<<cipher[i];
}
cout<<endl<<endl<<"\t\t ***END***"<<endl<<endl;
}
};
int main()
{
RailFence R1;
R1.GetPlain();
R1.GetKey();
R1.PlainManipulation();
R1.MakeMatrix();
R1.Encryption();
R1.DisplayCipher();
return 0;
}
#include<string.h>
using namespace std;
class RailFence
{
char plain[55],cipher[55],matrix[9][9];
int pl,kl,i,j,k,l,n,m,temp,key[11];
public:
RailFence(){}
void GetPlain()
{
cout<<endl<<"\t\t ***ENTER PLAIN TEXT***"<<endl;
gets(plain);
pl=strlen(plain);
}
void GetKey()
{
cout<<endl<<"\t\t ***ENTER KEY SIZE***"<<endl;
cin>>kl;
cout<<endl<<"\t\t ***ENTER KEY***"<<endl;
for(i=0;i<kl;i++)
{
cin>>key[i];
}
cout<<"KEY IS::";
for(i=0;i<kl;i++)
{
cout<<"\t"<<key[i];
}
cout<<endl;
}
void PlainManipulation()
{
if(pl%kl!=0)
{
temp=((pl/kl)+1)*kl;
for(j=pl;j<temp;j++)
{
plain[j]='x';
}
plain[temp]='\0';
}
else
{
temp=pl;
}
}
void MakeMatrix()
{
k=0;
cout<<"\t";
for(i=0;i<(temp/kl);i++)
{
for(j=0;j<kl;j++,k++)
{
matrix[i][j]=plain[k];
cout<<"\t"<<matrix[i][j];
}
cout<<endl;
cout<<"\t";
}
}
void Encryption()
{
k=1;
m=0;
for(i=0;i<kl;i++)
{
for(j=0;j<kl;j++)
{
if(key[j]==k)
{
k++;
for(l=0;l<temp/kl;l++)
{
for(n=0;n<kl;n++)
{
if(j==n)
{
cipher[m]=matrix[l][n];
m++;
}
}
}
}
}
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t ***CIPHER TEXT IS***"<<endl<<endl<<"\t\t";
for(i=0;i<temp;i++)
{
cout<<cipher[i];
}
cout<<endl<<endl<<"\t\t ***END***"<<endl<<endl;
}
};
int main()
{
RailFence R1;
R1.GetPlain();
R1.GetKey();
R1.PlainManipulation();
R1.MakeMatrix();
R1.Encryption();
R1.DisplayCipher();
return 0;
}