Xades4j and Base64 in EnvelopedXmlObject

帅比萌擦擦* 提交于 2021-02-11 12:28:46

问题


I tried to insert Base64 string into Object in EnvelopedXmlObject:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);        
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document document = dBuilder.newDocument();  
    String generatedDocument = ...;

    BASE64Encoder encoder = new BASE64Encoder();
    String generatedDocumentEncoded = encoder.encode(generatedDocument .getBytes("UTF-8"));
    Node node1 = document.createTextNode(generatedDocumentEncoded);
    DataObjectDesc dataObjectDesc = new EnvelopedXmlObject   (node1, "plain/text", "http://www.w3.org/2000/09/xmldsig#base64");
    ....

When i run this code in file.xml each line of Object is ended with "
" which cause an error with decoding text

When I add this lines: Node node1 = document.createTextNode(generatedDocumentEncoded);

saveFile("c:\\temp\\temp.xml", generatedDocumentEncoded);
String stringFromFile = read("c:\\temp\\temp.xml");
node1.setTextContent(stringFromFile); 

then generated file is ok - where is my mistake ? :)


回答1:


I think that Base64 encoder You use will produce chunked output (put carriage returns in string). You can replace in output string this characters like below:

generatedDocumentEncoded = generatedDocumentEncoded.replaceAll("(\r\n|\n)", "");
Node node1 = document.createTextNode(generatedDocumentEncoded);

or force Your BASE64Encoder generate not chunked output. I'm using for that Base64 class from Apache Commons:

byte[] bytes = generatedDocument.getBytes("UTF-8");
String generatedDocumentEncoded = new String(Base64.encodeBase64(bytes, false), "UTF8");


来源:https://stackoverflow.com/questions/26293066/xades4j-and-base64-in-envelopedxmlobject

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