{"record":{"id":"51e9dc1e7c27551b","repo":"TheAlgorithms/Java","slug":"invalid-base64-input-length-must-be-multiple-of-4","errorCode":null,"errorMessage":"Invalid Base64 input length; must be multiple of 4","messagePattern":"Invalid Base64 input length; must be multiple of 4","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/Base64.java","lineNumber":117,"sourceCode":"    /**\n     * Decodes the given Base64 encoded string to a byte array.\n     *\n     * @param input the Base64 encoded string to decode\n     * @return the decoded byte array\n     * @throws IllegalArgumentException if input is null or contains invalid Base64 characters\n     */\n    public static byte[] decode(String input) {\n        if (input == null) {\n            throw new IllegalArgumentException(\"Input cannot be null\");\n        }\n\n        if (input.isEmpty()) {\n            return new byte[0];\n        }\n\n        // Strict RFC 4648 compliance: length must be a multiple of 4\n        if (input.length() % 4 != 0) {\n            throw new IllegalArgumentException(\"Invalid Base64 input length; must be multiple of 4\");\n        }\n\n        // Validate padding: '=' can only appear at the end (last 1 or 2 chars)\n        int firstPadding = input.indexOf('=');\n        if (firstPadding != -1) {\n            if (firstPadding < input.length() - 2) {\n                throw new IllegalArgumentException(\"Padding '=' can only appear at the end (last 1 or 2 characters)\");\n            }\n            for (int i = firstPadding; i < input.length(); i++) {\n                if (input.charAt(i) != '=') {\n                    throw new IllegalArgumentException(\"A padding '=' must not be followed by a non-padding character\");\n                }\n            }\n        }\n\n        List<Byte> result = new ArrayList<>();\n\n        // Process input in groups of 4 characters","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/Base64.java#L99-L135","documentation":"Base64.decode enforces RFC 4648 strict compliance: the input length must be a multiple of 4 characters. Standard Base64 encodes every 3 bytes into 4 characters and pads with '=' to reach a multiple of 4, so any other length indicates corruption, truncation, or non-standard encoding (e.g., URL-safe without padding).","triggerScenarios":"Passing a Base64 string that was truncated by a transport that strips '=' padding. Passing URL-safe Base64 (no padding). Passing a string with whitespace or newlines that shifted the length. Manually constructed strings of the wrong length.","commonSituations":"JWT or data URIs where padding was stripped. Copy-paste truncation. Logs that wrapped lines. Bases64 variants (base64url) that omit padding.","solutions":["Restore padding: while (input.length() % 4 != 0) input += \"=\";","Switch to java.util.Base64.getDecoder() (lenient) or Base64.getUrlDecoder() for URL-safe input.","Strip whitespace/newlines before decoding: input.replaceAll(\"\\\\s\", \"\")."],"exampleFix":"// before\nbyte[] out = Base64.decode(\"SGVsbG8\"); // length 7, not multiple of 4\n\n// after\nString s = \"SGVsbG8\";\nwhile (s.length() % 4 != 0) s += \"=\";\nbyte[] out = Base64.decode(s);","handlingStrategy":"validation","validationCode":"String s = input == null ? \"\" : input.replaceAll(\"\\\\s\", \"\");\nwhile (s.length() % 4 != 0) s += \"=\";\nbyte[] out = Base64.decode(s);","typeGuard":"static boolean isValidBase64Length(String s) {\n    s = s.replaceAll(\"\\\\s\", \"\");\n    return s.length() % 4 == 0;\n}","tryCatchPattern":"try {\n    byte[] out = Base64.decode(input);\n} catch (IllegalArgumentException e) {\n    // try re-padding or switch to java.util.Base64\n    out = java.util.Base64.getDecoder().decode(input);\n}","preventionTips":["Strip whitespace/newlines before decoding.","Restore missing '=' padding to a multiple of 4.","Use java.util.Base64.getUrlDecoder() for URL-safe (unpadded) input."],"tags":["conversions","base64","rfc-4648","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}