TheAlgorithms/Java · error · IllegalArgumentException
Invalid key matrix. Determinant is zero modulo 26.
Error message
Invalid key matrix. Determinant is zero modulo 26.
What it means
Thrown by HillCipher.validateDeterminant when the key matrix's determinant modulo 26 equals zero. A Hill cipher requires the key matrix to be invertible modulo 26 so ciphertext can be decrypted; a determinant of 0 (mod 26) means the matrix is singular and decryption is impossible.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/HillCipher.java:75
for (int i = 0; i < matrixSize; i++) {
plainVector[i] = 0;
for (int j = 0; j < matrixSize; j++) {
plainVector[i] += inverseKeyMatrix[i][j] * messageVector[j];
}
plainVector[i] = plainVector[i] % 26;
plainText.append((char) (plainVector[i] + 'A'));
}
}
return plainText.toString();
}
// Validates that the determinant of the key matrix is not zero modulo 26
private void validateDeterminant(int[][] keyMatrix, int n) {
int det = determinant(keyMatrix, n) % 26;
if (det == 0) {
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
}
}
// Computes the determinant of a matrix recursively
private int determinant(int[][] matrix, int n) {
int det = 0;
if (n == 1) {
return matrix[0][0];
}
int sign = 1;
int[][] subMatrix = new int[n - 1][n - 1];
for (int x = 0; x < n; x++) {
int subI = 0;
for (int i = 1; i < n; i++) {
int subJ = 0;
for (int j = 0; j < n; j++) {
if (j != x) {
subMatrix[subI][subJ++] = matrix[i][j];View on GitHub (pinned to fdfb9a395b)
Solutions
- Use a key matrix whose determinant is non-zero AND coprime with 26 (gcd(det, 26) == 1) for full invertibility.
- Regenerate random key matrices until one passes the determinant check.
- Validate the matrix before assigning it as the cipher key.
Example fix
// before
hill = new HillCipher(randomMatrix, n);
// after
int[][] key;
int detMod;
do {
key = randomMatrix(n);
detMod = ((determinant(key, n) % 26) + 26) % 26;
} while (detMod == 0 || gcd(detMod, 26) != 1);
hill = new HillCipher(key, n); Defensive patterns
Strategy: validation
Validate before calling
int detMod = ((determinant(keyMatrix, n) % 26) + 26) % 26;
if (detMod == 0 || gcd(detMod, 26) != 1) {
throw new IllegalArgumentException("Key matrix not invertible mod 26");
}
hill = new HillCipher(keyMatrix, n); Type guard
static boolean isInvertibleMod26(int[][] m, int n) {
int d = ((determinant(m, n) % 26) + 26) % 26;
return d != 0 && gcd(d, 26) == 1;
} Try / catch
try {
hill = new HillCipher(keyMatrix, n);
} catch (IllegalArgumentException e) {
// singular matrix; regenerate until invertible
} Prevention
- Require gcd(det mod 26, 26) == 1 for full invertibility, not just non-zero.
- Regenerate random matrices until one passes.
- Avoid matrices with all-even entries or repeated rows.
When it happens
Trigger: Providing a key matrix whose determinant ≡ 0 (mod 26). This occurs with matrices that have linearly dependent rows modulo 26, all-even entries, or certain repeated/redundant structures.
Common situations: Random key generation without checking invertibility; user-supplied key matrices with repeated rows; keys that look valid over the reals but are singular mod 26 (e.g. determinant 26, 52).
Related errors
- Invalid Baconian code: {}
- DES key must be supplied as a 64 character binary string
- Encrypted message should be a multiple of 64 characters in l
- Base, secret, and prime must be non-null and positive values
- Other public value must be non-null and positive.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/a2312ee8646498fb.
Report an issue: GitHub.