0

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?

user5155835
  • 4,392
  • 4
  • 53
  • 97
  • 1
    you can try `getMimeDecoder()` instead of `getDecoder()` - the returned decoder does ignore invalid characters, but probably correctly dealing with SOH and BEL is recommened. Documentation extract: "*MIME: ... All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation." – user85421 Aug 17 '19 at 06:31

2 Answers2

1

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!
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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

user5155835
  • 4,392
  • 4
  • 53
  • 97
  • Well yes ... but this could break if the way you got the `comment` string is sensitive to the platform text encoding scheme. Embedding binary data in a "string" is kind of dodgy. – Stephen C Aug 17 '19 at 11:37
  • @StephenC Thank you, currently the string is not sensitive to the platform – user5155835 Aug 18 '19 at 10:51
  • You misunderstand me. `new String(byte[])` is sensitive to the platform, because it depends on the platform dependent charset that is used to "decode" the bytes into a string. – Stephen C Aug 19 '19 at 09:25