{"record":{"id":"ddee74ee0a64d9b5","repo":"TheAlgorithms/Java","slug":"invalid-baconian-code","errorCode":null,"errorMessage":"Invalid Baconian code: {}","messagePattern":"Invalid Baconian code: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/BaconianCipher.java","lineNumber":65,"sourceCode":"\n        return ciphertext.toString();\n    }\n\n    /**\n     * Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher.\n     *\n     * @param ciphertext The ciphertext to decrypt.\n     * @return The decrypted plaintext message.\n     */\n    public String decrypt(String ciphertext) {\n        StringBuilder plaintext = new StringBuilder();\n\n        for (int i = 0; i < ciphertext.length(); i += 5) {\n            String code = ciphertext.substring(i, i + 5);\n            if (REVERSE_BACONIAN_MAP.containsKey(code)) {\n                plaintext.append(REVERSE_BACONIAN_MAP.get(code));\n            } else {\n                throw new IllegalArgumentException(\"Invalid Baconian code: \" + code);\n            }\n        }\n\n        return plaintext.toString();\n    }\n}\n","sourceCodeStart":47,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/BaconianCipher.java#L47-L72","documentation":"Thrown by BaconianCipher.decrypt(String) when a 5-character group sliced from the ciphertext is not present in REVERSE_BACONIAN_MAP. Bacon's cipher encodes each letter as a 5-symbol group (traditionally 'a'/'b' or '0'/'1'); the decryptor walks the input in 5-char chunks and looks each up, throwing if a chunk is unknown.","triggerScenarios":"Ciphertext whose length is not a multiple of 5 (the final chunk is short/garbled), ciphertext using a different symbol alphabet than the map expects (e.g. 0/1 when the map uses a/b), or corrupted characters.","commonSituations":"Encrypt/decrypt alphabet mismatch; transmission corruption that drops a character (length no longer divisible by 5); assuming a 0/1 encoding when the map uses a/b or vice versa.","solutions":["Verify ciphertext length is an exact multiple of 5 before decrypting.","Confirm the symbol alphabet matches the cipher's REVERSE_BACONIAN_MAP (check whether it uses a/b or 0/1).","Reject or pad truncated ciphertext rather than passing it through."],"exampleFix":"// before\nString plain = cipher.decrypt(text);\n\n// after\nif (text.length() % 5 != 0) {\n    throw new IllegalArgumentException(\"Ciphertext length must be a multiple of 5\");\n}\nString plain = cipher.decrypt(text);","handlingStrategy":"validation","validationCode":"if (ciphertext == null || ciphertext.length() % 5 != 0\n        || !ciphertext.matches(\"[ab01]+\")) {\n    throw new IllegalArgumentException(\"Invalid Baconian ciphertext\");\n}\nString plain = cipher.decrypt(ciphertext);","typeGuard":"static boolean isPlausibleBaconian(String s) {\n    return s != null && s.length() % 5 == 0;\n}","tryCatchPattern":"try {\n    String plain = cipher.decrypt(ciphertext);\n} catch (IllegalArgumentException e) {\n    // a 5-char group was not in the map; check alphabet/length\n}","preventionTips":["Confirm the symbol alphabet matches REVERSE_BACONIAN_MAP (a/b vs 0/1).","Ensure ciphertext length is an exact multiple of 5.","Detect and re-request truncated messages before decrypting."],"tags":["cryptography","ciphers","java","validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}