简体   繁体   中英

Java - decode base64 - Illegal base64 character 1

I have following data in a file: 在此处输入图片说明

I want to decode the UserData. On reading it as string comment , I'm doing following:

String[] split = comment.split("=");
if(split[0].equals("UserData")) {
    System.out.println(split[1]);
    byte[] callidArray = Arrays.copyOf(java.util.Base64.getDecoder().decode(split[1]), 9);
    System.out.println("UserData:" + Hex.encodeHexString(callidArray).toString());
}

But I'm getting the following exception:

java.lang.IllegalArgumentException: Illegal base64 character 1

What could be the reason?

The image suggests that the string you are trying to decode contains characters like SOH and BEL. These are ASCII control characters, and will not ever appear in a Base64 encoded string.

(Base64 typically consists of letters, digits, and + , \\ and = . There are some variant formats, but control characters are never included.)

This is confirmed by the exception message:

  java.lang.IllegalArgumentException: Illegal base64 character 1

The SOH character has ASCII code 1.


Conclusions:

  1. You cannot decode that string as if it was Base64. It won't work.
  2. It looks like the string is not "encoded" at all ... in the normal sense of what "encoding" means in Java.
  3. We can't advise you on what you should do with it without a clear explanation of:

    • where the (binary) data comes from,
    • what you expected it to contain, and
    • how you read the data and turned it into a Java String object: show us the code that did that!

The UserData field in the picture in the question actually contains Bytes representation of Hexadecimal characters.

So, I don't need to decode Base64. I need to copy the string to a byte array and get equivalent hexadecimal characters of the byte array.

String[] split = comment.split("=");
if(split[0].equals("UserData")) {
    System.out.println(split[1]);
    byte[] callidArray = Arrays.copyOf(split[1].getBytes(), 9);
    System.out.println("UserData:" + Hex.encodeHexString(callidArray).toString());
}

Output: UserData:010a20077100000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM