Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <math.h>
- using namespace std;
- int main()
- {
- char plaintext[3];
- int key[3][3];
- cout<<"Enter the elements for the key(integers only):"<<endl;
- for(int i=0;i<3;i++)
- for(int j=0;j<3;j++)
- cin>>key[i][j];
- cout<<"The entered key is :"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<key[i][j]<<" ";
- }
- cout<<"\n";
- cout<<"Enter a three letter word:"<<endl;
- cin>>plaintext;
- int message[3][1];
- for(int k=0;k<3;k++)
- for(int j=0;j<1;j++)
- message[k][j]=((int)plaintext[k]-97);
- cout<<"The message matrix is:"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<1;j++)
- cout<<message[i][j];
- }
- cout<<"\n";
- cout<<"The multiplied matrix is:"<<endl;
- int k,i,j;
- int c[3][1];
- for(i=0;i<3;++i)
- {
- for(j=0;j<1;++j)
- {
- c[i][j]=0;
- for(k=0;k<3;++k)
- c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
- cout<<c[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The encrypted data is:"<<endl;
- for(int i=0;i<1;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<(char)((fmod(c[i][j],26)+97));
- }
- cout<<"\n";
- float det=0;
- float ikey[3][3];
- for(i = 0; i < 3; i++)
- det=det + (key[0][i] * (key[1][(i+1)%3] * key[2][(i+2)%3] - key[1][(i+2)%3] * key[2][(i+1)%3]));
- cout<<"Inverse of matrix is:"<<endl;
- for(i = 0; i < 3; i++){
- for(j = 0; j < 3; j++)
- {
- cout<<((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det<<"\t";
- ikey[i][j]=((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det;
- }
- cout<<"\n";
- }
- float decrypt[3][1];
- for(i=0;i<3;i++)
- {
- for(j=0;j<1;j++)
- {
- decrypt[i][j]=0;
- for(k=0;k<3;k++)
- decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
- cout<<decrypt[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The decrypted data is:"<<endl;
- for(i=0;i<3;i++)
- cout<<(char)(fmod(decrypt[i][0],26)+97);
- }
Add Comment
Please, Sign In to add comment