{"record":{"id":"f985e20d9316b322","repo":"TheAlgorithms/Java","slug":"invalid-base64-character","errorCode":null,"errorMessage":"Invalid Base64 character: {}","messagePattern":"Invalid Base64 character: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/Base64.java","lineNumber":200,"sourceCode":"     * Gets the numeric value of a Base64 character.\n     *\n     * @param c the Base64 character\n     * @return the numeric value (0-63)\n     * @throws IllegalArgumentException if character is not a valid Base64 character\n     */\n    private static int getBase64Value(char c) {\n        if (c >= 'A' && c <= 'Z') {\n            return c - 'A';\n        } else if (c >= 'a' && c <= 'z') {\n            return c - 'a' + 26;\n        } else if (c >= '0' && c <= '9') {\n            return c - '0' + 52;\n        } else if (c == '+') {\n            return 62;\n        } else if (c == '/') {\n            return 63;\n        } else {\n            throw new IllegalArgumentException(\"Invalid Base64 character: \" + c);\n        }\n    }\n}\n","sourceCodeStart":182,"sourceCodeEnd":204,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/Base64.java#L182-L204","documentation":"Thrown by the private Base64.getBase64Value(char) helper when a character is outside the standard Base64 alphabet (A-Z, a-z, 0-9, '+', '/'). This is RFC 4648 standard alphabet only; URL-safe characters ('-' and '_') are not accepted, and whitespace/newlines are not skipped.","triggerScenarios":"Passing URL-safe Base64 that uses '-' or '_' instead of '+' or '/'. Passing input with embedded whitespace, newlines, or carriage returns. Passing a character outside the 65-symbol alphabet (including padding in a data position).","commonSituations":"JWTs and URLs use base64url encoding which is incompatible. Base64 output wrapped at 76 columns (MIME) contains newlines. Copy-paste from sources that introduced stray characters.","solutions":["For URL-safe input, translate '-' -> '+' and '_' -> '/', or use java.util.Base64.getUrlDecoder().","Strip all whitespace and newlines: input.replaceAll(\"\\\\s\", \"\").","Switch to java.util.Base64.getMimeDecoder() for MIME-wrapped input with line breaks."],"exampleFix":"// before\nbyte[] out = Base64.decode(\"a-b_c\"); // URL-safe chars rejected\n\n// after\nString std = input.replace('-', '+').replace('_', '/');\nwhile (std.length() % 4 != 0) std += \"=\";\nbyte[] out = Base64.decode(std);","handlingStrategy":"validation","validationCode":"String s = input.replaceAll(\"\\\\s\", \"\").replace('-', '+').replace('_', '/');\nwhile (s.length() % 4 != 0) s += \"=\";\nfor (char c : s.toCharArray()) {\n    if (!(Character.isLetterOrDigit(c) || c == '+' || c == '/' || c == '=')) {\n        throw new IllegalArgumentException(\"invalid char: \" + c);\n    }\n}\nbyte[] out = Base64.decode(s);","typeGuard":"static boolean isStandardBase64Alphabet(String s) {\n    for (char c : s.toCharArray()) {\n        if (!(Character.isLetterOrDigit(c) || c == '+' || c == '/' || c == '=')) return false;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    byte[] out = Base64.decode(input);\n} catch (IllegalArgumentException e) {\n    // try URL-safe or MIME decoder\n    out = java.util.Base64.getUrlDecoder().decode(input);\n}","preventionTips":["Convert URL-safe chars ('-','_') to ('+','/') before decoding.","Strip whitespace and newlines from wrapped (MIME) Base64.","Use java.util.Base64.getMimeDecoder()/getUrlDecoder() for non-standard variants."],"tags":["conversions","base64","rfc-4648","character-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}