You are on page 1of 2

#include<iostream>

#include<stdio.h>

using namespace std;

class hill
{
public:
string msg,k;
int *enc;
int key[3][3],inv[3][3];
hill(string a, string b)
{
msg = a;
k = b;
int i,j,l=0;
cout<<"\nKey Matrix : "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(l>=k.size())
{
key[i][j] = 'x' - 'a' + 1;
}
else
{
key[i][j] = k[l]-'a' + 1;
}
cout<<key[i][j]<<" ";
l++;
}
cout<<endl;
}
cout<<endl;
while(msg.size()%3!=0)
{
msg = msg + 'x';
}
cout<<"Extended Message : ";
cout<<msg<<endl;

enc = new int[msg.size()];

}
void encrypt()
{
int i,j,l,m;
for(l=0;l<msg.size();)
{
m = l;
for(i=0;i<3;i++)
{
enc[l] = (key[i][0]*(msg[m]-'a' + 1) + key[i]
[1]*(msg[m+1]-'a' + 1) +key[i][2]*(msg[m+2]-'a' + 1))%26 + 'a'-1;
cout<<(char)enc[l];
l++;
}
}
}
};

int main()
{
string s,k;
cout<<"Enter the Plain Text Message"<<endl;
fflush(stdin);
getline(cin,s);
cout<<"Enter the Key"<<endl;
fflush(stdin);
getline(cin,k);

hill h(s,k);
cout<<endl;
cout<<"Encrypted message is : ";
h.encrypt();
cout<<endl;
return 0;
}

You might also like