TheAlgorithms/Java · error · IllegalArgumentException
Key cannot be empty.
Error message
Key cannot be empty.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/Vigenere.java:48
/**
* Encrypts a given message using the Vigenère Cipher with the specified key.
* Steps:
* 1. Iterate over each character in the message.
* 2. If the character is a letter, shift it by the corresponding character in the key.
* 3. Preserve the case of the letter.
* 4. Preserve non-alphabetic characters.
* 5. Move to the next character in the key (cyclic).
* 6. Return the encrypted message.
*
* @param message The plaintext message to encrypt.
* @param key The keyword used for encryption.
* @throws IllegalArgumentException if the key is empty.
* @return The encrypted message.
*/
public String encrypt(final String message, final String key) {
if (key.isEmpty()) {
throw new IllegalArgumentException("Key cannot be empty.");
}
StringBuilder result = new StringBuilder();
int j = 0;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
result.append((char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A'));
} else {
result.append((char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a'));
}
j = ++j % key.length();
} else {
result.append(c);
}
}
return result.toString();View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
String out = vigenere.encrypt(text, key);
// after
if (key == null || key.isEmpty()) {
throw new IllegalStateException("Vigenere key not configured");
}
String out = vigenere.encrypt(text, key); Defensive patterns
Strategy: validation
Validate before calling
if (key == null || key.isEmpty()) {
throw new IllegalStateException("Vigenere key must be non-empty");
}
String out = vigenere.encrypt(message, key); Prevention
- 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.
When it happens
Trigger: Calling vigenere.encrypt(text, ""); passing a key variable that resolved to an empty string.
Common situations: 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.
Related errors
- Key cannot be null or empty
- Key must contain integers from 1 to {}
- Key must contain each position exactly once
- Message is empty
- Key must not be empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/936bf34b91a9c792.
Report an issue: GitHub.