5

I have this string data to base64 decode

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

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

when I ran this code I am getting the error: java.lang.IllegalArgumentException: Illegal base64 character a, I have tried so far to pass StandardCharsets.UTF_8 in carg.getBytes() But nothing. what am I doing wrong here?

hanan
  • 532
  • 2
  • 7
  • 23
  • That code works fine for me. Is that really the same base64 string? Can there be non-printable chars hidden in there? – Tom Jun 17 '21 at 12:24
  • **1,** Assuming the line breaks do not exist in the actual code. **2.** Adding a Charset to `getBytes` should be done as the bytes are expected to represent a (superset of) ASCII. So if the platform, operating system, would use UTF-16LE, the code would break. **3.** To check for invisible control chars: `if (carg.length() != carg.getBytes(StandardCharsets.UTF_8).length) ...`. **4.** You can pass the String i.o. the bytes – Joop Eggen Jun 17 '21 at 12:24
  • @Tom It is the same base64 string. do I need to get rid of new lines? – hanan Jun 17 '21 at 12:26
  • 1
    In your current code here? Yes, that is currently not compilable. – Tom Jun 17 '21 at 12:28
  • the code was initially driven from android but now I am trying same base64 on java running on WINDOWS 10. I dont if this is the effect. – hanan Jun 17 '21 at 12:28
  • @Tom I have updated the code. Look at it. – hanan Jun 17 '21 at 12:30
  • 1
    Your URL encoded String contains line breaks (`%0A`), those aren't valid base64 characters. – Tom Jun 17 '21 at 12:34
  • @Tom so How this worked in android but not here on windows? is there a way I can dodge it? – hanan Jun 17 '21 at 12:36
  • Impossible to say since we don't know your system. But you should try to find the place which adds the line breaks and fix it there. – Tom Jun 17 '21 at 12:37

1 Answers1

7

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.

hanan
  • 532
  • 2
  • 7
  • 23