{"record":{"id":"a2312ee8646498fb","repo":"TheAlgorithms/Java","slug":"invalid-key-matrix-determinant-is-zero-modulo-26","errorCode":null,"errorMessage":"Invalid key matrix. Determinant is zero modulo 26.","messagePattern":"Invalid key matrix\\. Determinant is zero modulo 26\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/HillCipher.java","lineNumber":75,"sourceCode":"\n            for (int i = 0; i < matrixSize; i++) {\n                plainVector[i] = 0;\n                for (int j = 0; j < matrixSize; j++) {\n                    plainVector[i] += inverseKeyMatrix[i][j] * messageVector[j];\n                }\n                plainVector[i] = plainVector[i] % 26;\n                plainText.append((char) (plainVector[i] + 'A'));\n            }\n        }\n\n        return plainText.toString();\n    }\n\n    // Validates that the determinant of the key matrix is not zero modulo 26\n    private void validateDeterminant(int[][] keyMatrix, int n) {\n        int det = determinant(keyMatrix, n) % 26;\n        if (det == 0) {\n            throw new IllegalArgumentException(\"Invalid key matrix. Determinant is zero modulo 26.\");\n        }\n    }\n\n    // Computes the determinant of a matrix recursively\n    private int determinant(int[][] matrix, int n) {\n        int det = 0;\n        if (n == 1) {\n            return matrix[0][0];\n        }\n        int sign = 1;\n        int[][] subMatrix = new int[n - 1][n - 1];\n        for (int x = 0; x < n; x++) {\n            int subI = 0;\n            for (int i = 1; i < n; i++) {\n                int subJ = 0;\n                for (int j = 0; j < n; j++) {\n                    if (j != x) {\n                        subMatrix[subI][subJ++] = matrix[i][j];","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/HillCipher.java#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nhill = new HillCipher(randomMatrix, n);\n\n// after\nint[][] key;\nint detMod;\ndo {\n    key = randomMatrix(n);\n    detMod = ((determinant(key, n) % 26) + 26) % 26;\n} while (detMod == 0 || gcd(detMod, 26) != 1);\nhill = new HillCipher(key, n);","handlingStrategy":"validation","validationCode":"int detMod = ((determinant(keyMatrix, n) % 26) + 26) % 26;\nif (detMod == 0 || gcd(detMod, 26) != 1) {\n    throw new IllegalArgumentException(\"Key matrix not invertible mod 26\");\n}\nhill = new HillCipher(keyMatrix, n);","typeGuard":"static boolean isInvertibleMod26(int[][] m, int n) {\n    int d = ((determinant(m, n) % 26) + 26) % 26;\n    return d != 0 && gcd(d, 26) == 1;\n}","tryCatchPattern":"try {\n    hill = new HillCipher(keyMatrix, n);\n} catch (IllegalArgumentException e) {\n    // singular matrix; regenerate until invertible\n}","preventionTips":["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."],"tags":["cryptography","ciphers","hill-cipher","java","validation","linear-algebra","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}