{"record":{"id":"bbe171e32e7aa140","repo":"TheAlgorithms/Java","slug":"key-must-contain-integers-from-1-to","errorCode":null,"errorMessage":"Key must contain integers from 1 to {}","messagePattern":"Key must contain integers from 1 to (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/PermutationCipher.java","lineNumber":97,"sourceCode":"        // Remove padding characters from the end\r\n        return removePadding(decrypted.toString());\r\n    }\r\n    /**\r\n     * Validates that the permutation key is valid.\r\n     * A valid key must contain all integers from 1 to n exactly once, where n is the key length.\r\n     *\r\n     * @param key the permutation key to validate\r\n     * @throws IllegalArgumentException if the key is invalid\r\n     */\r\n    private void validateKey(int[] key) {\r\n        if (key == null || key.length == 0) {\r\n            throw new IllegalArgumentException(\"Key cannot be null or empty\");\r\n        }\r\n\r\n        Set<Integer> keySet = new HashSet<>();\r\n        for (int position : key) {\r\n            if (position < 1 || position > key.length) {\r\n                throw new IllegalArgumentException(\"Key must contain integers from 1 to \" + key.length);\r\n            }\r\n            if (!keySet.add(position)) {\r\n                throw new IllegalArgumentException(\"Key must contain each position exactly once\");\r\n            }\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Pads the text with padding characters to make its length divisible by the block size.\r\n     *\r\n     * @param text the text to pad\r\n     * @param blockSize the size of each block\r\n     * @return the padded text\r\n     */\r\n    private String padText(String text, int blockSize) {\r\n        int remainder = text.length() % blockSize;\r\n        if (remainder == 0) {\r\n            return text;\r","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/PermutationCipher.java#L79-L115","documentation":"After confirming the key is non-empty, validateKey() iterates each element and checks it falls in [1, key.length]. Any element equal to 0, negative, or greater than the block size is rejected because the cipher uses these values as 1-based character indices inside each block, and an out-of-range index would read past (or before) the block.","triggerScenarios":"Passing a zero-based key like {0,1,2} (contains 0); a key with a gap such as {1,2,4} for length 3 (4 > 3); or a negative value like {1,-2,3}.","commonSituations":"Developer assumed 0-based indexing and wrote {0,1,...,n-1}; a programmatically generated key has an off-by-one that pushes the last value past n; a typo in a hand-authored key.","solutions":["Use 1-based positions: for a block of size n, every element must satisfy 1 <= element <= n.","Generate the key by shuffling a 1..n list so no value can fall outside the range.","Add a unit test asserting every key element is in [1, key.length] before calling the cipher."],"exampleFix":"// before (0-based, triggers the error)\nint[] key = {0, 1, 2};\ncipher.encrypt(text, key);\n\n// after (1-based)\nint[] key = {3, 1, 2};\ncipher.encrypt(text, key);","handlingStrategy":"validation","validationCode":"for (int v : key) {\n    if (v < 1 || v > key.length) {\n        throw new IllegalArgumentException(\"Key element \" + v + \" out of range [1,\" + key.length + \"]\");\n    }\n}\ncipher.encrypt(text, key);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the cipher uses 1-based positions, not 0-based indices.","Generate keys by shuffling a 1..n list to keep all values in range.","Assert in tests that every key element lies in [1, key.length]."],"tags":["cipher","permutation","off-by-one","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}