Advertisement
sheyshya1

hill cipher

Mar 28th, 2022
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5.  
  6. int modInverse(int a, int m){
  7.     a=a%m;
  8.     for(int x=-m;x<m;x++)
  9.        if((a*x)%m==1)
  10.           return x;
  11. }
  12. void getCofactor(vector<vector<int> > &a, vector<vector<int> > &temp, int p, int q, int n){
  13.     int i=0,j=0;
  14.     for(int row=0;row<n;row++){
  15.         for(int col=0;col<n;col++){
  16.             if(row!=p&&col!=q){
  17.                 temp[i][j++] = a[row][col];
  18.                 if (j==n-1){
  19.                     j=0;
  20.                     i++;
  21.                 }
  22.             }
  23.         }
  24.     }
  25. }
  26. int determinant(vector<vector<int> > &a, int n, int N){
  27.     int D = 0;
  28.     if(n==1)
  29.         return a[0][0];
  30.     vector<vector<int> > temp(N, vector<int>(N));
  31.     int sign = 1;
  32.     for(int f=0;f<n;f++){
  33.         getCofactor(a, temp, 0, f, n);
  34.         D += sign * a[0][f] * determinant(temp, n - 1, N);
  35.         sign = -sign;
  36.     }
  37.     return D;
  38. }
  39. void adjoint(vector<vector<int> > &a,vector<vector<int> > &adj,int N){
  40.     if(N == 1){
  41.         adj[0][0] = 1;
  42.         return;
  43.     }
  44.     int sign = 1;
  45.     vector<vector<int> > temp(N, vector<int>(N));
  46.     for(int i=0;i<N;i++){
  47.         for(int j=0;j<N;j++){
  48.             getCofactor(a, temp, i, j, N);
  49.             sign = ((i+j)%2==0)? 1: -1;
  50.             adj[j][i] = (sign)*(determinant(temp, N-1 , N));
  51.         }
  52.     }
  53. }
  54. bool inverse(vector<vector<int> > &a, vector<vector<int> > &inv, int N){
  55.     int det = determinant(a, N, N);
  56.     if(det == 0){
  57.         cout << "Inverse does not exist";
  58.         return false;
  59.     }
  60.     int invDet = modInverse(det,26);
  61.     cout<<det%26<<' '<<invDet<<'\n';
  62.     vector<vector<int> > adj(N, vector<int>(N));
  63.     adjoint(a, adj, N);
  64.     for(int i=0;i<N;i++)
  65.         for(int j=0;j<N;j++)
  66.             inv[i][j] = (adj[i][j]*invDet)%26;
  67.     return true;
  68. }
  69. int decryption(){
  70.     int x,y,i,j,k,n;
  71.     cout<<"Enter the size of key matrix\n";
  72.     cin>>n;
  73.     cout<<"Enter the key matrix\n";
  74.     vector<vector<int> > a(n, vector<int>(n));
  75.     vector<vector<int> > adj(n, vector<int>(n));
  76.     vector<vector<int> > inv(n, vector<int>(n));
  77.     for(i=0;i<n;i++){
  78.         for(j=0;j<n;j++){
  79.             cin>>a[i][j];
  80.         }
  81.     }
  82.     if(inverse(a,inv,n)){
  83.         cout<<"Inverse exist\n";
  84.     }
  85.     cout<<"Enter the message to decrypt\n";
  86.     string s;
  87.     cin>>s;
  88.     k=0;
  89.     string ans;
  90.     while(k<s.size()){
  91.         for(i=0;i<n;i++){
  92.             int sum = 0;
  93.             int temp = k;
  94.             for(j=0;j<n;j++){
  95.                 sum += ((inv[i][j] + 26)%26*(s[temp++]-'a')%26)%26;
  96.                 sum = sum%26;
  97.             }
  98.             ans+=(sum+'a');
  99.         }
  100.         k+=n;
  101.     }
  102.     //ans+='\0';
  103.     int f=ans.size()-1;
  104.     while(ans[f]=='x'){
  105.         f--;
  106.     }
  107.     for(i=0;i<=f;i++){
  108.         cout<<ans[i];
  109.     }
  110.     cout<<'\n';
  111.     return 0;
  112. }
  113. int main(){
  114.     int x,y,i,j,k,n;
  115.     cout<<"Enter the size of key matrix\n";
  116.     cin>>n;
  117.     cout<<"Enter the key matrix\n";
  118.     int a[n][n];
  119.     for(i=0;i<n;i++){
  120.         for(j=0;j<n;j++){
  121.             cin>>a[i][j];
  122.         }
  123.     }
  124.     cout<<"Enter the message to encrypt\n";
  125.     string s;
  126.     cin>>s;
  127.     int temp = (n-s.size()%n)%n;
  128.     for(i=0;i<temp;i++){
  129.         s+='x';
  130.     }
  131.     k=0;
  132.     string ans="";
  133.     while(k<s.size()){
  134.         for(i=0;i<n;i++){
  135.             int sum = 0;
  136.             int temp = k;
  137.             for(j=0;j<n;j++){
  138.                 sum += (a[i][j]%26*(s[temp++]-'a')%26)%26;
  139.                 sum = sum%26;
  140.             }
  141.             ans+=(sum+'a');
  142.         }
  143.         k+=n;
  144.     }
  145.     cout<<ans<<'\n';
  146.     cout<<"\n";
  147.     cout<<"Decryption";
  148.     decryption();
  149.  
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement