- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathHillCipher.java
103 lines (90 loc) · 3.57 KB
/
HillCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
packagecom.thealgorithms.ciphers;
publicclassHillCipher {
// Encrypts the message using the key matrix
publicStringencrypt(Stringmessage, int[][] keyMatrix) {
message = message.toUpperCase().replaceAll("[^A-Z]", "");
intmatrixSize = keyMatrix.length;
validateDeterminant(keyMatrix, matrixSize);
StringBuildercipherText = newStringBuilder();
int[] messageVector = newint[matrixSize];
int[] cipherVector = newint[matrixSize];
intindex = 0;
while (index < message.length()) {
for (inti = 0; i < matrixSize; i++) {
if (index < message.length()) {
messageVector[i] = message.charAt(index++) - 'A';
} else {
messageVector[i] = 'X' - 'A'; // Padding with 'X' if needed
}
}
for (inti = 0; i < matrixSize; i++) {
cipherVector[i] = 0;
for (intj = 0; j < matrixSize; j++) {
cipherVector[i] += keyMatrix[i][j] * messageVector[j];
}
cipherVector[i] = cipherVector[i] % 26;
cipherText.append((char) (cipherVector[i] + 'A'));
}
}
returncipherText.toString();
}
// Decrypts the message using the inverse key matrix
publicStringdecrypt(Stringmessage, int[][] inverseKeyMatrix) {
message = message.toUpperCase().replaceAll("[^A-Z]", "");
intmatrixSize = inverseKeyMatrix.length;
validateDeterminant(inverseKeyMatrix, matrixSize);
StringBuilderplainText = newStringBuilder();
int[] messageVector = newint[matrixSize];
int[] plainVector = newint[matrixSize];
intindex = 0;
while (index < message.length()) {
for (inti = 0; i < matrixSize; i++) {
if (index < message.length()) {
messageVector[i] = message.charAt(index++) - 'A';
} else {
messageVector[i] = 'X' - 'A'; // Padding with 'X' if needed
}
}
for (inti = 0; i < matrixSize; i++) {
plainVector[i] = 0;
for (intj = 0; j < matrixSize; j++) {
plainVector[i] += inverseKeyMatrix[i][j] * messageVector[j];
}
plainVector[i] = plainVector[i] % 26;
plainText.append((char) (plainVector[i] + 'A'));
}
}
returnplainText.toString();
}
// Validates that the determinant of the key matrix is not zero modulo 26
privatevoidvalidateDeterminant(int[][] keyMatrix, intn) {
intdet = determinant(keyMatrix, n) % 26;
if (det == 0) {
thrownewIllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
}
}
// Computes the determinant of a matrix recursively
privateintdeterminant(int[][] matrix, intn) {
intdet = 0;
if (n == 1) {
returnmatrix[0][0];
}
intsign = 1;
int[][] subMatrix = newint[n - 1][n - 1];
for (intx = 0; x < n; x++) {
intsubI = 0;
for (inti = 1; i < n; i++) {
intsubJ = 0;
for (intj = 0; j < n; j++) {
if (j != x) {
subMatrix[subI][subJ++] = matrix[i][j];
}
}
subI++;
}
det += sign * matrix[0][x] * determinant(subMatrix, n - 1);
sign = -sign;
}
returndet;
}
}