{"record":{"id":"bec00ac5e0b6acf8","repo":"apache/dubbo","slug":"base64-code-length-64","errorCode":null,"errorMessage":"Base64 code length < 64.","messagePattern":"Base64 code length < 64\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":500,"sourceCode":"     *\n     * @param b    byte array.\n     * @param code base64 code string(0-63 is base64 char,64 is pad char).\n     * @return base64 string.\n     */\n    public static String bytes2base64(byte[] b, String code) {\n        return bytes2base64(b, 0, b.length, code);\n    }\n\n    /**\n     * to base64 string.\n     *\n     * @param b    byte array.\n     * @param code base64 code string(0-63 is base64 char,64 is pad char).\n     * @return base64 string.\n     */\n    public static String bytes2base64(byte[] b, int offset, int length, String code) {\n        if (code.length() < 64) {\n            throw new IllegalArgumentException(\"Base64 code length < 64.\");\n        }\n\n        return bytes2base64(b, offset, length, code.toCharArray());\n    }\n\n    /**\n     * to base64 string.\n     *\n     * @param b    byte array.\n     * @param code base64 code(0-63 is base64 char,64 is pad char).\n     * @return base64 string.\n     */\n    public static String bytes2base64(byte[] b, char[] code) {\n        return bytes2base64(b, 0, b.length, code);\n    }\n\n    /**\n     * to base64 string.","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L482-L518","documentation":"Thrown by Bytes.bytes2base64(byte[], int, int, String code) when the custom base64 alphabet string 'code' has fewer than 64 characters. Base64 requires exactly 64 encoding characters (indices 0-63); an optional 65th character is the pad. A code shorter than 64 cannot map all 6-bit values and is rejected before any encoding happens.","triggerScenarios":"Calling the bytes2base64 overload that accepts a custom String alphabet with a code argument shorter than 64 chars. The String overload delegates to the char[] overload after this check. Passing a truncated or hand-written alphabet triggers it.","commonSituations":"Using a custom (e.g., URL-safe) base64 alphabet but providing an incomplete character set; copy-paste that dropped characters; mistaking the alphabet for a key/seed. The default Bytes.BASE64 constant is correct, so this only happens with custom alphabets.","solutions":["Use the default alphabet by calling bytes2base64(byte[]) or bytes2base64(byte[], int, int) which use Bytes.BASE64.","If a custom alphabet is required, ensure it has exactly 64 (optionally 65 with pad) characters - validate code.length() >= 64.","Use a well-known constant for the custom alphabet rather than inline literals."],"exampleFix":"// before\nString urlSafe = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"; // 64 ok, but a typo can drop one\nString b64 = Bytes.bytes2base64(data, 0, data.length, urlSafe); // throws [158] if < 64\n\n// after: prefer the default or validate\nString b64 = Bytes.bytes2base64(data); // uses Bytes.BASE64","handlingStrategy":"validation","validationCode":"if (code == null || code.length() < 64) {\n    throw new IllegalArgumentException(\"base64 alphabet must have >= 64 chars: \" + (code == null ? 0 : code.length()));\n}\nString b64 = Bytes.bytes2base64(data, off, len, code);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the default-alphabet overloads bytes2base64(byte[]) unless a custom alphabet is truly needed.","Define custom alphabets as named constants and unit-test their length.","Never derive the alphabet from variable/untrusted input."],"tags":["bytes","base64","validation","encoding"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}