I'm trying to sign xlm document using Certificate. Code pretty much looks like this:
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();
Unfortunately signedXML in the signature and certificate part has some very strange characters at the end of line:
<ds:SignatureValue Id="xmldsig-some-id-bla-bla-sigvalue">
blablabla...
...
blablabla...
blabla==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
blablabla...
...
blablabla...
blablabla...
blablabla...
blablabla...
blabla==
</ds:X509Certificate>
I've no idea where from comes this strange
ending.
I've tried may things (like -Dfile.encoding=UTF-8
) to get read of this but without success.
I'm using
<dependency>
<groupId>com.googlecode.xades4j</groupId>
<artifactId>xades4j</artifactId>
<version>1.5.1</version>
</dependency>
Please help me.