{"record":{"id":"be2e072396fef6ad","repo":"TheAlgorithms/Java","slug":"key-must-not-be-empty","errorCode":null,"errorMessage":"Key must not be empty","messagePattern":"Key must not be empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/XORCipher.java","lineNumber":67,"sourceCode":"        byte[] outputBytes = new byte[inputBytes.length];\n        for (int i = 0; i < inputBytes.length; ++i) {\n            outputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]);\n        }\n        return outputBytes;\n    }\n\n    /**\n     * Encrypts the given plaintext using the XOR cipher with the specified key.\n     * The result is a hexadecimal-encoded string representing the ciphertext.\n     *\n     * @param plainText The input plaintext to encrypt.\n     * @param key The encryption key.\n     * @throws IllegalArgumentException if the key is empty.\n     * @return A hexadecimal string representing the encrypted text.\n     */\n    public static String encrypt(final String plainText, final String key) {\n        if (key.isEmpty()) {\n            throw new IllegalArgumentException(\"Key must not be empty\");\n        }\n\n        byte[] plainTextBytes = plainText.getBytes(CS_DEFAULT);\n        byte[] keyBytes = key.getBytes(CS_DEFAULT);\n        byte[] xorResult = xor(plainTextBytes, keyBytes);\n        return HexFormat.of().formatHex(xorResult);\n    }\n\n    /**\n     * Decrypts the given ciphertext (in hexadecimal format) using the XOR cipher\n     * with the specified key. The result is the original plaintext.\n     *\n     * @param cipherText The hexadecimal string representing the encrypted text.\n     * @param key The decryption key (must be the same as the encryption key).\n     * @throws IllegalArgumentException if the key is empty.\n     * @return The decrypted plaintext.\n     */\n    public static String decrypt(final String cipherText, final String key) {","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/XORCipher.java#L49-L85","documentation":"XORCipher.encrypt(String, String) requires a non-empty key because it XORs each plaintext byte against repeating key bytes via xor(plainTextBytes, keyBytes). With an empty key array the XOR cycle is undefined, so the guard fails fast with a descriptive message.","triggerScenarios":"Calling XORCipher.encrypt(text, \"\"); passing a key string that resolved to empty.","commonSituations":"Key not loaded from its source (env var unset, config field blank); key defaulted to empty string; key cleared during a refactor.","solutions":["Provide a non-empty key string such as \"secret\".","Validate the key is non-empty before encrypting.","Load the key through a helper that throws if the source value is missing."],"exampleFix":"// before\nString ct = XORCipher.encrypt(text, key);\n\n// after\nif (key == null || key.isEmpty()) {\n    throw new IllegalStateException(\"XOR key not provided\");\n}\nString ct = XORCipher.encrypt(text, key);","handlingStrategy":"validation","validationCode":"if (key == null || key.isEmpty()) {\n    throw new IllegalStateException(\"XOR key must be non-empty\");\n}\nString ct = XORCipher.encrypt(plainText, key);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Load the key through a helper that throws if the source value is missing.","Validate the key is non-empty before encrypting.","Do not default a missing XOR key to empty."],"tags":["cipher","xor","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}