TheAlgorithms/Java · error · IllegalArgumentException
Encrypted message should be a multiple of 64 characters in l
Error message
Encrypted message should be a multiple of 64 characters in length
What it means
Thrown by DES.decrypt(String) when the ciphertext length is not a multiple of 64. This implementation treats each 64-bit block as a 64-character binary substring and processes them in a loop; a length not divisible by 64 means the input is malformed or truncated.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/DES.java:237
for (j = 0; j < 8; j++) {
bitBlock.append(pad(Integer.toBinaryString(bytes[j]), 8));
}
encryptedMessage.append(encryptBlock(bitBlock.toString(), subKeys));
}
return encryptedMessage.toString();
}
/**
* @param message The encrypted string. Expects it to be a multiple of 64 bits, in binary format
* @return The decrypted String, in plain English
*/
public String decrypt(String message) {
StringBuilder decryptedMessage = new StringBuilder();
int l = message.length();
int i;
int j;
if (l % 64 != 0) {
throw new IllegalArgumentException("Encrypted message should be a multiple of 64 characters in length");
}
for (i = 0; i < l; i += 64) {
String block = message.substring(i, i + 64);
String result = decryptBlock(block, subKeys);
byte[] res = new byte[8];
for (j = 0; j < 64; j += 8) {
res[j / 8] = (byte) Integer.parseInt(result.substring(j, j + 8), 2);
}
decryptedMessage.append(new String(res));
}
return decryptedMessage.toString().replace("\0", ""); // Get rid of the null bytes used for padding
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the ciphertext is the binary-string output produced by this DES implementation's encrypt method.
- Verify length % 64 == 0 before calling decrypt.
- If ciphertext was truncated, re-obtain a complete copy rather than decrypting a partial message.
Example fix
// before
String out = des.decrypt(received);
// after
if (received.length() % 64 != 0 || !received.matches("[01]+")) {
throw new IllegalArgumentException("Ciphertext must be a binary string, length multiple of 64");
}
String out = des.decrypt(received); Defensive patterns
Strategy: validation
Validate before calling
if (ciphertext == null || ciphertext.length() % 64 != 0
|| !ciphertext.matches("[01]+")) {
throw new IllegalArgumentException("Ciphertext must be a binary string with length % 64 == 0");
}
String out = des.decrypt(ciphertext); Type guard
static boolean isDesCiphertext(String s) {
return s != null && s.length() % 64 == 0 && s.matches("[01]+");
} Try / catch
try {
String out = des.decrypt(ciphertext);
} catch (IllegalArgumentException e) {
// malformed/truncated ciphertext; re-obtain a complete copy
} Prevention
- Only feed ciphertext produced by this DES implementation's encrypt.
- Check length % 64 == 0 and contents before decrypting.
- Do not decrypt partial/truncated messages.
When it happens
Trigger: Passing ciphertext that is not in the library's 64-char-per-block binary-string format, or ciphertext that was truncated/corrupted so its length is no longer a multiple of 64.
Common situations: Feeding hex or Base64 ciphertext instead of the binary-string format; partial transmission; mixing this DES implementation's format with another tool's output.
Related errors
- DES key must be supplied as a 64 character binary string
- Invalid Baconian code: {}
- 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/d375864474a6c087.
Report an issue: GitHub.