實現希爾密碼的 C++ 程式


基於線性代數,希爾密碼是一種密碼學中的多音替換密碼。

加密資訊:金鑰字串和資訊字串以矩陣形式表示。然後對它們進行乘法,然後對 26 求模。用於加密資訊的關鍵矩陣應該有逆矩陣。

解密資訊:用用於加密的逆金鑰矩陣乘以加密資訊,並對 26 求模,即可獲得解密資訊。

例如

金鑰矩陣

1 0 1
2 4 0
3 5 6

矩陣形式的資訊字串“ABC”−

0
1
2

加密

乘以上述兩個矩陣後,我們得到,

2
4
17

這將是加密資訊“CER”

解密

金鑰矩陣的逆矩陣為−

1.09091 0.227273 -0.181818
-0.545455 0.136364 0.0909091
-0.0909091 -0.227273 0.181818

現在將金鑰矩陣的逆矩陣與加密資訊矩陣相乘為−

0
1
2

這便是原始資訊字串“ABC”。

下面是一個實現上述示例的 C++ 程式。

演算法

Begin
Function getKeyMatrix()
   For i = 0 to 2
      For j = 0 to 3
      Take the elements of matrix a[i][j] as input.
         m[i][j] = a[i][j]
      done
   done
   Take the message string as user input.
   For i = 0 to 2
      msg[i][0] = mes[i] - 65
   done
End
Begin
Function encrypt()
   For i = 0 to 2
      For j = 0 to 0
         For k = 0 to 2
            en[i][j] = en[i][j] + a[i][k] * msg[k][j]
   Take modulo 26 for each element of the matrix obtained by multiplication and print the encrypted message.
End
Begin
Function decrypt()
   Call function inversematrix()
   For i = 0 to 2
      For j = 0 to 0
         For k = 0 to 2
            de[i][j] = de[i][j] + b[i][k] * en[k][j]
Take modulo 26 of the multiplication to get the original message.

示例

#include<iostream>
#include<math.h>
using namespace std;
float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];
void getKeyMatrix() { //get key and message from user
   int i, j;
   char mes[3];
   cout<<"Enter 3x3 matrix for key (should have inverse):\n";
   for(i = 0; i < 3; i++)
   for(j = 0; j < 3; j++) {
      cin>>a[i][j];
      m[i][j] = a[i][j];
   }
   cout<<"\nEnter a string of 3 letter(use A through Z): ";
   cin>>mes;
   for(i = 0; i < 3; i++)
   msg[i][0] = mes[i] - 65;
}
void encrypt() { //encrypts the message
   int i, j, k;
   for(i = 0; i < 3; i++)
   for(j = 0; j < 1; j++)
   for(k = 0; k < 3; k++)
   en[i][j] = en[i][j] + a[i][k] * msg[k][j];
   cout<<"\nEncrypted string is: ";
   for(i = 0; i < 3; i++)
   cout<<(char)(fmod(en[i][0], 26) + 65); //modulo 26 is taken for each element of the matrix obtained by multiplication
}
void inversematrix() { //find inverse of key matrix
   int i, j, k;
   float p, q;
   for(i = 0; i < 3; i++)
   for(j = 0; j < 3; j++) {
      if(i == j)
         b[i][j]=1;
      else
         b[i][j]=0;
   }
   for(k = 0; k < 3; k++) {
      for(i = 0; i < 3; i++) {
         p = m[i][k];
         q = m[k][k];
         for(j = 0; j < 3; j++) {
            if(i != k) {
               m[i][j] = m[i][j]*q - p*m[k][j];
               b[i][j] = b[i][j]*q - p*b[k][j];
            }
         }
      }
   }
   for(i = 0; i < 3; i++)
   for(j = 0; j < 3; j++)
   b[i][j] = b[i][j] / m[i][i];
   cout<<"\n\nInverse Matrix is:\n";
   for(i = 0; i < 3; i++) {
      for(j = 0; j < 3; j++)
      cout<<b[i][j]<<" ";
      cout<<"\n";
   }
}
void decrypt() { //decrypt the message
   int i, j, k;
   inversematrix();
   for(i = 0; i < 3; i++)
   for(j = 0; j < 1; j++)
   for(k = 0; k < 3; k++)
   de[i][j] = de[i][j] + b[i][k] * en[k][j];
   cout<<"\nDecrypted string is: ";
   for(i = 0; i < 3; i++)
   cout<<(char)(fmod(de[i][0], 26) + 65); //modulo 26 is taken to get the original message
   cout<<"\n";
}
int main() {
   getKeyMatrix();
   encrypt();
   decrypt();
}

輸出

Enter 3x3 matrix for key (should have inverse):
1
0
1
2
4
0
3
5
6

Enter a string of 3 letter(use A through Z): ABC

Encrypted string is: CER

Inverse Matrix is:
1.09091 0.227273 -0.181818
-0.545455 0.136364 0.0909091
-0.0909091 -0.227273 0.181818

Decrypted string is: ABC

更新於: 30-Jul-2019

7K+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.