{"record":{"id":"936bf34b91a9c792","repo":"TheAlgorithms/Java","slug":"key-cannot-be-empty","errorCode":null,"errorMessage":"Key cannot be empty.","messagePattern":"Key cannot be empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/Vigenere.java","lineNumber":48,"sourceCode":"\n    /**\n     * Encrypts a given message using the Vigenère Cipher with the specified key.\n     * Steps:\n     * 1. Iterate over each character in the message.\n     * 2. If the character is a letter, shift it by the corresponding character in the key.\n     * 3. Preserve the case of the letter.\n     * 4. Preserve non-alphabetic characters.\n     * 5. Move to the next character in the key (cyclic).\n     * 6. Return the encrypted message.\n     *\n     * @param message The plaintext message to encrypt.\n     * @param key The keyword used for encryption.\n     * @throws IllegalArgumentException if the key is empty.\n     * @return The encrypted message.\n     */\n    public String encrypt(final String message, final String key) {\n        if (key.isEmpty()) {\n            throw new IllegalArgumentException(\"Key cannot be empty.\");\n        }\n\n        StringBuilder result = new StringBuilder();\n        int j = 0;\n        for (int i = 0; i < message.length(); i++) {\n            char c = message.charAt(i);\n            if (Character.isLetter(c)) {\n                if (Character.isUpperCase(c)) {\n                    result.append((char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A'));\n                } else {\n                    result.append((char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a'));\n                }\n                j = ++j % key.length();\n            } else {\n                result.append(c);\n            }\n        }\n        return result.toString();","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/Vigenere.java#L30-L66","documentation":"Vigenere.encrypt(String, String) rejects an empty key because the cipher cycles through key characters to shift each plaintext letter — with zero key characters there is nothing to cycle through, and key.charAt(j) would throw StringIndexOutOfBoundsException. The guard converts that into a clear, intentional failure.","triggerScenarios":"Calling vigenere.encrypt(text, \"\"); passing a key variable that resolved to an empty string.","commonSituations":"Key field left blank in configuration; key read from an environment variable that was unset; key derived from another source that produced an empty string.","solutions":["Supply a non-empty alphabetic keyword such as \"LEMON\".","Validate the key is non-empty (and ideally alphabetic) before calling encrypt.","Read keys from a source that fails loudly when the value is missing rather than defaulting to empty."],"exampleFix":"// before\nString out = vigenere.encrypt(text, key);\n\n// after\nif (key == null || key.isEmpty()) {\n    throw new IllegalStateException(\"Vigenere key not configured\");\n}\nString out = vigenere.encrypt(text, key);","handlingStrategy":"validation","validationCode":"if (key == null || key.isEmpty()) {\n    throw new IllegalStateException(\"Vigenere key must be non-empty\");\n}\nString out = vigenere.encrypt(message, key);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Load keys from a source that fails loudly when the value is missing.","Validate the key is non-empty (and alphabetic) before encrypting.","Avoid defaulting a missing key to an empty string."],"tags":["cipher","vigenere","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}