TheAlgorithms/Java · error · IllegalArgumentException

Key length must match input length

Error message

Key length must match input length

What it means

Thrown by OneTimePadCipher.validateInputs when input.length != key.length. A one-time pad XORs each plaintext/ciphertext byte with a corresponding key byte, so the key must be exactly the same length as the data. Applies to both encrypt and decrypt, which both route through validateInputs.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/OneTimePadCipher.java:78

     * <p>For a One-Time Pad, decryption is identical to encryption:
     * {@code plaintext = ciphertext XOR key}.
     *
     * @param ciphertext the ciphertext bytes, must not be {@code null}
     * @param key the one-time pad key bytes, must not be {@code null}
     * @return the decrypted plaintext bytes
     * @throws IllegalArgumentException if the key length does not match ciphertext length
     * @throws NullPointerException if ciphertext or key is {@code null}
     */
    public static byte[] decrypt(byte[] ciphertext, byte[] key) {
        validateInputs(ciphertext, key);
        return xor(ciphertext, key);
    }

    private static void validateInputs(byte[] input, byte[] key) {
        Objects.requireNonNull(input, "input must not be null");
        Objects.requireNonNull(key, "key must not be null");
        if (input.length != key.length) {
            throw new IllegalArgumentException("Key length must match input length");
        }
    }

    private static byte[] xor(byte[] data, byte[] key) {
        byte[] result = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = (byte) (data[i] ^ key[i]);
        }
        return result;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Generate the key with exactly the plaintext length: generateKey(plaintext.length).
  2. Pre-check key.length == data.length before encrypt/decrypt.
  3. Do not reuse or truncate one-time-pad keys; regenerate per message.

Example fix

// before
byte[] key = OneTimePadCipher.generateKey(16);
byte[] ct = OneTimePadCipher.encrypt(plaintext, key); // fails if plaintext.length != 16

// after
byte[] key = OneTimePadCipher.generateKey(plaintext.length);
byte[] ct = OneTimePadCipher.encrypt(plaintext, key);
Defensive patterns

Strategy: validation

Validate before calling

if (key.length != data.length) {
    throw new IllegalArgumentException("key length (" + key.length + ") must equal data length (" + data.length + ")");
}
byte[] out = OneTimePadCipher.encrypt(data, key);

Type guard

static boolean lengthsMatch(byte[] data, byte[] key) {
    return data != null && key != null && data.length == key.length;
}

Try / catch

try {
    byte[] ct = OneTimePadCipher.encrypt(data, key);
} catch (IllegalArgumentException e) {
    // length mismatch; regenerate key at the right length
    byte[] k = OneTimePadCipher.generateKey(data.length);
    ct = OneTimePadCipher.encrypt(data, k);
}

Prevention

When it happens

Trigger: Calling encrypt or decrypt with a key whose byte length differs from the data byte length. Also note null inputs throw NullPointerException (via Objects.requireNonNull) before this check.

Common situations: Reusing or truncating a key; deriving key length from a different source than the message; generating a key for the wrong message size; multi-block messages with a single-block key.

Related errors


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