{"record":{"id":"2bfcc85564ac6f8f","repo":"TheAlgorithms/Java","slug":"des-key-must-be-supplied-as-a-64-character-binary","errorCode":null,"errorMessage":"DES key must be supplied as a 64 character binary string","messagePattern":"DES key must be supplied as a 64 character binary string","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/DES.java","lineNumber":16,"sourceCode":"package com.thealgorithms.ciphers;\n\n/**\n * This class is build to demonstrate the application of the DES-algorithm\n * (https://en.wikipedia.org/wiki/Data_Encryption_Standard) on a plain English message. The supplied\n * key must be in form of a 64 bit binary String.\n */\npublic class DES {\n\n    private String key;\n    private final String[] subKeys;\n\n    private void sanitize(String key) {\n        int length = key.length();\n        if (length != 64) {\n            throw new IllegalArgumentException(\"DES key must be supplied as a 64 character binary string\");\n        }\n    }\n\n    DES(String key) {\n        sanitize(key);\n        this.key = key;\n        subKeys = getSubkeys(key);\n    }\n\n    public String getKey() {\n        return this.key;\n    }\n\n    public void setKey(String key) {\n        sanitize(key);\n        this.key = key;\n    }\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/DES.java#L1-L34","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nDES des = new DES(myHexKey); // e.g. \"1A2B3C...\"\n\n// after\nString binaryKey = padTo64(Long.toBinaryString(parseHex(myHexKey)));\nif (!binaryKey.matches(\"[01]{64}\")) throw new IllegalArgumentException(\"bad key\");\nDES des = new DES(binaryKey);","handlingStrategy":"validation","validationCode":"if (key == null || key.length() != 64 || !key.matches(\"[01]{64}\")) {\n    throw new IllegalArgumentException(\"DES key must be a 64-char binary string\");\n}\nDES des = new DES(key);","typeGuard":"static boolean isDesBitString(String s) { return s != null && s.matches(\"[01]{64}\"); }","tryCatchPattern":"try {\n    DES des = new DES(key);\n} catch (IllegalArgumentException e) {\n    // wrong key format; convert hex/bytes to a 64-char binary string first\n}","preventionTips":["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."],"tags":["cryptography","ciphers","des","java","validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}