Invalid Signature SHA1WithRSA

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Invalid Signature SHA1WithRSA

daniel.felgar
This post has NOT been accepted by the mailing list yet.
I'm migrating my code to Apache Santuário, but when I check the signature is invalid!

This is my xml:
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
        <infNFe Id="NFe35160665019499000124650800000000411000000414" versao="3.10">
                <ide>
                        <cUF>35</cUF>
                        <cNF>00000041</cNF>
                        <natOp>0</natOp>
                </ide>
        </infNFe>
</NFe>

This is my code:
                Init.init();
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(true);
                Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
                Element ele = (Element) doc.getElementsByTagName("infNFe").item(0);
                ele.setIdAttribute("Id", true);

                ElementProxy.setDefaultPrefix(Constants.SignatureSpecNS, "");
                final XMLSignature sig = new XMLSignature(doc, "NFe35160665019499000124650800000000411000000414",
                                XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1, Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);


                final Transforms transforms = new Transforms(doc);
                transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
                transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
                sig.addDocument("#NFe35160665019499000124650800000000411000000414", transforms, Constants.ALGO_ID_DIGEST_SHA1);
               
                final Key privateKey = certi.getPrivateKey();
                final X509Certificate cert = certi.getCertificate();
                sig.addKeyInfo(cert);
                sig.sign(privateKey);

                doc.getDocumentElement().appendChild(sig.getElement());

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                XMLUtils.outputDOMc14nWithComments(doc, os);
                String xmlFinal = new String(os.toByteArray());
                System.out.println(xmlFinal);

When i check the signature , it's invalid!!

Loading...