user5155835
user5155835

Reputation: 4742

Java - decode base64 - Illegal base64 character 1

I have following data in a file: enter image description here

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?

Upvotes: 0

Views: 3153

Answers (2)

user5155835
user5155835

Reputation: 4742

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

Upvotes: 0

Stephen C
Stephen C

Reputation: 719626

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!

Upvotes: 1

Related Questions