javax.xml.bind's Base64 encoder/decoder eats last two characters of string

泄露秘密 提交于 2019-12-10 17:48:15

问题


I need to convert some strings using Base64 encoding, and was delighted to see that I didn't have to roll my own converter--Java provides one with javax.xml.bind.DataConverter. However, it has some problems. Here's the output of my time with a Jython REPL:

>>> import javax.xml.bind.DatatypeConverter as DC
>>> import java.lang.String as String
>>> def foo(text):
...   return DC.printBase64Binary(DC.parseBase64Binary(String(text)))
... 
>>> foo("hello")
'hell'
>>> foo("This, it's a punctuated sentence.")
'Thisitsapunctuatedsenten'
>>> foo("\"foo\" \"bar\"")
'foob'
>>> foo("\"foo\" \"bar\"12")
'foobar12'
>>> foo("\"foo\" \"bar\"1")
'foob'

As you can see, it doesn't handle non-alphanumeric characters at all, and also frequently--but not always--truncates the string by two characters.

I guess it might be time to just write my own class, but now I'm bothered that either a) I'm failing at reading the javadoc or something b) The class doesn't work as expected.

So any help is much appreciated; thanks in advance.


回答1:


hello is not a base64 String, so the parsing fails. You must convert the string into a byte array (try String(text).getBytes('UTF-8')) and then call DC.printBase64Binary() on the byte array to get the data in Base64.

DC.parseBase64Binary() will then convert this Base64 encoded data back into the byte array (which you can then convert back into a string).




回答2:


A few findings after spending time resolving a similar problem on a GAE platform (Base64 decoder eats last (two) characters when decoding a base64-string from facebook)

If the encoded string is not of a 4*n length then the method DatatypeConverter.parseBase64Binary might drop some trailing characters (rendering the JSON payload syntactically wrong). My solution was to add the following code:

while (payload.length() % 4 != 0) payload += "=";

With regards to the code example in the question, I would suggest a change where the test string gets first encoded and then decoded, ie:

return DC.parseBase64Binary(DC.printBase64Binary(String(text).getBytes()))



回答3:


You're not giving it complete base64 (including final padding) etc to start with. If you give it a complete base64 string, it should be fine.

You should only try to interpret data as if it's base64 if it really is base64 to start with. Doing it with arbitrary character sequences is a bad idea.

It's unclear what you're really trying to do, if you're not actually starting with base64 data. You talk about "converting some strings" - are they base64 or not?




回答4:


I think that the javax.xml.bind.DatatypeConverter class may expect to work with XML data or XSD types, as the JavaDoc method states for the parameter:

A string containing lexical representation of xsd:base64Binary

Personally I wouldn't feel comfortable using a class/library oriented towards XML transformations for something like this.

Take a look at the commons-codec library, which has an easy-to-use Base64 class.




回答5:


import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.lang.String ;
public class HttpBasicAuthenticationHeader {

 public static void main(String[] args) {
 DatatypeConverter dc;

 String str="ENCODE";

String s="";

try {
s=javax.xml.bind.DatatypeConverter.printBase64Binary(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(s);
 }



回答6:


I'm receiving the data in Deflater zip technique. So, a little function to be decompressed is:

public byte[] descomprimir() throws IOException, DataFormatException {
    final String wsData = "eNqzsa/IzVEoSy0qzszPs1Uy1DNQUkjNS85PycxLt1XyDPbXtbAwtdQ1VLK347JJTixJzMlPzy/Wt+MCAAU6ETY=";
    byte[] data = DatatypeConverter.parseBase64Binary(wsData);

    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    return output;
}

Then you can convert the byte to a new String or anything else.



来源:https://stackoverflow.com/questions/6265912/javax-xml-binds-base64-encoder-decoder-eats-last-two-characters-of-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!