java—如何在签名和证书部分删除xml数字签名中奇怪的行尾?

bfnvny8b  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(260)

我正在尝试使用证书签署xlm文档。代码大致如下:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
InputSource inputSource = new InputSource(new StringReader(xml));
inputSource.setEncoding("UTF-8");
Document document = dbf.newDocumentBuilder().parse(inputSource);
Element element = document.getDocumentElement();
DOMHelper.useIdAsXmlId(element);
FirstCertSelector firstCertSelector = new FirstCertSelector();
PassProvider passProvider = new PassProvider(cert.getCertPass());
KeyingDataProvider kdp = new FileSystemKeyStoreKeyingDataProvider("pkcs12", cert.getCertFilePath(), firstCertSelector, passProvider, passProvider, true);

DataObjectDesc dataObjectDesc = (new DataObjectReference("")).withTransform(new EnvelopedSignatureTransform());
SignedDataObjects signedDataObjects = (new SignedDataObjects()).withSignedDataObject(dataObjectDesc);
XadesSigner xadesSigner = (new XadesBesSigningProfile(kdp)).withAlgorithmsProviderEx(new SigningAlgorithm()).newSigner();
xadesSigner.sign(signedDataObjects, element);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
document.setXmlStandalone(true);

DOMSource source = new DOMSource(document);
StringWriter result = new StringWriter();
transformer.transform(source, new StreamResult(result));
String signedXML = result.toString();

不幸的是,签名和证书部分的signedxml在行尾有一些非常奇怪的字符:

<ds:SignatureValue Id="xmldsig-some-id-bla-bla-sigvalue">
blablabla...&#13;
...
blablabla...&#13;
blabla==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
blablabla...&#13;
...
blablabla...&#13;
blablabla...&#13;
blablabla...&#13;
blablabla...&#13;
blabla==
</ds:X509Certificate>

我不知道这奇怪的是从哪里来的 &#13; 结束。我试过很多事情(比如 -Dfile.encoding=UTF-8 )但没有成功。
我在用

<dependency>
    <groupId>com.googlecode.xades4j</groupId>
    <artifactId>xades4j</artifactId>
    <version>1.5.1</version>
</dependency>

请帮帮我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题