Hill Cipher C++ Code
#include<iostream>
#include<string.h>
using namespace std;
class HillCipher
{
char plain[99],temp[4],cipher1[4],cipher[99],cipher2[99];
int key[3][3],i,j,k,pL,PT[3],n,count,temp2,j1,k1,c[3],z,tt;
public:
HillCipher(){count=0; }
void GetPlain()
{
cout<<"\t\t\t *** ENTER PLAIN TEXT ***"<<endl<<endl;
gets(plain);
}
void PlainModification()
{
pL=strlen(plain);
if(pL%3==1)
{
for(i=pL;i<pL+2;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
if(pL%3==2)
{
for(i=pL;i<pL+1;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
}
void GetKey()
{
cout<<endl<<"\t\t\t *** ENTER KEY ***"<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>key[i][j];
}
}
}
void DisplayKey()
{
cout<<"\t\t\t *** KEY IS *** "<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t\t"<<key[i][j];
}
cout<<endl;
}
}
void PlainSend()
{
pL=strlen(plain);
for(i=0;i<pL;i=i+3)
{
for(k=0,j=i;k<3,j<i+3;k++,j++)
{
temp[k]=plain[j];
}
temp[3]='\0';
FindNumber();
}
}
void FindNumber()
{
for(n=0;n<3;n++)
{
PT[n]=temp[n]-97;
}
MatricMul();
}
void MatricMul()
{
for(j1=0;j1<3;j1++)
{
temp2=0;
for(k1=0;k1<3;k1++)
{
temp2=temp2+key[k1][j1]*PT[k1];
}
c[j1]=temp2;
}
Encryption();
}
void Encryption()
{
for(n=0;n<3;n++)
{
c[n]=c[n]%26;
}
for(n=0;n<3;n++)
{
cipher1[n]=c[n]+97;
}
cipher1[3]='\0';
UpdateCipher();
}
void UpdateCipher()
{
count++;
tt=(count-1)*3;
for(z=0,n=tt;z<3,n<pL;z++,n++)
{
cipher[n]=cipher1[z];
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t\t *** CIPHER TEXT IS ***"<<endl<<endl<<"\t\t\t ";
for(z=0;z<count*3;z++)
{
cout<<cipher[z];
}
cout<<endl<<endl<<"\t\t\t *** END OF PROGRAM *** "<<endl;
}
};
int main()
{
HillCipher h1;
h1.GetPlain();
h1.PlainModification();
h1.GetKey();
h1.DisplayKey();
h1.PlainSend();
h1.DisplayCipher();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
class HillCipher
{
char plain[99],temp[4],cipher1[4],cipher[99],cipher2[99];
int key[3][3],i,j,k,pL,PT[3],n,count,temp2,j1,k1,c[3],z,tt;
public:
HillCipher(){count=0; }
void GetPlain()
{
cout<<"\t\t\t *** ENTER PLAIN TEXT ***"<<endl<<endl;
gets(plain);
}
void PlainModification()
{
pL=strlen(plain);
if(pL%3==1)
{
for(i=pL;i<pL+2;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
if(pL%3==2)
{
for(i=pL;i<pL+1;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
}
void GetKey()
{
cout<<endl<<"\t\t\t *** ENTER KEY ***"<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>key[i][j];
}
}
}
void DisplayKey()
{
cout<<"\t\t\t *** KEY IS *** "<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t\t"<<key[i][j];
}
cout<<endl;
}
}
void PlainSend()
{
pL=strlen(plain);
for(i=0;i<pL;i=i+3)
{
for(k=0,j=i;k<3,j<i+3;k++,j++)
{
temp[k]=plain[j];
}
temp[3]='\0';
FindNumber();
}
}
void FindNumber()
{
for(n=0;n<3;n++)
{
PT[n]=temp[n]-97;
}
MatricMul();
}
void MatricMul()
{
for(j1=0;j1<3;j1++)
{
temp2=0;
for(k1=0;k1<3;k1++)
{
temp2=temp2+key[k1][j1]*PT[k1];
}
c[j1]=temp2;
}
Encryption();
}
void Encryption()
{
for(n=0;n<3;n++)
{
c[n]=c[n]%26;
}
for(n=0;n<3;n++)
{
cipher1[n]=c[n]+97;
}
cipher1[3]='\0';
UpdateCipher();
}
void UpdateCipher()
{
count++;
tt=(count-1)*3;
for(z=0,n=tt;z<3,n<pL;z++,n++)
{
cipher[n]=cipher1[z];
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t\t *** CIPHER TEXT IS ***"<<endl<<endl<<"\t\t\t ";
for(z=0;z<count*3;z++)
{
cout<<cipher[z];
}
cout<<endl<<endl<<"\t\t\t *** END OF PROGRAM *** "<<endl;
}
};
int main()
{
HillCipher h1;
h1.GetPlain();
h1.PlainModification();
h1.GetKey();
h1.DisplayKey();
h1.PlainSend();
h1.DisplayCipher();
return 0;
}