Base64: Java.Lang.Illegalargumentexception: Illegal Character

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>";

java.lang.IllegalArgumentException: Illegal base64 character a

the answer will be just to get a way to remove those line breaks as they are not valid base64 characters. I changed the code as follows:

String mfstr = "BZCaRm9ChAbA58sgSwlhzgVuPwboh2qvgcPuLxKJkLpesdHvyZtaheThUSw6%2BHItGBtgimHpXqbn%0ApggBaRR2wisjNiyQrX03eEJlet6%2BqFL6TouRr0wW3NLRZSHOHUSFtJkpq0cyXy%2FSfMVB47y93xlq%0Az845uXSTK2Vi%2FgzwFVphHd%2BTK%2FrO%2FDxJ4EfvAoW0zxeYS%2BCWnIsl%2F4ILehVYasGtxC%2FjbG1I8S%2Fc%0AZoqXIcPmPWrszbG7R1ouDQ473TyCMLx9PBsl1Z%2Bj39V4Qr01ZRw7GVP2m%2Bk4xrHg2Im1OuXpd2vl%0AKGwe5j2T1ZHtoYxCvXOOU1YeJYSR%2Ff7Kd7KbpnjvFT2Ua%2FOdHx%2FKzBoK3Yk97fdvMcelUGMxveKq%0A8C9aCXFVU1xjK81CwB72QWkK5%2B8DCjItVDFcpnVFnhk8ZwlYKU6o8jETDockNMKiDmBYqKGpnNII%0ACnQBGiWy0inWj40k8VoFNIuVK1yYzLoVvFrYR514Ex6U2AK00c0f7C2C5vISsOEp%2BW8KHG2hFW7G%0A97IgwnX3vtQc0s0SaZ%2B7SPgbxwUujlULaOa0t5W9ZDs9b7jDyBZA7m8DiFrLH2YpzpBhrHXcw%2B%2BZ%0A";

String carg = URLDecoder.decode(mfstr, "UTF-8");

carg = carg.replace("\n","");

byte[] v5 = Base64.getDecoder().decode(carg.getBytes());

Thanks @Tom for the head up discussion.

java.lang.IllegalArgumentException: Illegal base64 character 5b

So Same code was working fine in my local machine in Java8 and Java11 but in Docker Container it was failing so i used the little different method in place of getDecoder() i used getMimeDecoder()

So previous code was

byte encodedCert[] = Base64.getDecoder().decode(CertString);

Which is now


Illegal base64 character "a" using java.util.Base64 from within Scala

The following snippet decodes the encoding correctly:

val decodedWithMime = java.util.Base64.getMimeDecoder.decode(contentEncoded)
val convertedByteArray =

as pointed out by comments, the error message Illegal Base64 character a corresponds to the hex value for the newline character \n. Using the Mime Decoder it is possible to decode the string without removing the newline characters beforehand.

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

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);

which outputs (with no other changes)

[121, -35, 118, -33]

Related Topics

Leave a reply