Base64: java.lang.IllegalArgumentException: Illegal character Base64: java.lang.IllegalArgumentException: Illegal character java java

Base64: java.lang.IllegalArgumentException: Illegal character


Your encoded text is [B@6499375d. That is not Base64, something went wrong while encoding. That decoding code looks good.

Use this code to convert the byte[] to a String before adding it to the URL:

String encodedEmailString = new String(encodedEmail, "UTF-8");// ...String confirmLink = "Complete your registration by clicking on following"    + "\n<a href='" + confirmationURL + encodedEmailString + "'>link</a>";


I encountered this error since my encoded image started with data:image/png;base64,iVBORw0....

This answer led me to the solution:

String partSeparator = ",";if (data.contains(partSeparator)) {  String encodedImg = data.split(partSeparator)[1];  byte[] decodedImg = Base64.getDecoder().decode(encodedImg.getBytes(StandardCharsets.UTF_8));  Path destinationFile = Paths.get("/path/to/imageDir", "myImage.png");  Files.write(destinationFile, decodedImg);}

That code removes the meta data in front of the Base64-encoded image and passes the Base64 string to Java's Base64.Decoder to get the image as bytes.


Just use the below code to resolve this:

JsonObject obj = Json.createReader(new ByteArrayInputStream(Base64.getDecoder().decode(accessToken.split("\\.")[1].                        replace('-', '+').replace('_', '/')))).readObject();

In the above code replace('-', '+').replace('_', '/') did the job. For more details see the https://jwt.io/js/jwt.js. I understood the problem from the part of the code got from that link:

function url_base64_decode(str) {  var output = str.replace(/-/g, '+').replace(/_/g, '/');  switch (output.length % 4) {    case 0:      break;    case 2:      output += '==';      break;    case 3:      output += '=';      break;    default:      throw 'Illegal base64url string!';  }  var result = window.atob(output); //polifyll https://github.com/davidchambers/Base64.js  try{    return decodeURIComponent(escape(result));  } catch (err) {    return result;  }}