#include<bits/stdc++.h> using namespace std; #define mod 26 char A[27];//密文串 int K[100][100];//加密矩阵 int n; int main() { cin>>n; for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin>>K[i][j]; cin>>A;//在下面Begin和End之间补全代码,对输入的字符串进行希尔加密; /*********** Begin ***********/ /*********** End ***********/ } 补充
时间: 2025-05-20 22:22:13 浏览: 14
### 希尔加密算法的 C++ 实现
希尔加密算法是一种基于矩阵运算的经典密码学方法。以下是完整的实现过程,包括密钥生成、明文编码以及加解密逻辑。
#### 密钥生成
为了确保加密的安全性和有效性,需要定义一个可逆方阵作为密钥矩阵 $ K $ 。该矩阵必须满足其行列式的模数不等于零,并且在指定域上存在乘法逆元。
```cpp
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// 计算二维数组的行列式
int calculateDeterminant(vector<vector<int>> matrix, int size) {
if (size == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
int determinant = 0;
for (int i = 0; i < size; ++i) {
vector<vector<int>> submatrix(size - 1, vector<int>(size - 1));
for (int row = 1; row < size; ++row) {
for (int col = 0; col < size; ++col) {
if (col < i) {
submatrix[row - 1][col] = matrix[row][col];
} else if (col > i) {
submatrix[row - 1][col - 1] = matrix[row][col];
}
}
}
determinant += ((i % 2 == 0) ? 1 : -1) * matrix[0][i] * calculateDeterminant(submatrix, size - 1);
}
return determinant;
}
// 获取伴随矩阵并计算逆矩阵
vector<vector<int>> getInverseMatrix(const vector<vector<int>>& keyMatrix, const int mod) {
int n = keyMatrix.size();
vector<vector<int>> adjugate(n, vector<int>(n, 0));
// 构建伴随矩阵
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
vector<vector<int>> minor(n - 1, vector<int>(n - 1, 0));
for (int row = 0; row < n; ++row) {
for (int col = 0; col < n; ++col) {
if (row != i && col != j) {
minor[row < i ? row : row - 1][col < j ? col : col - 1] = keyMatrix[row][col];
}
}
}
adjugate[j][i] = pow(-1, i + j) * calculateDeterminant(minor, n - 1);
}
}
// 找到行列式的模逆元
int detModInv = 0;
int determinant = calculateDeterminant(keyMatrix, n);
while (true) {
if ((determinant * detModInv) % mod == 1) break;
detModInv++;
}
// 返回逆矩阵
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
adjugate[i][j] = (adjugate[i][j] * detModInv) % mod;
}
}
return adjugate;
}
```
#### 加密部分
通过将明文字母转换为数值向量并与密钥矩阵相乘完成加密操作。
```cpp
void encryptHillCipher(const string& plaintext, const vector<vector<int>>& keyMatrix, const int mod, string& ciphertext) {
int blockSize = keyMatrix.size();
vector<int> plainVector(blockSize);
for (size_t i = 0; i < plaintext.length(); i += blockSize) {
// 将字符映射至整数(A=0, B=1...)
for (int j = 0; j < blockSize && (i + j) < plaintext.length(); ++j) {
plainVector[j] = toupper(plaintext[i + j]) - 'A';
}
// 补充填充字符以匹配块大小
if (plaintext.length() - i < blockSize) {
for (int k = plaintext.length() - i; k < blockSize; ++k) {
plainVector[k] = ('X' - 'A');
}
}
// 进行矩阵乘法
vector<int> cipherBlock(blockSize, 0);
for (int r = 0; r < blockSize; ++r) {
for (int c = 0; c < blockSize; ++c) {
cipherBlock[r] = (cipherBlock[r] + keyMatrix[r][c] * plainVector[c]) % mod;
}
}
// 转换回字母形式
for (int l = 0; l < blockSize; ++l) {
ciphertext += char(cipherBlock[l] + 'A');
}
}
}
```
#### 解密部分
利用逆矩阵恢复原始消息。
```cpp
void decryptHillCipher(const string& ciphertext, const vector<vector<int>>& inverseKeyMatrix, const int mod, string& decryptedText) {
int blockSize = inverseKeyMatrix.size();
for (size_t i = 0; i < ciphertext.length(); i += blockSize) {
vector<int> cipherVector(blockSize);
// 映射密文字符至整数
for (int j = 0; j < blockSize && (i + j) < ciphertext.length(); ++j) {
cipherVector[j] = toupper(ciphertext[i + j]) - 'A';
}
// 使用逆矩阵进行解码
vector<int> plainBlock(blockSize, 0);
for (int r = 0; r < blockSize; ++r) {
for (int c = 0; c < blockSize; ++c) {
plainBlock[r] = (plainBlock[r] + inverseKeyMatrix[r][c] * cipherVector[c]) % mod;
}
}
// 转换回原文本
for (int l = 0; l < blockSize; ++l) {
decryptedText += char((plainBlock[l] + mod) % mod + 'A'); // 防止负值
}
}
}
```
以上代码实现了希尔加密的核心功能[^1]。调用 `encryptHillCipher` 和 `decryptHillCipher` 函数即可分别执行加密和解密操作。
---
阅读全文
相关推荐


















