TheAlgorithms/Java · error · IllegalArgumentException

Key must contain integers from 1 to {}

Error message

Key must contain integers from 1 to {}

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/PermutationCipher.java:97

        // Remove padding characters from the end
        return removePadding(decrypted.toString());
    }
    /**
     * Validates that the permutation key is valid.
     * A valid key must contain all integers from 1 to n exactly once, where n is the key length.
     *
     * @param key the permutation key to validate
     * @throws IllegalArgumentException if the key is invalid
     */
    private void validateKey(int[] key) {
        if (key == null || key.length == 0) {
            throw new IllegalArgumentException("Key cannot be null or empty");
        }

        Set<Integer> keySet = new HashSet<>();
        for (int position : key) {
            if (position < 1 || position > key.length) {
                throw new IllegalArgumentException("Key must contain integers from 1 to " + key.length);
            }
            if (!keySet.add(position)) {
                throw new IllegalArgumentException("Key must contain each position exactly once");
            }
        }
    }

    /**
     * Pads the text with padding characters to make its length divisible by the block size.
     *
     * @param text the text to pad
     * @param blockSize the size of each block
     * @return the padded text
     */
    private String padText(String text, int blockSize) {
        int remainder = text.length() % blockSize;
        if (remainder == 0) {
            return text;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use 1-based positions: for a block of size n, every element must satisfy 1 <= element <= n.
  2. Generate the key by shuffling a 1..n list so no value can fall outside the range.
  3. Add a unit test asserting every key element is in [1, key.length] before calling the cipher.

Example fix

// before (0-based, triggers the error)
int[] key = {0, 1, 2};
cipher.encrypt(text, key);

// after (1-based)
int[] key = {3, 1, 2};
cipher.encrypt(text, key);
Defensive patterns

Strategy: validation

Validate before calling

for (int v : key) {
    if (v < 1 || v > key.length) {
        throw new IllegalArgumentException("Key element " + v + " out of range [1," + key.length + "]");
    }
}
cipher.encrypt(text, key);

Prevention

When it happens

Trigger: 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}.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/bbe171e32e7aa140. Report an issue: GitHub.