TheAlgorithms/Java · error · IllegalArgumentException
Input data contains invalid characters.
Error message
Input data contains invalid characters.
What it means
Thrown by MonoAlphabetic.decrypt(String, String) when a character in data is not found in the key (key.indexOf(c) == -1). The decryptor maps each ciphertext character back through the key, so any character absent from the key — or a key that is not a full permutation of A-Z — fails.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java:32
StringBuilder sb = new StringBuilder();
// Encrypt each character
for (char c : data.toCharArray()) {
int idx = charToPos(c); // Get the index of the character
sb.append(key.charAt(idx)); // Map to the corresponding character in the key
}
return sb.toString();
}
// Decryption method
public static String decrypt(String data, String key) {
StringBuilder sb = new StringBuilder();
// Decrypt each character
for (char c : data.toCharArray()) {
int idx = key.indexOf(c); // Find the index of the character in the key
if (idx == -1) {
throw new IllegalArgumentException("Input data contains invalid characters.");
}
sb.append(posToChar(idx)); // Convert the index back to the original character
}
return sb.toString();
}
// Helper method: Convert a character to its position in the alphabet
private static int charToPos(char c) {
return c - 'A'; // Subtract 'A' to get position (0 for A, 1 for B, etc.)
}
// Helper method: Convert a position in the alphabet to a character
private static char posToChar(int pos) {
return (char) (pos + 'A'); // Add 'A' to convert position back to character
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Use exactly the same key used for encryption when decrypting.
- Validate the key is a 26-letter permutation of A-Z before use.
- Pre-check that every ciphertext character exists in the key (key.indexOf(c) != -1).
Example fix
// before
String plain = MonoAlphabetic.decrypt(cipher, key);
// after
if (!key.matches("(?i)(?:([A-Z])(?!.*\1)){26}")) {
throw new IllegalArgumentException("Key must be a 26-letter permutation of A-Z");
}
String plain = MonoAlphabetic.decrypt(cipher.toUpperCase(), key.toUpperCase()); Defensive patterns
Strategy: validation
Validate before calling
if (!key.matches("(?i)(?:([A-Za-z])(?!.*\\1)){26}")) {
throw new IllegalArgumentException("Key must be a 26-letter permutation of A-Z");
}
for (char c : data.toCharArray()) {
if (key.indexOf(c) == -1) throw new IllegalArgumentException("char not in key: " + c);
}
String plain = MonoAlphabetic.decrypt(data, key); Type guard
static boolean isValidPermutationKey(String key) {
return key != null && key.length() == 26
&& key.chars().distinct().count() == 26
&& key.toUpperCase().chars().allMatch(c -> c >= 'A' && c <= 'Z');
} Try / catch
try {
String plain = MonoAlphabetic.decrypt(data, key);
} catch (IllegalArgumentException e) {
// a char not in key, or wrong key; reconcile with encryption key
} Prevention
- Use the exact same key for decrypt as for encrypt.
- Validate the key is a 26-letter permutation of A-Z.
- Pre-check each ciphertext char exists in the key.
When it happens
Trigger: Passing ciphertext containing a character not present in the supplied key, or using a different key than was used for encryption, or a key that is missing some letters.
Common situations: Encrypt/decrypt key mismatch; a malformed key that isn't a 26-letter permutation of A-Z; ciphertext corrupted with characters outside the key.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Input data contains invalid characters. Only uppercase A-Z a
- Invalid Baconian code: {}
- DES key must be supplied as a 64 character binary string
- Encrypted message should be a multiple of 64 characters in l
- Base, secret, and prime must be non-null and positive values
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/408704b938ac808a.
Report an issue: GitHub.