{"record":{"id":"641c10639edf37dc","repo":"TheAlgorithms/Java","slug":"a-padding-must-not-be-followed-by-a-non-paddin","errorCode":null,"errorMessage":"A padding '=' must not be followed by a non-padding character","messagePattern":"A padding '=' must not be followed by a non-padding character","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/Base64.java","lineNumber":128,"sourceCode":"\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\n        for (int i = 0; i < input.length(); i += 4) {\n            // Get up to 4 characters\n            int char1 = getBase64Value(input.charAt(i));\n            int char2 = getBase64Value(input.charAt(i + 1));\n            int char3 = input.charAt(i + 2) == '=' ? 0 : getBase64Value(input.charAt(i + 2));\n            int char4 = input.charAt(i + 3) == '=' ? 0 : getBase64Value(input.charAt(i + 3));\n\n            // Combine four 6-bit groups into a 24-bit number\n            int combined = (char1 << 18) | (char2 << 12) | (char3 << 6) | char4;\n\n            // Extract three 8-bit bytes","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/Base64.java#L110-L146","documentation":"Base64.decode rejects input where a '=' padding character is followed by a non-'=' character (e.g., \"AB=CD\"). RFC 4648 requires that once padding begins, all subsequent characters must also be padding. This catches strings where padding appears, then a data character follows, indicating corruption.","triggerScenarios":"Input like \"AB=C=\" or \"A=BC\" where '=' is followed by a non-'=' character. Strings assembled from fragments where padding leaked into the middle. Base64 strings with trailing whitespace after padding that wasn't stripped.","commonSituations":"Concatenating two Base64 fragments without re-encoding. Log lines or wrapped text where '=' from one chunk precedes data from the next. Whitespace/newline characters appearing after padding.","solutions":["Strip all whitespace and newlines before decoding: input = input.replaceAll(\"\\\\s\", \"\").","Re-encode the source data as a single Base64 string instead of concatenating fragments.","Validate with a regex like ^[A-Za-z0-9+/]*={0,2}$ before calling decode."],"exampleFix":"// before\nString joined = frag1 + frag2; // frag1 ended with '=', frag2 starts with data\nbyte[] out = Base64.decode(joined);\n\n// after\nbyte[] raw = (decodeOrFetch(frag1) + decodeOrFetch(frag2)).getBytes();\nString joined = Base64.encode(raw);","handlingStrategy":"validation","validationCode":"String s = input.replaceAll(\"\\\\s\", \"\");\nint fp = s.indexOf('=');\nif (fp != -1) {\n    for (int i = fp; i < s.length(); i++) {\n        if (s.charAt(i) != '=') throw new IllegalArgumentException(\"non-pad after pad\");\n    }\n}\nbyte[] out = Base64.decode(s);","typeGuard":"static boolean paddingIsContiguousAtEnd(String s) {\n    s = s.replaceAll(\"\\\\s\", \"\");\n    int fp = s.indexOf('=');\n    if (fp == -1) return true;\n    for (int i = fp; i < s.length(); i++) if (s.charAt(i) != '=') return false;\n    return true;\n}","tryCatchPattern":"try {\n    byte[] out = Base64.decode(input);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid Base64 padding: \" + e.getMessage(), e);\n}","preventionTips":["Strip whitespace/newlines that can appear between fragments.","Re-encode concatenated fragments rather than joining raw strings.","Pre-validate with a strict regex before decoding."],"tags":["conversions","base64","rfc-4648","padding","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}