Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:30:36+00:00 2026-05-29T11:30:36+00:00

I am trying to sign an XML document with a private key and a

  • 0

I am trying to sign an XML document with a private key and a certificate which are retrieved from the database correctly. This private key and certificate where given to me so I am not quite sure about the provider equivalence in Java. For signing the XML document I used this tutorial

The code I implemented for doing this is the following:

        PrivateKey pk = null;
        javax.security.cert.X509Certificate cert = null;
        try{
            ByteArrayInputStream bis = new ByteArrayInputStream(keyCer.getLlave());
            ObjectInput in = new ObjectInputStream(bis);
            pk = (PrivateKey) in.readObject(); 
            bis.close();

            bis = new ByteArrayInputStream(keyCer.getCertificado());
            in = new ObjectInputStream(bis);
            cert = (javax.security.cert.X509Certificate) in.readObject(); 
            bis.close();

            keyCer.setCertB64(Base64.encodeBase64String(cert.getEncoded()));

            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            dbfac.setNamespaceAware(true);
            DocumentBuilder docBuilder;
            docBuilder = dbfac.newDocumentBuilder();
            DOMImplementation domImpl = docBuilder.getDOMImplementation();
            Document doc = domImpl.createDocument("http://cancelacfd.sat.gob.mx", "Cancelacion", null);
            doc.setXmlVersion("1.0");
            doc.setXmlStandalone(true);

            Element cancelacion = doc.getDocumentElement();
            cancelacion.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsd","http://www.w3.org/2001/XMLSchema");
            cancelacion.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
            cancelacion.setAttribute("RfcEmisor", rfc);
            cancelacion.setAttribute("Fecha", fecha);

            Element folios = doc.createElement("Folios");
            cancelacion.appendChild(folios);
            for (int i=0; i<uuid.length; i++) {
                Element u = doc.createElement("UUID");
                u.setTextContent(uuid[i]);
                folios.appendChild(u);
            }

            DOMSignContext dsc = new DOMSignContext (pk, doc.getDocumentElement()); 
            XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");

            Reference ref = fac.newReference ("", fac.newDigestMethod(DigestMethod.SHA1, null), 
                        Collections.singletonList
                        (fac.newTransform(Transform.ENVELOPED,
                        (TransformParameterSpec) null)), null, null);

            SignedInfo si = fac.newSignedInfo
                      (fac.newCanonicalizationMethod
                        (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                          (C14NMethodParameterSpec) null),
                        fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null),
                        Collections.singletonList(ref)); 

            KeyInfoFactory kif = fac.getKeyInfoFactory();
            KeyValue kv = kif.newKeyValue(cert.getPublicKey());
            KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv)); 

            XMLSignature signature = fac.newXMLSignature(si, ki);

            signature.sign(dsc);

            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            trans.setOutputProperty(OutputKeys.VERSION, "1.0");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //CREAR STRING DEL ARBOL XML
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();
            System.out.println(xmlString);

           } catch (Exception e) {
               e.printStackTrace();
           }

When running the program I get the following exception:

javax.xml.crypto.dsig.XMLSignatureException: java.security.InvalidKeyException: No installed provider supports this key: sun.security.rsa.RSAPrivateCrtKeyImpl
     [java]     at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.sign(Unknown Source)
     [java]     at prueba.cancelaciones.CancelaFE.doPost(CancelaFE.java:140)
     [java]     at prueba.cancelaciones.CancelaFE.doGet(CancelaFE.java:66)
     [java]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
     [java]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
     [java]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
     [java]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
     [java]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
     [java]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
     [java]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
     [java]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
     [java]     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
     [java]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
     [java]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
     [java]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
     [java]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
     [java]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
     [java]     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
     [java]     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
     [java]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     [java]     at java.lang.Thread.run(Unknown Source)
     [java] Caused by: java.security.InvalidKeyException: No installed provider supports this key: sun.security.rsa.RSAPrivateCrtKeyImpl
     [java]     at java.security.Signature$Delegate.chooseProvider(Unknown Source)
     [java]     at java.security.Signature$Delegate.engineInitSign(Unknown Source)
     [java]     at java.security.Signature.initSign(Unknown Source)
     [java]     at org.jcp.xml.dsig.internal.dom.DOMSignatureMethod.sign(Unknown Source)
     [java]     ... 21 more

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:30:37+00:00Added an answer on May 29, 2026 at 11:30 am

    The problem was mine, I was specifying wrongly the Signature Method. It should be:

    SignedInfo si = fac.newSignedInfo
                          (fac.newCanonicalizationMethod
                            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                              (C14NMethodParameterSpec) null),
                            fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
                            Collections.singletonList(ref)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sign some X509 certificates. My root private key is an
I'm trying to sign a message using a private key that is encrypted, I
I'm trying to digitally sign an XML document using Java. I've got an implementation
I'm trying to sign a xml document (http://www.w3.org/TR/xmldsig-core/) with an enveloped signature, I managed
I'm trying to get the client's certificate and sign an xml file using it.
I'm trying to digitally sign a PDF file using THIS project as an example.
I'm trying to save the site that a user came from when they sign
I´m trying to sign some text or XML node using my certificates, installed on
I am trying to sign into my application. First I throw the RestartResponseAtInterceptPageException (this
I'm trying to implement a single-sign-on link from application written in JAVA, to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.