javaspringspring-bootbase64decode

Illegal Argument Exception: Illegal base64 character 3a when decoding String value using Base64.getDecode()


The string value I'm decoding is "ed:1234" but it is throwing an error of IllegalArgumentException. Would greatly appreciate if someone knows why I have this error.

Code:

String authInfo = "ed:1234";
byte[] bytes = Base64.getDecoder().decode(authInfo);

Error:

java.lang.IllegalArgumentException: Illegal base64 character 3a

Solution

  • The issue is the : (ascii decimal 58 or hex 3a) is only valid in one (of several) Base64 encoding schemes, you want Base64.getMimeDecoder(). Like,

    byte[] bytes = Base64.getMimeDecoder().decode(authInfo);
    System.out.println(Arrays.toString(bytes));
    

    which outputs (with no other changes)

    [121, -35, 118, -33]