One file, different lines in base64 after convert base64 to org.w3c.dom.document on Java

Viewed 124

There is a service that receives an xml file in base64.

For example this :

PGFkcmVzc2Jvb2s+CiA8Y29udGFjdCBudW1iZXI9IjEiPgogIDxuYW1lPlBpZ2d5PC9uYW1lPgogIDxwaG9uZT4rNDkgNjMxMzIyMTg3PC9waG9uZT4KICA8ZW1haWw+cGlnZ3lAbWVnYS5kZTwvZW1haWw+CiA8L2NvbnRhY3Q+CiA8Y29udGFjdCBudW1iZXI9IjIiPgogIDxuYW1lPktlcm1pdDwvbmFtZT4KICA8cGhvbmU+KzQ5IDYzMTMyMjE4MTwvcGhvbmU+CiAgPGVtYWlsPmtlcm1pdEBtZWdhLmRlPC9lbWFpbD4KIDwvY29udGFjdD4KIDxjb250YWN0IG51bWJlcj0iMyI+CiAgPG5hbWU+R29uem88L25hbWU+CiAgPHBob25lPis0OSA2MzEzMjIxODY8L3Bob25lPgogIDxlbWFpbD5nb256b0BtZWdhLmRlPC9lbWFpbD4KIDwvY29udGFjdD4KPC9hZHJlc3Nib29rPg==

From it I get an object of type org.w3c.dom.document.

 public static Document convertBase64ToDocument(String documentBase64) throws ResponseException {

    byte[] bytes = Base64.decode(documentBase64);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(bytes));
    } catch (Exception e) {
        ResponseException rs = new ResponseException(ExceptionMessages.ConvertException + " " + e.getMessage(),
                ErrorCodes.convertExceptionCode);
        xmlUtilsLogger.error(rs.getMessage() + ErrorCodes.namePrint + rs.getNumber());
        throw rs;
    }
}

I don't do anything with it, and I convert it back to base64.

public static String documentToBase64(Document document) throws ResponseException {
   return ObjectUtils.encodeByteToBase64(XmlUtils.documentToByteArray(document));
}

public static byte[] documentToByteArray(Document document) throws ResponseException {
    try {
        Source source = new DOMSource(document);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(bos);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
        transformer.transform(source, result);

        return bos.toByteArray();
    } catch (TransformerException e) {
        ResponseException rs = new ResponseException(ExceptionMessages.ConvertException + " " + e.getMessage(),
                                                     ErrorCodes.convertExceptionCode);
        xmlUtilsLogger.error(rs.getMessage() + ErrorCodes.namePrint + rs.getNumber());
        throw rs;
    }
}

And I get the modified base64 string.

PGFkcmVzc2Jvb2s+DQogPGNvbnRhY3QgbnVtYmVyPSIxIj4NCiAgPG5hbWU+UGlnZ3k8L25hbWU+DQogIDxwaG9uZT4rNDkgNjMxMzIyMTg3PC9waG9uZT4NCiAgPGVtYWlsPnBpZ2d5QG1lZ2EuZGU8L2VtYWlsPg0KIDwvY29udGFjdD4NCiA8Y29udGFjdCBudW1iZXI9IjIiPg0KICA8bmFtZT5LZXJtaXQ8L25hbWU+DQogIDxwaG9uZT4rNDkgNjMxMzIyMTgxPC9waG9uZT4NCiAgPGVtYWlsPmtlcm1pdEBtZWdhLmRlPC9lbWFpbD4NCiA8L2NvbnRhY3Q+DQogPGNvbnRhY3QgbnVtYmVyPSIzIj4NCiAgPG5hbWU+R29uem88L25hbWU+DQogIDxwaG9uZT4rNDkgNjMxMzIyMTg2PC9waG9uZT4NCiAgPGVtYWlsPmdvbnpvQG1lZ2EuZGU8L2VtYWlsPg0KIDwvY29udGFjdD4NCjwvYWRyZXNzYm9vaz4=

In appearance, xml is no different.

<adressbook>
 <contact number="1">
  <name>Piggy</name>
  <phone>+49 631322187</phone>
  <email>piggy@mega.de</email>
 </contact>
 <contact number="2">
  <name>Kermit</name>
  <phone>+49 631322181</phone>
  <email>kermit@mega.de</email>
 </contact>
 <contact number="3">
  <name>Gonzo</name>
  <phone>+49 631322186</phone>
  <email>gonzo@mega.de</email>
 </contact>
</adressbook>

As you can see additional information (transformer.setOutputProperty (OutputKeys.OMIT_XML_DECLARATION, "yes");) was not added.

In the future, you need to work out a hash from this data. Naturally, it will be different. The question is why they are different

1 Answers

The difference is that the original string (A) had linebreaks coded as LF (ASCII 0a) but the result (B) has them as CR LF (ASCII 0d 0a)

You can see this easily when you convert the base64 string to HEX

A: 3c616472657373626f6f6b3e0a203c636f6e
B: 3c616472657373626f6f6b3e0d0a203c636f

Looks like the transformer doesn't offer an option for the linebreaks.

Related