Advertisement
_Mizanur

Hill Cipher

Feb 4th, 2023
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string pt,encrypt;
  6.     cout<<"Enter plain text: ";
  7.     cin>>pt;
  8.     if(pt.size()%2!=0)
  9.         pt+='X';
  10.     int n;
  11.     cout<<"Enter the order of key: ";
  12.     cin>>n;
  13.     ///taking key matrix
  14.     int arr[n][n];
  15.     for(int i=0;i<n;i++)
  16.     {
  17.         for(int j=0;j<n;j++)
  18.         {
  19.             cin>>arr[i][j];
  20.         }
  21.     }
  22.     int ind=0;
  23.     for(int l=0;l<pt.size();l+=2){
  24.     int arr1[n][1];
  25.     for(int i=0;i<n;i++)
  26.     {
  27.         arr1[i][0]=pt[ind]-'A';
  28.         ind++;
  29.     }
  30.  
  31.     ///Matrix Multiplication
  32.     int ans[n][1];
  33.     for(int i=0;i<n;i++)
  34.     {
  35.         for(int j=0;j<1;j++)
  36.         {
  37.             ans[i][j]=0;
  38.         }
  39.     }
  40.     for(int i=0;i<n;i++)
  41.     {
  42.         for(int j=0;j<1;j++)
  43.         {
  44.             for(int k=0;k<n;k++){
  45.             ans[i][j]=(ans[i][j]+arr[i][k]*arr1[k][j])%26;
  46.             //cout<<arr[i][k]<<" "<<arr1[k][j]<<endl;
  47.             }
  48.         }
  49.     }
  50.     for(int i=0;i<n;i++)
  51.     {
  52.         for(int j=0;j<1;j++)
  53.         {
  54.           encrypt+='A'+ans[i][j];
  55.         }
  56.     }
  57.     }
  58.     cout<<"Encrypt Message: "<<encrypt<<endl;
  59.  
  60.     ///inverse matrix;
  61.     int mulinv=0;
  62.     int det=(arr[0][0]*arr[1][1])-(arr[0][1]*arr[1][0]);
  63.     for(int i=1;;i++)
  64.     {
  65.         if((det*i)%26==1){
  66.             mulinv=i;
  67.             cout<<"multiplication inverse: "<<mulinv<<endl;
  68.             break;
  69.         }
  70.     }
  71.  
  72.  
  73.  
  74.     int arr2[n][n];
  75.     int temp=arr[0][0];
  76.     arr[0][0]=arr[1][1];
  77.     arr[1][1]=temp;
  78.     arr[0][1]=(-1*arr[0][1]);
  79.     arr[1][0]=(-1*arr[1][0]);
  80.  
  81.  
  82.     cout<<"Adj: "<<endl;
  83.     for(int i=0;i<n;i++)
  84.     {
  85.         for(int j=0;j<n;j++)
  86.         {
  87.             cout<<arr[i][j]<<" ";
  88.         }
  89.         cout<<endl;
  90.     }
  91.     cout<<endl<<endl;
  92.     for(int i=0;i<n;i++)
  93.     {
  94.         for(int j=0;j<n;j++)
  95.         {
  96.             arr2[i][j]=((arr[i][j]*mulinv)%26);
  97.         }
  98.     }
  99.  
  100.     for(int i=0;i<n;i++)
  101.     {
  102.         for(int j=0;j<n;j++)
  103.         {
  104.             if(arr2[i][j]<0)
  105.                 arr2[i][j]+=26;
  106.         }
  107.     }
  108.     string decrypt;
  109.     int ind1=0;
  110.     for(int l=0;l<encrypt.size();l+=2){
  111.     int arr3[n][1];
  112.  
  113.     int arr1[n][1];
  114.     for(int i=0;i<n;i++)
  115.     {
  116.         arr3[i][0]=encrypt[ind1]-'A';
  117.         ind1++;
  118.     }
  119.  
  120.     int ans1[n][1];
  121.     for(int i=0;i<n;i++)
  122.     {
  123.         for(int j=0;j<1;j++)
  124.         {
  125.             ans1[i][j]=0;
  126.         }
  127.     }
  128.  
  129.     for(int i=0;i<n;i++)
  130.     {
  131.         for(int j=0;j<n;j++)
  132.         {
  133.             cout<<arr2[i][j]<<" ";
  134.         }
  135.         cout<<endl;
  136.     }
  137.     cout<<endl;
  138.     for(int i=0;i<n;i++)
  139.     {
  140.         for(int j=0;j<1;j++)
  141.         {
  142.             cout<<arr3[i][j];
  143.         }
  144.         cout<<endl;
  145.     }
  146.     cout<<endl;
  147.     for(int i=0;i<n;i++)
  148.     {
  149.         for(int j=0;j<1;j++)
  150.         {
  151.             for(int k=0;k<n;k++){
  152.             ans1[i][j]=(ans1[i][j]+arr2[i][k]*arr3[k][j])%26;
  153.             //cout<<ans1[i][j]<<" ";
  154.             //cout<<arr[i][k]<<" "<<arr1[k][j]<<endl;
  155.             }
  156.         }
  157.     }
  158.     for(int i=0;i<n;i++)
  159.     {
  160.         for(int j=0;j<1;j++)
  161.         {
  162.           decrypt+=ans1[i][j]+'A';
  163.         }
  164.     }
  165.     }
  166.  
  167.     cout<<"Decrypt Message: "<<decrypt<<endl;
  168.  
  169.  
  170.  
  171.  
  172.     return 0;
  173. }
  174.  
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement