Hill密码算法java
时间: 2023-10-12 19:21:58 浏览: 39
以下是Hill密码算法的Java实现代码:
```java
import java.util.Arrays;
public class HillCipher {
private final int[][] keyMatrix;
public HillCipher(int[][] keyMatrix) {
this.keyMatrix = keyMatrix;
}
public String encrypt(String message) {
int[] messageVector = toVector(message);
int[] encryptedVector = multiply(keyMatrix, messageVector);
return toString(encryptedVector);
}
public String decrypt(String message) {
int[] messageVector = toVector(message);
int[][] inverseMatrix = getInverseMatrix(keyMatrix);
int[] decryptedVector = multiply(inverseMatrix, messageVector);
return toString(decryptedVector);
}
private int[] toVector(String message) {
int[] vector = new int[message.length()];
for (int i = 0; i < message.length(); i++) {
vector[i] = message.charAt(i) - 'a';
}
return vector;
}
private String toString(int[] vector) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < vector.length; i++) {
builder.append((char) (vector[i] + 'a'));
}
return builder.toString();
}
private int[] multiply(int[][] matrix, int[] vector) {
int[] result = new int[vector.length];
for (int i = 0; i < matrix.length; i++) {
int sum = 0;
for (int j = 0; j < vector.length; j++) {
sum += matrix[i][j] * vector[j];
}
result[i] = sum % 26;
}
return result;
}
private int[][] getInverseMatrix(int[][] matrix) {
int det = determinant(matrix);
int[][] adjugateMatrix = adjugate(matrix);
int[][] inverseMatrix = new int[matrix.length][matrix.length];
int detInverse = multiplicativeInverse(det);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
inverseMatrix[i][j] = ((adjugateMatrix[i][j] * detInverse) % 26 + 26) % 26;
}
}
return inverseMatrix;
}
private int determinant(int[][] matrix) {
if (matrix.length == 1) {
return matrix[0][0];
} else if (matrix.length == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
} else {
int det = 0;
for (int i = 0; i < matrix.length; i++) {
int[][] subMatrix = new int[matrix.length - 1][matrix.length - 1];
for (int j = 1; j < matrix.length; j++) {
for (int k = 0; k < matrix.length; k++) {
if (k < i) {
subMatrix[j - 1][k] = matrix[j][k];
} else if (k > i) {
subMatrix[j - 1][k - 1] = matrix[j][k];
}
}
}
det += matrix[0][i] * Math.pow(-1, i) * determinant(subMatrix);
}
return det;
}
}
private int[][] adjugate(int[][] matrix) {
int[][] adjugateMatrix = new int[matrix.length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
int[][] subMatrix = new int[matrix.length - 1][matrix.length - 1];
for (int k = 0; k < matrix.length; k++) {
if (k < i) {
for (int l = 0; l < matrix.length; l++) {
if (l < j) {
subMatrix[k][l] = matrix[k][l];
} else if (l > j) {
subMatrix[k][l - 1] = matrix[k][l];
}
}
} else if (k > i) {
for (int l = 0; l < matrix.length; l++) {
if (l < j) {
subMatrix[k - 1][l] = matrix[k][l];
} else if (l > j) {
subMatrix[k - 1][l - 1] = matrix[k][l];
}
}
}
}
adjugateMatrix[i][j] = determinant(subMatrix) * (int) Math.pow(-1, i + j);
}
}
return adjugateMatrix;
}
private int multiplicativeInverse(int n) {
for (int i = 1; i < 26; i++) {
if ((n * i) % 26 == 1) {
return i;
}
}
throw new ArithmeticException("Multiplicative inverse does not exist.");
}
public static void main(String[] args) {
int[][] keyMatrix = {{3, 2}, {5, 7}};
HillCipher cipher = new HillCipher(keyMatrix);
String message = "hello";
String encryptedMessage = cipher.encrypt(message);
String decryptedMessage = cipher.decrypt(encryptedMessage);
System.out.println("Message: " + message);
System.out.println("Encrypted message: " + encryptedMessage);
System.out.println("Decrypted message: " + decryptedMessage);
}
}
```
在这个实现中,HillCipher类包含了加密和解密方法,构造函数需要一个密钥矩阵。加密和解密方法都需要将字符串转换为向量,并使用矩阵乘法对其进行加密或解密。在getInverseMatrix方法中,使用了求行列式和伴随矩阵的方法来计算矩阵的逆。在multiplicativeInverse方法中,使用了求乘法逆元的方法来计算26的逆元。在main方法中,我们使用了一个2x2的矩阵作为密钥矩阵,对"hello"进行加密和解密的测试。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)