ash_wath

Hill Cipher

Nov 7th, 2017
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5. int main()
  6. {
  7. char plaintext[3];
  8. int key[3][3];
  9. cout<<"Enter the elements for the key(integers only):"<<endl;
  10. for(int i=0;i<3;i++)
  11. for(int j=0;j<3;j++)
  12. cin>>key[i][j];
  13. cout<<"The entered key is :"<<endl;
  14. for(int i=0;i<3;i++)
  15. {
  16. cout<<"\n";
  17. for(int j=0;j<3;j++)
  18. cout<<key[i][j]<<" ";
  19. }
  20. cout<<"\n";
  21. cout<<"Enter a three letter word:"<<endl;
  22. cin>>plaintext;
  23. int message[3][1];
  24. for(int k=0;k<3;k++)
  25. for(int j=0;j<1;j++)
  26. message[k][j]=((int)plaintext[k]-97);
  27. cout<<"The message matrix is:"<<endl;
  28. for(int i=0;i<3;i++)
  29. {
  30. cout<<"\n";
  31. for(int j=0;j<1;j++)
  32. cout<<message[i][j];
  33. }
  34. cout<<"\n";
  35. cout<<"The multiplied matrix is:"<<endl;
  36. int k,i,j;
  37. int c[3][1];
  38. for(i=0;i<3;++i)
  39. {
  40. for(j=0;j<1;++j)
  41. {
  42. c[i][j]=0;
  43. for(k=0;k<3;++k)
  44. c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
  45. cout<<c[i][j]<<" ";
  46. }
  47. cout<<"\n";
  48. }
  49. cout<<"The encrypted data is:"<<endl;
  50. for(int i=0;i<1;i++)
  51. {
  52. cout<<"\n";
  53. for(int j=0;j<3;j++)
  54. cout<<(char)((fmod(c[i][j],26)+97));
  55. }
  56. cout<<"\n";
  57. float det=0;
  58. float ikey[3][3];
  59. for(i = 0; i < 3; i++)
  60. 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]));
  61. cout<<"Inverse of matrix is:"<<endl;
  62. for(i = 0; i < 3; i++){
  63. for(j = 0; j < 3; j++)
  64. {
  65. 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";
  66. 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;
  67. }
  68. cout<<"\n";
  69. }
  70. float decrypt[3][1];
  71. for(i=0;i<3;i++)
  72. {
  73. for(j=0;j<1;j++)
  74. {
  75. decrypt[i][j]=0;
  76. for(k=0;k<3;k++)
  77. decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
  78. cout<<decrypt[i][j]<<" ";
  79. }
  80. cout<<"\n";
  81. }
  82. cout<<"The decrypted data is:"<<endl;
  83. for(i=0;i<3;i++)
  84. cout<<(char)(fmod(decrypt[i][0],26)+97);
  85. }
Add Comment
Please, Sign In to add comment