TheAlgorithms/Java · error · IllegalArgumentException
Invalid Baconian code: {}
Error message
Invalid Baconian code: {} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/BaconianCipher.java:65
return ciphertext.toString();
}
/**
* Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher.
*
* @param ciphertext The ciphertext to decrypt.
* @return The decrypted plaintext message.
*/
public String decrypt(String ciphertext) {
StringBuilder plaintext = new StringBuilder();
for (int i = 0; i < ciphertext.length(); i += 5) {
String code = ciphertext.substring(i, i + 5);
if (REVERSE_BACONIAN_MAP.containsKey(code)) {
plaintext.append(REVERSE_BACONIAN_MAP.get(code));
} else {
throw new IllegalArgumentException("Invalid Baconian code: " + code);
}
}
return plaintext.toString();
}
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
String plain = cipher.decrypt(text);
// after
if (text.length() % 5 != 0) {
throw new IllegalArgumentException("Ciphertext length must be a multiple of 5");
}
String plain = cipher.decrypt(text); Defensive patterns
Strategy: validation
Validate before calling
if (ciphertext == null || ciphertext.length() % 5 != 0
|| !ciphertext.matches("[ab01]+")) {
throw new IllegalArgumentException("Invalid Baconian ciphertext");
}
String plain = cipher.decrypt(ciphertext); Type guard
static boolean isPlausibleBaconian(String s) {
return s != null && s.length() % 5 == 0;
} Try / catch
try {
String plain = cipher.decrypt(ciphertext);
} catch (IllegalArgumentException e) {
// a 5-char group was not in the map; check alphabet/length
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 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
- Other public value must be non-null and positive.
- Bit length must be at least {} for security.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ddee74ee0a64d9b5.
Report an issue: GitHub.