TheAlgorithms/Java · error · IllegalArgumentException
DES key must be supplied as a 64 character binary string
Error message
DES key must be supplied as a 64 character binary string
What it means
Thrown by the DES constructor (via sanitize) when the supplied key is not exactly 64 characters long. This DES implementation represents the 64-bit key as a binary string of 64 '0'/'1' characters; only the length is checked here — non-binary contents would fail later during subkey generation.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/DES.java:16
package com.thealgorithms.ciphers;
/**
* This class is build to demonstrate the application of the DES-algorithm
* (https://en.wikipedia.org/wiki/Data_Encryption_Standard) on a plain English message. The supplied
* key must be in form of a 64 bit binary String.
*/
public class DES {
private String key;
private final String[] subKeys;
private void sanitize(String key) {
int length = key.length();
if (length != 64) {
throw new IllegalArgumentException("DES key must be supplied as a 64 character binary string");
}
}
DES(String key) {
sanitize(key);
this.key = key;
subKeys = getSubkeys(key);
}
public String getKey() {
return this.key;
}
public void setKey(String key) {
sanitize(key);
this.key = key;
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Supply a 64-character string consisting only of '0' and '1'.
- Convert an existing byte/hex key to its 64-bit binary-string representation before constructing DES.
- Validate length == 64 and contents match [01]{64} before instantiation.
Example fix
// before
DES des = new DES(myHexKey); // e.g. "1A2B3C..."
// after
String binaryKey = padTo64(Long.toBinaryString(parseHex(myHexKey)));
if (!binaryKey.matches("[01]{64}")) throw new IllegalArgumentException("bad key");
DES des = new DES(binaryKey); Defensive patterns
Strategy: validation
Validate before calling
if (key == null || key.length() != 64 || !key.matches("[01]{64}")) {
throw new IllegalArgumentException("DES key must be a 64-char binary string");
}
DES des = new DES(key); Type guard
static boolean isDesBitString(String s) { return s != null && s.matches("[01]{64}"); } Try / catch
try {
DES des = new DES(key);
} catch (IllegalArgumentException e) {
// wrong key format; convert hex/bytes to a 64-char binary string first
} Prevention
- Note this library's key format is a 64-char bit string, not hex or bytes.
- Convert conventional keys to the bit-string form before constructing.
- Validate length and contents before instantiation.
When it happens
Trigger: Passing a hex key string (16 chars), a raw 8-byte key, a Base64 key, or a binary string of any length other than 64.
Common situations: Confusing this library's bit-string format with conventional hex/byte key formats; truncating or padding a key; using a key from another DES library that uses bytes.
Related errors
- Encrypted message should be a multiple of 64 characters in l
- 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/2bfcc85564ac6f8f.
Report an issue: GitHub.