123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include <iostream>
- #include<cstring>
- #include <map>
- using namespace std;
- int main() {
-
- string capital_alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ", small_alphabets="abcdefghijklmnopqrstuvwxyz";
-
- string plain_text,cipher_text="",decrypted_plain_text="";
- int x,y,i,j,mul_factor,index,count;
- map<int,int> flag;
-
- cout<<"Enter the plain text: "<<endl;
- cin>>plain_text;
-
- cout<<"Enter the size of matrix"<<endl;
- cin>>x>>y;
-
- int key_matrix[x][y],plain_text_matrix[plain_text.length()],cipher_text_matrix[plain_text.length()],temporary_matrix[plain_text.length()][plain_text.length()],key_inverse_matrix[plain_text.length()][plain_text.length()],final_matrix[plain_text.length()];
-
- cout<<"Enter the elements of matrix"<<endl;
-
- for(i=0;i<x;i++)
- for(j=0;j<y;j++)
- cin>>key_matrix[i][j];
-
- for(i=0;i<plain_text.length();i++)
- {
- cipher_text_matrix[i]=0;
- final_matrix[i]=0;
- if(plain_text[i]>='A'&&plain_text[i]<='Z')
- {
- plain_text_matrix[i]=plain_text[i]-'A';
- flag[i]=1;
- }
-
- if(plain_text[i]>='a'&&plain_text[i]<='z')
- plain_text_matrix[i]=plain_text[i]-'a';
-
-
- //cout<<<<plain_text_matrix[i]<<endl;
-
- }
-
- for(i=0;i<plain_text.length();i++)
- for(j=0;j<plain_text.length();j+=1)
- cipher_text_matrix[i]+=plain_text_matrix[j]*key_matrix[j][i];
-
- for(i=0;i<plain_text.length();i++)
- {
- //while(( cipher_text_matrix[i]%26)<=26)
- cipher_text_matrix[i]=cipher_text_matrix[i]%26;
-
- if(flag[i]==1)
- cipher_text+=capital_alphabets[cipher_text_matrix[i]];
-
- else
- cipher_text+=small_alphabets[cipher_text_matrix[i]];
-
- }
-
- cout<<"Cipher text is "<<cipher_text<<endl;
-
- temporary_matrix[0][0]=key_matrix[1][1];
- temporary_matrix[1][1]=key_matrix[0][0];
- temporary_matrix[0][1]=-1*key_matrix[0][1];
- temporary_matrix[1][0]=-1*key_matrix[1][0];
-
- mul_factor=(key_matrix[0][0]*key_matrix[1][1])-(key_matrix[0][1]*key_matrix[1][0]);
-
- int a=mul_factor%26,x1,determinant;
-
-
-
- //while(a>26)
- a=a%26;
-
- if(a<0)
- a=a+26;
-
- for(x1=1;x1<=26;x1++)
- if(((a*x1)%26)==1)
- break;
-
- determinant = x1;
-
-
- for(i=0;i<plain_text.length();i++)
- for(j=0;j<plain_text.length();j++)
- key_inverse_matrix[i][j]=temporary_matrix[i][j]*determinant;
-
-
-
- for(i=0;i<plain_text.length();i++)
- for(j=0;j<plain_text.length();j++)
- final_matrix[i]+=cipher_text_matrix[j]*key_inverse_matrix[j][i];
-
-
- for(i=0;i<plain_text.length();i++)
- {
- //while((final_matrix[i]%26)<=26)
- final_matrix[i]=final_matrix[i]%26;
-
- if(flag[i]==1)
- decrypted_plain_text+=capital_alphabets[final_matrix[i]];
-
- else
- decrypted_plain_text+=small_alphabets[final_matrix[i]];
- }
-
- cout<<"Decrypted Text is "<<decrypted_plain_text<<endl;
-
-
- }
- /*
- OUTPUT:-
-
- Enter the plain text:
- ME
- Enter the size of matrix
- 2 2
- Enter the elements of matrix
- 9 4 5 7
- Cipher text is YY
- Decrypted Text is ME
-
- */
|