{"record":{"id":"321d686ab8132573","repo":"TheAlgorithms/Java","slug":"key-length-must-match-input-length","errorCode":null,"errorMessage":"Key length must match input length","messagePattern":"Key length must match input length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/OneTimePadCipher.java","lineNumber":78,"sourceCode":"     * <p>For a One-Time Pad, decryption is identical to encryption:\n     * {@code plaintext = ciphertext XOR key}.\n     *\n     * @param ciphertext the ciphertext bytes, must not be {@code null}\n     * @param key the one-time pad key bytes, must not be {@code null}\n     * @return the decrypted plaintext bytes\n     * @throws IllegalArgumentException if the key length does not match ciphertext length\n     * @throws NullPointerException if ciphertext or key is {@code null}\n     */\n    public static byte[] decrypt(byte[] ciphertext, byte[] key) {\n        validateInputs(ciphertext, key);\n        return xor(ciphertext, key);\n    }\n\n    private static void validateInputs(byte[] input, byte[] key) {\n        Objects.requireNonNull(input, \"input must not be null\");\n        Objects.requireNonNull(key, \"key must not be null\");\n        if (input.length != key.length) {\n            throw new IllegalArgumentException(\"Key length must match input length\");\n        }\n    }\n\n    private static byte[] xor(byte[] data, byte[] key) {\n        byte[] result = new byte[data.length];\n        for (int i = 0; i < data.length; i++) {\n            result[i] = (byte) (data[i] ^ key[i]);\n        }\n        return result;\n    }\n}\n","sourceCodeStart":60,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/OneTimePadCipher.java#L60-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Generate the key with exactly the plaintext length: generateKey(plaintext.length).","Pre-check key.length == data.length before encrypt/decrypt.","Do not reuse or truncate one-time-pad keys; regenerate per message."],"exampleFix":"// before\nbyte[] key = OneTimePadCipher.generateKey(16);\nbyte[] ct = OneTimePadCipher.encrypt(plaintext, key); // fails if plaintext.length != 16\n\n// after\nbyte[] key = OneTimePadCipher.generateKey(plaintext.length);\nbyte[] ct = OneTimePadCipher.encrypt(plaintext, key);","handlingStrategy":"validation","validationCode":"if (key.length != data.length) {\n    throw new IllegalArgumentException(\"key length (\" + key.length + \") must equal data length (\" + data.length + \")\");\n}\nbyte[] out = OneTimePadCipher.encrypt(data, key);","typeGuard":"static boolean lengthsMatch(byte[] data, byte[] key) {\n    return data != null && key != null && data.length == key.length;\n}","tryCatchPattern":"try {\n    byte[] ct = OneTimePadCipher.encrypt(data, key);\n} catch (IllegalArgumentException e) {\n    // length mismatch; regenerate key at the right length\n    byte[] k = OneTimePadCipher.generateKey(data.length);\n    ct = OneTimePadCipher.encrypt(data, k);\n}","preventionTips":["Generate the key at plaintext length: generateKey(plaintext.length).","Never reuse or truncate one-time-pad keys.","Pre-check length equality before encrypt/decrypt."],"tags":["cryptography","ciphers","one-time-pad","java","validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}