{"record":{"id":"408704b938ac808a","repo":"TheAlgorithms/Java","slug":"input-data-contains-invalid-characters","errorCode":null,"errorMessage":"Input data contains invalid characters.","messagePattern":"Input data contains invalid characters\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java","lineNumber":32,"sourceCode":"        StringBuilder sb = new StringBuilder();\n\n        // Encrypt each character\n        for (char c : data.toCharArray()) {\n            int idx = charToPos(c); // Get the index of the character\n            sb.append(key.charAt(idx)); // Map to the corresponding character in the key\n        }\n        return sb.toString();\n    }\n\n    // Decryption method\n    public static String decrypt(String data, String key) {\n        StringBuilder sb = new StringBuilder();\n\n        // Decrypt each character\n        for (char c : data.toCharArray()) {\n            int idx = key.indexOf(c); // Find the index of the character in the key\n            if (idx == -1) {\n                throw new IllegalArgumentException(\"Input data contains invalid characters.\");\n            }\n            sb.append(posToChar(idx)); // Convert the index back to the original character\n        }\n        return sb.toString();\n    }\n\n    // Helper method: Convert a character to its position in the alphabet\n    private static int charToPos(char c) {\n        return c - 'A'; // Subtract 'A' to get position (0 for A, 1 for B, etc.)\n    }\n\n    // Helper method: Convert a position in the alphabet to a character\n    private static char posToChar(int pos) {\n        return (char) (pos + 'A'); // Add 'A' to convert position back to character\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java#L14-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"Encrypt/decrypt key mismatch; a malformed key that isn't a 26-letter permutation of A-Z; ciphertext corrupted with characters outside the key.","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)."],"exampleFix":"// before\nString plain = MonoAlphabetic.decrypt(cipher, key);\n\n// after\nif (!key.matches(\"(?i)(?:([A-Z])(?!.*\\1)){26}\")) {\n    throw new IllegalArgumentException(\"Key must be a 26-letter permutation of A-Z\");\n}\nString plain = MonoAlphabetic.decrypt(cipher.toUpperCase(), key.toUpperCase());","handlingStrategy":"validation","validationCode":"if (!key.matches(\"(?i)(?:([A-Za-z])(?!.*\\\\1)){26}\")) {\n    throw new IllegalArgumentException(\"Key must be a 26-letter permutation of A-Z\");\n}\nfor (char c : data.toCharArray()) {\n    if (key.indexOf(c) == -1) throw new IllegalArgumentException(\"char not in key: \" + c);\n}\nString plain = MonoAlphabetic.decrypt(data, key);","typeGuard":"static boolean isValidPermutationKey(String key) {\n    return key != null && key.length() == 26\n        && key.chars().distinct().count() == 26\n        && key.toUpperCase().chars().allMatch(c -> c >= 'A' && c <= 'Z');\n}","tryCatchPattern":"try {\n    String plain = MonoAlphabetic.decrypt(data, key);\n} catch (IllegalArgumentException e) {\n    // a char not in key, or wrong key; reconcile with encryption key\n}","preventionTips":["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."],"tags":["cryptography","ciphers","java","validation","string","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}