hillchiper_3*3.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. NAME : RUSHIKESH FANSE
  3. ROLL : 16
  4. DIV : H
  5. ASSIGNMENT NO 3 : HILL CHIPHER FOR 3*3 MATRIX
  6. */
  7. #include<cstdio>
  8. #include<iostream>
  9. #include<vector>
  10. //calculate the minor elements of matrix in advance
  11. #define simplify1 ((enc_mat[1][1]*enc_mat[2][2])-(enc_mat[1][2]*enc_mat[2][1]))
  12. #define simplify2 ((enc_mat[1][0]*enc_mat[2][2])-(enc_mat[1][2]*enc_mat[2][0]))
  13. #define simplify3 ((enc_mat[1][0]*enc_mat[2][1])-(enc_mat[1][1]*enc_mat[2][0]))
  14. #define simplify4 ((enc_mat[0][1]*enc_mat[2][2])-(enc_mat[0][2]*enc_mat[2][1]))
  15. #define simplify5 ((enc_mat[0][0]*enc_mat[2][2])-(enc_mat[0][2]*enc_mat[2][0]))
  16. #define simplify6 ((enc_mat[0][0]*enc_mat[2][1])-(enc_mat[0][1]*enc_mat[2][0]))
  17. #define simplify7 ((enc_mat[0][1]*enc_mat[1][2])-(enc_mat[0][2]*enc_mat[1][1]))
  18. #define simplify8 ((enc_mat[0][0]*enc_mat[1][2])-(enc_mat[0][2]*enc_mat[1][0]))
  19. #define simplify9 ((enc_mat[0][0]*enc_mat[1][1])-(enc_mat[0][1]*enc_mat[1][0]))
  20. #define ps push_back
  21. //we know number of alphabet is 26
  22. #define NUMBER_OF_ALPHABET 26
  23. using namespace std;
  24. class HillChipher{
  25. //first matrix is the input matrix
  26. //second matrix is encryption matrix
  27. //third matrix is for the result of encryption
  28. int in_mat[3],enc_mat[3][3],res[3];
  29. //decryption matrix
  30. //and output matrix
  31. int dec_mat[3][3],out_mat[3];
  32. //cofactor matrix
  33. int cofactor_mat[3][3];
  34. //transpose matrix
  35. int transpose_mat[3][3];
  36. //det value is stored in this variable;
  37. int det;
  38. //this are the functions used to find the inverse matrix for decryption
  39. void find_determinant(){
  40. det=enc_mat[0][0]*simplify1-enc_mat[0][1]*simplify2+enc_mat[0][2]*simplify3;
  41. }
  42. void find_cofactor(){
  43. cofactor_mat[0][0]=simplify1;
  44. cofactor_mat[0][1]=0-simplify2;
  45. cofactor_mat[0][2]=simplify3;
  46. cofactor_mat[1][0]=0-simplify4;
  47. cofactor_mat[1][1]=simplify5;
  48. cofactor_mat[1][2]=0-simplify6;
  49. cofactor_mat[2][0]=simplify7;
  50. cofactor_mat[2][1]=0-simplify8;
  51. cofactor_mat[2][2]=simplify9;
  52. }
  53. void find_traspose(){
  54. for(int i=0;i<3;i++){
  55. for(int j=0;j<3;j++){
  56. transpose_mat[j][i]=cofactor_mat[i][j];
  57. }
  58. }
  59. }
  60. bool necessary(){
  61. int flag=false;
  62. for(int i=0;i<3;i++){
  63. for(int j=0;j<3;j++){
  64. if((cofactor_mat[i][j]%det)!=0){
  65. return true;
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. void find_modular_multiplicative_inverse(){
  72. //we first check whether the operation is necessary
  73. //if yes we continue else we find the adjacent matrix
  74. vector<int> a,b,c,d;
  75. bool val=false;
  76. if(val=necessary()){
  77. //we know the range already so we peform the mod operation and we
  78. //check for what value of x does we get 1 for modulus that is our answer
  79. //it is the equation for modular inverse which is given as follows
  80. //ax=1(mod b) here a is the determinant value and m is the number of
  81. //alphabets
  82. int x;
  83. while(det<-NUMBER_OF_ALPHABET)
  84. det = det%NUMBER_OF_ALPHABET;
  85. if(det<0)
  86. det=det+NUMBER_OF_ALPHABET;
  87. for (x=1; x<NUMBER_OF_ALPHABET; x++)
  88. if ((det*x) % NUMBER_OF_ALPHABET == 1)
  89. break;
  90. det=x;
  91. find_adjacent_mat(val);
  92. }
  93. else
  94. find_adjacent_mat(val);
  95. }
  96. void find_adjacent_mat(bool op){
  97. for(int i=0;i<3;i++){
  98. for(int j=0;j<3;j++){
  99. if(op){
  100. dec_mat[i][j]=(transpose_mat[i][j]*det)%26;
  101. if(dec_mat[i][j]<0)
  102. dec_mat[i][j]=dec_mat[i][j]+26;
  103. }
  104. else{
  105. dec_mat[i][j]=(transpose_mat[i][j]/det)%26;
  106. if(dec_mat[i][j]<0)
  107. dec_mat[i][j]=dec_mat[i][j]+26;
  108. }
  109. }
  110. }
  111. }
  112. void find_inverse(){
  113. find_determinant();
  114. find_cofactor();
  115. find_traspose();
  116. find_modular_multiplicative_inverse();
  117. }
  118. int char_to_int(char ch){
  119. return (int)ch-97;
  120. }
  121. char int_to_char(int in){
  122. return (char)(in+97);
  123. }
  124. public:
  125. enum operation{encryption,decryption};
  126. void accept_input_matrix(){
  127. char ch;
  128. for(int i=0;i<3;i++){
  129. cin>>ch;
  130. in_mat[i]=char_to_int(ch);
  131. }
  132. }
  133. void accept_encryption_matrix(){
  134. char ch;
  135. for(int i=0;i<3;i++){
  136. for(int j=0;j<3;j++){
  137. cin>>enc_mat[i][j];
  138. enc_mat[i][j]%=26;
  139. }
  140. }
  141. }
  142. void encryption_operation(){
  143. for(int i=0;i<3;i++){
  144. res[i]=(in_mat[0]*enc_mat[0][i]+in_mat[1]*enc_mat[1][i]+in_mat[2]*enc_mat[2][i])%26;
  145. }
  146. }
  147. void decryption_operation(){
  148. //find the inverse of the given encryption matrix so as to decode the
  149. //encrypted matrix
  150. find_inverse();
  151. for(int i=0;i<3;i++){
  152. out_mat[i]=(res[0]*dec_mat[0][i]+res[1]*dec_mat[1][i]+res[2]*dec_mat[2][i])%26;
  153. }
  154. }
  155. void display_matrix(int operate){
  156. switch(operate){
  157. //print the matrix for encryption
  158. case encryption:
  159. for(int i=0;i<3;i++){
  160. cout<<int_to_char(res[i])<<" ";
  161. }
  162. cout<<endl;
  163. break;
  164. //print the matrix for decryption
  165. case decryption:
  166. for(int i=0;i<3;i++){
  167. cout<<int_to_char(out_mat[i])<<" ";
  168. }
  169. cout<<endl;
  170. break;
  171. }
  172. }
  173. };
  174. void start(){
  175. //create the object of hill chipher
  176. HillChipher hc;
  177. //accept the input matrix
  178. cout<<"Enter the input matrix"<<endl;
  179. hc.accept_input_matrix();
  180. //accept the key matrix
  181. cout<<"Enter the encryption matrix"<<endl;
  182. hc.accept_encryption_matrix();
  183. //do the encryption operation
  184. hc.encryption_operation();
  185. //display the encrypted matrix
  186. cout<<"Encrypted matrix is "<<endl;
  187. hc.display_matrix(HillChipher::encryption);
  188. //do the decryption operation
  189. hc.decryption_operation();
  190. //display the decrypted matrix
  191. cout<<"Decrypted matrix is "<<endl;
  192. hc.display_matrix(HillChipher::decryption);
  193. }
  194. int main(){
  195. //the code works only for small characters
  196. //start program
  197. start();
  198. return 0;
  199. }
  200. /*
  201. OUTPUT:
  202. rishi@rishi-PC:~$ ./a.out
  203. Enter the input matrix
  204. ned
  205. Enter the encryption matrix
  206. 9 4 8
  207. 5 7 5
  208. 6 8 1
  209. Encrypted matrix is
  210. z a x
  211. Decrypted matrix is
  212. n e d
  213. */