<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How to sign an xml files with PEM certificate ?</title><link>https://community.appian.com/discussions/f/general/28595/how-to-sign-an-xml-files-with-pem-certificate</link><description>We have imported our certificate in the admin console =&amp;gt; Integration =&amp;gt;Client Certificates, is there any way of retrieving the signature form here to sign the xml document ?</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: How to sign an xml files with PEM certificate ?</title><link>https://community.appian.com/thread/112649?ContentTypeID=1</link><pubDate>Fri, 12 May 2023 14:02:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:6318def7-7f94-4cb0-9f78-634d62ba7902</guid><dc:creator>felixr</dc:creator><description>&lt;p&gt;Hi, &lt;a href="/members/rachelg9844"&gt;rachelg9844&lt;/a&gt; thanks for replaying&amp;nbsp; i was trying to work with the code but unfortunately&lt;br /&gt;&lt;br /&gt;this&amp;nbsp;&amp;nbsp; is not&amp;nbsp;&amp;nbsp; working&amp;nbsp; by chance am i passing the wrong parameters ?. the only changes that i don&amp;#39;t have like this are the password every other letter is the same. for some reason is throwing:&lt;br /&gt;&lt;br /&gt;Exception in thread &amp;quot;main&amp;quot; java.lang.NullPointerException: Cannot invoke &amp;quot;java.security.KeyStore$PrivateKeyEntry.getCertificate()&amp;quot; because &amp;quot;keyEntry&amp;quot; is null&lt;br /&gt;&lt;br /&gt;do you have any advice for this and thanks. &amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;import java.security.*;&lt;br /&gt;&lt;br /&gt;import java.security.cert.Certificate;&lt;br /&gt;&lt;br /&gt;import java.security.cert.CertificateException;&lt;br /&gt;&lt;br /&gt;import java.security.cert.X509Certificate;&lt;br /&gt;&lt;br /&gt;import java.util.Collections;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import javax.xml.XMLConstants;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dom.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dsig.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dsig.dom.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dsig.keyinfo.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;&lt;br /&gt;&lt;br /&gt;import javax.xml.crypto.dsig.spec.TransformParameterSpec;&lt;br /&gt;&lt;br /&gt;import javax.xml.parsers.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.transform.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.transform.dom.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.transform.stream.*;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import org.w3c.dom.Document;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class SigngerForXml {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;public static void main(String[] args) throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Load the XML document&lt;br /&gt;&lt;br /&gt;DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();&lt;br /&gt;&lt;br /&gt;DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();&lt;br /&gt;&lt;br /&gt;Document doc = dBuilder.parse(new File(&amp;quot;./documents/request.xml&amp;quot;));&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Load the certificate&lt;br /&gt;&lt;br /&gt;KeyStore ks = KeyStore.getInstance(&amp;quot;JKS&amp;quot;);&lt;br /&gt;&lt;br /&gt;ks.load(new FileInputStream(&amp;quot;./documents/4300648_identity.p12&amp;quot;), &amp;quot;mypassword&amp;quot;.toCharArray());&lt;br /&gt;&lt;br /&gt;KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) &lt;br /&gt;&lt;br /&gt;ks.getEntry(&amp;quot;4300648_identity&amp;quot;, new KeyStore.PasswordProtection (&amp;quot;mypassword&amp;quot;.toCharArray()));&lt;br /&gt;&lt;br /&gt;X509Certificate cert = (X509Certificate) keyEntry.getCertificate();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Create the XML signature&lt;br /&gt;&lt;br /&gt;DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), &lt;br /&gt;&lt;br /&gt;doc.getDocumentElement());&lt;br /&gt;&lt;br /&gt;XMLSignatureFactory fac = XMLSignatureFactory.getInstance(&amp;quot;DOM&amp;quot;);&lt;br /&gt;&lt;br /&gt;Reference ref = fac.newReference(&amp;quot;&amp;quot;, fac.newDigestMethod(DigestMethod.SHA256, null),&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;null)), null, null);&lt;br /&gt;&lt;br /&gt;SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod&lt;br /&gt;&lt;br /&gt;(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null),&lt;br /&gt;&lt;br /&gt;&amp;nbsp;fac.newSignatureMethod(SignatureMethod.RSA_SHA256, null), Collections.singletonList(ref));&lt;br /&gt;&lt;br /&gt;KeyInfoFactory kif = fac.getKeyInfoFactory();&lt;br /&gt;&lt;br /&gt;X509Data x509d = kif.newX509Data(Collections.singletonList(cert));&lt;br /&gt;&lt;br /&gt;KeyInfo ki = kif.newKeyInfo(Collections.singletonList(x509d));&lt;br /&gt;&lt;br /&gt;XMLSignature signature = fac.newXMLSignature(si, ki);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Sign the XML document&lt;br /&gt;&lt;br /&gt;signature.sign(dsc);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Save the signed XML document&lt;br /&gt;&lt;br /&gt;TransformerFactory tf = TransformerFactory.newInstance();&lt;br /&gt;&lt;br /&gt;Transformer trans = tf.newTransformer();&lt;br /&gt;&lt;br /&gt;trans.transform(new DOMSource(doc), new StreamResult(new File(&amp;quot;./documents/response.xml&amp;quot;))); }}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to sign an xml files with PEM certificate ?</title><link>https://community.appian.com/thread/112548?ContentTypeID=1</link><pubDate>Thu, 11 May 2023 05:58:45 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:044e50c1-eec2-40a2-a492-a9bc9377a067</guid><dc:creator>rachelg9844</dc:creator><description>[deleted]&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to sign an xml files with PEM certificate ?</title><link>https://community.appian.com/thread/112404?ContentTypeID=1</link><pubDate>Tue, 09 May 2023 02:14:03 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:40e8b80e-8147-4a79-98c9-537a8e677d77</guid><dc:creator>Mathieu Drouin</dc:creator><description>&lt;p&gt;Not possible AFAIK.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>