hill_cipher_2*2.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <iostream>
  2. #include<cstring>
  3. #include <map>
  4. using namespace std;
  5. int main() {
  6. string capital_alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ", small_alphabets="abcdefghijklmnopqrstuvwxyz";
  7. string plain_text,cipher_text="",decrypted_plain_text="";
  8. int x,y,i,j,mul_factor,index,count;
  9. map<int,int> flag;
  10. cout<<"Enter the plain text: "<<endl;
  11. cin>>plain_text;
  12. cout<<"Enter the size of matrix"<<endl;
  13. cin>>x>>y;
  14. 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()];
  15. cout<<"Enter the elements of matrix"<<endl;
  16. for(i=0;i<x;i++)
  17. for(j=0;j<y;j++)
  18. cin>>key_matrix[i][j];
  19. for(i=0;i<plain_text.length();i++)
  20. {
  21. cipher_text_matrix[i]=0;
  22. final_matrix[i]=0;
  23. if(plain_text[i]>='A'&&plain_text[i]<='Z')
  24. {
  25. plain_text_matrix[i]=plain_text[i]-'A';
  26. flag[i]=1;
  27. }
  28. if(plain_text[i]>='a'&&plain_text[i]<='z')
  29. plain_text_matrix[i]=plain_text[i]-'a';
  30. //cout<<<<plain_text_matrix[i]<<endl;
  31. }
  32. for(i=0;i<plain_text.length();i++)
  33. for(j=0;j<plain_text.length();j+=1)
  34. cipher_text_matrix[i]+=plain_text_matrix[j]*key_matrix[j][i];
  35. for(i=0;i<plain_text.length();i++)
  36. {
  37. //while(( cipher_text_matrix[i]%26)<=26)
  38. cipher_text_matrix[i]=cipher_text_matrix[i]%26;
  39. if(flag[i]==1)
  40. cipher_text+=capital_alphabets[cipher_text_matrix[i]];
  41. else
  42. cipher_text+=small_alphabets[cipher_text_matrix[i]];
  43. }
  44. cout<<"Cipher text is "<<cipher_text<<endl;
  45. temporary_matrix[0][0]=key_matrix[1][1];
  46. temporary_matrix[1][1]=key_matrix[0][0];
  47. temporary_matrix[0][1]=-1*key_matrix[0][1];
  48. temporary_matrix[1][0]=-1*key_matrix[1][0];
  49. mul_factor=(key_matrix[0][0]*key_matrix[1][1])-(key_matrix[0][1]*key_matrix[1][0]);
  50. int a=mul_factor%26,x1,determinant;
  51. //while(a>26)
  52. a=a%26;
  53. if(a<0)
  54. a=a+26;
  55. for(x1=1;x1<=26;x1++)
  56. if(((a*x1)%26)==1)
  57. break;
  58. determinant = x1;
  59. for(i=0;i<plain_text.length();i++)
  60. for(j=0;j<plain_text.length();j++)
  61. key_inverse_matrix[i][j]=temporary_matrix[i][j]*determinant;
  62. for(i=0;i<plain_text.length();i++)
  63. for(j=0;j<plain_text.length();j++)
  64. final_matrix[i]+=cipher_text_matrix[j]*key_inverse_matrix[j][i];
  65. for(i=0;i<plain_text.length();i++)
  66. {
  67. //while((final_matrix[i]%26)<=26)
  68. final_matrix[i]=final_matrix[i]%26;
  69. if(flag[i]==1)
  70. decrypted_plain_text+=capital_alphabets[final_matrix[i]];
  71. else
  72. decrypted_plain_text+=small_alphabets[final_matrix[i]];
  73. }
  74. cout<<"Decrypted Text is "<<decrypted_plain_text<<endl;
  75. }
  76. /*
  77. OUTPUT:-
  78. Enter the plain text:
  79. ME
  80. Enter the size of matrix
  81. 2 2
  82. Enter the elements of matrix
  83. 9 4 5 7
  84. Cipher text is YY
  85. Decrypted Text is ME
  86. */