简体   繁体   English

Java-解码base64-非法的base64字符1

[英]Java - decode base64 - Illegal base64 character 1

I have following data in a file: 我在文件中有以下数据: 在此处输入图片说明

I want to decode the UserData. 我想解码UserData。 On reading it as string comment , I'm doing following: 在将其读取为字符串comment ,我正在执行以下操作:

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 java.lang.IllegalArgumentException:非法的base64字符1

What could be the reason? 可能是什么原因?

The image suggests that the string you are trying to decode contains characters like SOH and BEL. 该图像表明您尝试解码的字符串包含SOH和BEL之类的字符。 These are ASCII control characters, and will not ever appear in a Base64 encoded string. 这些是ASCII控制字符,永远不会出现在Base64编码的字符串中。

(Base64 typically consists of letters, digits, and + , \\ and = . There are some variant formats, but control characters are never included.) (Base64通常由字母,数字和+\\= 。虽然有一些变体格式,但从不包含控制字符。)

This is confirmed by the exception message: 异常消息确认了这一点:

  java.lang.IllegalArgumentException: Illegal base64 character 1

The SOH character has ASCII code 1. SOH字符具有ASCII码1。


Conclusions: 结论:

  1. You cannot decode that string as if it was Base64. 您不能将该字符串解码为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. 看起来该字符串根本没有被“编码”,就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! 如何读取数据并将其转换为Java String对象:向我们展示执行此操作的代码!

The UserData field in the picture in the question actually contains Bytes representation of Hexadecimal characters. 问题图片中的UserData字段实际上包含十六进制字符的字节表示。

So, I don't need to decode Base64. 因此,我不需要解码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 输出: UserData:010a20077100000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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