title: different digest value in xml in java
body: <pre><code>public void finalMethod(String originalXmlFilePath,String destnSignedXmlFilePath)
      throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException, InvalidAlgorithmParameterException, CertificateException, FileNotFoundException, IOException, SAXException, ParserConfigurationException, MarshalException, XMLSignatureException, TransformerException {
  
      // Create a DOM XMLSignatureFactory that will be used to
      // generate the enveloped signature.
      XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
  
      // Create a Reference to the enveloped document (in this case,
      // you are signing the whole document, so a URI of "" signifies
      // that, and also specify the SHA1 digest algorithm and
      // the ENVELOPED Transform.
      Reference ref = fac.newReference
       ("", fac.newDigestMethod(DigestMethod.SHA1, null),
        Collections.singletonList
         (fac.newTransform
          (Transform.ENVELOPED, (TransformParameterSpec) null)),
           null, null);
  
      // Create the SignedInfo.
      SignedInfo si = fac.newSignedInfo
       (fac.newCanonicalizationMethod
        (CanonicalizationMethod.INCLUSIVE,
         (C14NMethodParameterSpec) null),
          fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
           Collections.singletonList(ref));
  
  
  
  
      KeyStore p12 = KeyStore.getInstance("pkcs12");
      p12.load(new FileInputStream("C:/Users/prakhar.s/Downloads/JAVA DIGITAL/New folder (2)/TestDigCertificate/TestCertForLOBA.pfx"),"1234".toCharArray());
      Enumeration e = p12.aliases();
      String alias = (String) e.nextElement();
      System.out.println("Cert alias:" + alias);
      //Key privateKey = p12.getKey(alias, "1234".toCharArray());
      KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) p12.getEntry(alias, new KeyStore.PasswordProtection("1234".toCharArray()));
      //Certificate cert =  keyEntry.getCertificate(); 
  
  
  
  
  
      // Load the KeyStore and get the signing key and certificate.
     X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
  
  
      // Create the KeyInfo containing the X509Data.
      KeyInfoFactory kif = fac.getKeyInfoFactory();
  
  
  
      ArrayList x509Content = new ArrayList();
  
      //x509Content.add(cert.getSubjectX500Principal().getName());
  
  
  
      String dn = cert.getIssuerDN().toString();
      BigInteger sn = cert.getSerialNumber();
  
  
  
      BigInteger bigsn2 = new BigInteger (1, sn.toByteArray());
      System.out.println("new bigint (decimal): " + bigsn2);
      System.out.println("new bigint (hex): " + bigsn2.toString(16));
  
  
  
  
      X509IssuerSerial xd1 = kif.newX509IssuerSerial(dn, bigsn2);
  
      x509Content.add(xd1);
      x509Content.add(cert);
      X509Data xd = kif.newX509Data(x509Content);
  
      KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
  
      // Instantiate the document to be signed.
  
  
  
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      Document doc = dbf.newDocumentBuilder().parse
          (new FileInputStream(originalXmlFilePath));
  
  
  
  
  
      // Create a DOMSignContext and specify the RSA PrivateKey and
      // location of the resulting XMLSignature's parent element.
      DOMSignContext dsc = new DOMSignContext
          (keyEntry.getPrivateKey(), doc.getDocumentElement());
  
      // Create the XMLSignature, but don't sign it yet.
      XMLSignature signature = fac.newXMLSignature(si, ki);
  
      // Marshal, generate, and sign the enveloped signature.
      signature.sign(dsc);
  
  
  
  
  
  
  
  
  
  
  
  
  
      storeSignedDoc(doc, destnSignedXmlFilePath);
  
  
  }
  </code></pre>
  
  <p>Above is method that I am using to sign a xml file in Java. Below is the sample xml that have been signed in .net</p>
  
  <pre><code>      &lt;?xml version="1.0"?&gt;
  
  -&lt;BharatKoshPayment Version="1.0" DepartmentCode="020"&gt;
  
  
  -&lt;Submit&gt;
  
  
  -&lt;OrderBatch merchantBatchCode="e178eb86ce3dfa4b3f6a" Transactions="1" TotalAmount="1"&gt;
  
  
  -&lt;Order OrderCode="e178eb86ce3dfa4b3f6a" InstallationId="10011"&gt;
  
  
  -&lt;CartDetails&gt;
  
  &lt;Description/&gt;
  
  &lt;Amount value="1" exponent="0" CurrencyCode="INR"/&gt;
  
  &lt;OrderContent&gt;725&lt;/OrderContent&gt;
  
  &lt;PaymentTypeId&gt;294&lt;/PaymentTypeId&gt;
  
  &lt;PAOCode&gt;027973&lt;/PAOCode&gt;
  
  &lt;DDOCode&gt;227987&lt;/DDOCode&gt;
  
  &lt;/CartDetails&gt;
  
  
  -&lt;PaymentMethodMask&gt;
  
  &lt;Include Code="Online"/&gt;
  
  &lt;/PaymentMethodMask&gt;
  
  
  -&lt;Shopper&gt;
  
  &lt;ShopperEmailAddress&gt;prachiebooks@gmail.com&lt;/ShopperEmailAddress&gt;
  
  &lt;/Shopper&gt;
  
  
  -&lt;ShippingAddress&gt;
  
  
  -&lt;Address&gt;
  
  &lt;FirstName&gt;PRACHI INDIA Pvt.Ltd.&lt;/FirstName&gt;
  
  &lt;LastName/&gt;
  
  &lt;Address1&gt;INDER LOK DELHI-110035&lt;/Address1&gt;
  
  &lt;Address2/&gt;
  
  &lt;PostalCode&gt;110035&lt;/PostalCode&gt;
  
  &lt;City&gt;West Delhi&lt;/City&gt;
  
  &lt;StateRegion&gt;New Delhi&lt;/StateRegion&gt;
  
  &lt;State&gt;New Delhi&lt;/State&gt;
  
  &lt;CountryCode&gt;India&lt;/CountryCode&gt;
  
  &lt;MobileNumber&gt;7042112623&lt;/MobileNumber&gt;
  
  &lt;/Address&gt;
  
  &lt;/ShippingAddress&gt;
  
  
  -&lt;BillingAddress&gt;
  
  
  -&lt;Address&gt;
  
  &lt;FirstName&gt;PRACHI INDIA Pvt.Ltd.&lt;/FirstName&gt;
  
  &lt;LastName/&gt;
  
  &lt;Address1&gt;INDER LOK DELHI-110035&lt;/Address1&gt;
  
  &lt;Address2/&gt;
  
  &lt;PostalCode&gt;110035&lt;/PostalCode&gt;
  
  &lt;City&gt;West Delhi&lt;/City&gt;
  
  &lt;StateRegion&gt;New Delhi&lt;/StateRegion&gt;
  
  &lt;State&gt;New Delhi&lt;/State&gt;
  
  &lt;CountryCode&gt;India&lt;/CountryCode&gt;
  
  &lt;MobileNumber&gt;7042112623&lt;/MobileNumber&gt;
  
  &lt;/Address&gt;
  
  &lt;/BillingAddress&gt;
  
  &lt;StatementNarrative&gt;Publication Divisions Payment &lt;/StatementNarrative&gt;
  
  &lt;/Order&gt;
  
  &lt;/OrderBatch&gt;
  
  &lt;/Submit&gt;
  
  
  -&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  
  
  -&lt;SignedInfo&gt;
  
  &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  
  &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
  
  
  -&lt;Reference URI=""&gt;
  
  
  -&lt;Transforms&gt;
  
  &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
  
  &lt;/Transforms&gt;
  
  &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
  
  &lt;DigestValue&gt;QnaBNlOrLTHe9UMU2dyAJZ1DXFI=&lt;/DigestValue&gt;
  
  &lt;/Reference&gt;
  
  &lt;/SignedInfo&gt;
  
  &lt;SignatureValue&gt;QCBsnlKP1udt1eAtdk1b1VxP/bq4wnKpnEblvbSGPk4lJKNkC+TjfUbYdKSDtypLdCIldQox/x3jCmNJZVxRqtPlSYyeC/xOlrbA2ya5svGtH9fw7+wkHyzcGPtjoJKGhHTrfTDbMVMNxCe6Ph+OZEZuTm8LLWAzl9UcMEeZAUk=&lt;/SignatureValue&gt;
  
  
  -&lt;KeyInfo&gt;
  
  
  -&lt;X509Data&gt;
  
  
  -&lt;X509IssuerSerial&gt;
  
  &lt;X509IssuerName&gt;CN=TestCertForLOBA&lt;/X509IssuerName&gt;
  
  &lt;X509SerialNumber&gt;180518423413430116130326665604242149948&lt;/X509SerialNumber&gt;
  
  &lt;/X509IssuerSerial&gt;
  
  &lt;X509Certificate&gt;MIICFTCCAYKgAwIBAgIQh86VbnKj77tMYlZ5PHkSPDAJBgUrDgMCHQUAMBoxGDAWBgNVBAMTD1Rlc3RDZXJ0Rm9yTE9CQTAeFw0xNTEyMzExODMwMDBaFw0xNzAzMzAxODMwMDBaMBoxGDAWBgNVBAMTD1Rlc3RDZXJ0Rm9yTE9CQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqH1D3ycqoUs1N/+pr+xCXtrAhwJWf4XHXM/tNtvuJ/BLfwH8C4kji9TfZz/4PLvF/j5DJO22Pmv2iPYeKGWmrq0y7b8O++3zTcgO800/gFdM8GztW1E+FUwRq5A7D0UXlxUs7ePib0K4zWOyYOMlf28NYzFD5/qTnYBYjymfHkcCAwEAAaNkMGIwEwYDVR0lBAwwCgYIKwYBBQUHAwEwSwYDVR0BBEQwQoAQ3Nw37mdOlicLkYm1Kw9DTqEcMBoxGDAWBgNVBAMTD1Rlc3RDZXJ0Rm9yTE9CQYIQh86VbnKj77tMYlZ5PHkSPDAJBgUrDgMCHQUAA4GBAGwurrSEfqVIGnsTyXF5TRbdqEbsSazoFhTWlLLMupPAc2Xmvp5lmMH76TYG/Zsx9wsZK5cjP/GNBvKLXPtfkElQHFztHotoU29Y9QqNIx5xBX02CJpuDeWm27OjPI39JbgfPBNFyoRIZIBNcQP2tUXd8JYh/FxXOj/xhfY2OlKp&lt;/X509Certificate&gt;
  
  &lt;/X509Data&gt;
  
  &lt;/KeyInfo&gt;
  
  &lt;/Signature&gt;
  
  &lt;/BharatKoshPayment&gt;
  </code></pre>
  
  <p>Below is my Java generated signed xml. </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  -&lt;BharatKoshPayment Version="1.0" DepartmentCode="020"&gt;
  
  
  -&lt;Submit&gt;
  
  
  -&lt;OrderBatch merchantBatchCode="e178eb86ce3dda4b3f6a" Transactions="1" TotalAmount="1"&gt;
  
  
  -&lt;Order OrderCode="e178eb86ce3dda4b3f6a" InstallationId="10011"&gt;
  
  
  -&lt;CartDetails&gt;
  
  &lt;Description/&gt;
  
  &lt;Amount value="1" exponent="0" CurrencyCode="INR"/&gt;
  
  &lt;OrderContent&gt;725&lt;/OrderContent&gt;
  
  &lt;PaymentTypeId&gt;294&lt;/PaymentTypeId&gt;
  
  &lt;PAOCode&gt;027973&lt;/PAOCode&gt;
  
  &lt;DDOCode&gt;227987&lt;/DDOCode&gt;
  
  &lt;/CartDetails&gt;
  
  
  -&lt;PaymentMethodMask&gt;
  
  &lt;Include Code="Online"/&gt;
  
  &lt;/PaymentMethodMask&gt;
  
  
  -&lt;Shopper&gt;
  
  &lt;ShopperEmailAddress&gt;prachiebooks@gmail.com&lt;/ShopperEmailAddress&gt;
  
  &lt;/Shopper&gt;
  
  
  -&lt;ShippingAddress&gt;
  
  
  -&lt;Address&gt;
  
  &lt;FirstName&gt;PRACHI INDIA Pvt.Ltd.&lt;/FirstName&gt;
  
  &lt;LastName/&gt;
  
  &lt;Address1&gt;INDER LOK DELHI-110035&lt;/Address1&gt;
  
  &lt;Address2/&gt;
  
  &lt;PostalCode&gt;110035&lt;/PostalCode&gt;
  
  &lt;City&gt;West Delhi&lt;/City&gt;
  
  &lt;StateRegion&gt;New Delhi&lt;/StateRegion&gt;
  
  &lt;State&gt;New Delhi&lt;/State&gt;
  
  &lt;CountryCode&gt;India&lt;/CountryCode&gt;
  
  &lt;MobileNumber&gt;7042112623&lt;/MobileNumber&gt;
  
  &lt;/Address&gt;
  
  &lt;/ShippingAddress&gt;
  
  
  -&lt;BillingAddress&gt;
  
  
  -&lt;Address&gt;
  
  &lt;FirstName&gt;PRACHI INDIA Pvt.Ltd.&lt;/FirstName&gt;
  
  &lt;LastName/&gt;
  
  &lt;Address1&gt;INDER LOK DELHI-110035&lt;/Address1&gt;
  
  &lt;Address2/&gt;
  
  &lt;PostalCode&gt;110035&lt;/PostalCode&gt;
  
  &lt;City&gt;West Delhi&lt;/City&gt;
  
  &lt;StateRegion&gt;New Delhi&lt;/StateRegion&gt;
  
  &lt;State&gt;New Delhi&lt;/State&gt;
  
  &lt;CountryCode&gt;India&lt;/CountryCode&gt;
  
  &lt;MobileNumber&gt;7042112623&lt;/MobileNumber&gt;
  
  &lt;/Address&gt;
  
  &lt;/BillingAddress&gt;
  
  &lt;StatementNarrative&gt;Publication Divisions Payment &lt;/StatementNarrative&gt;
  
  &lt;/Order&gt;
  
  &lt;/OrderBatch&gt;
  
  &lt;/Submit&gt;
  
  
  -&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  
  
  -&lt;SignedInfo&gt;
  
  &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  
  &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
  
  
  -&lt;Reference URI=""&gt;
  
  
  -&lt;Transforms&gt;
  
  &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
  
  &lt;/Transforms&gt;
  
  &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
  
  &lt;DigestValue&gt;2/bkZpFjH4VQZXSIfugosld9T9I=&lt;/DigestValue&gt;
  
  &lt;/Reference&gt;
  
  &lt;/SignedInfo&gt;
  
  &lt;SignatureValue&gt;PVFjdC3dwhNUGa3GBKQTkcpUbdUkdNvzA3S3pLT9S0rNsyDAVcY2qW76+qaXn7aFkPBIyQlg2ikU qlizmo2WaAgZlMkBLcPdpp/wzXBUih10yyxgI9+ExuNZ3z1DqOoEZHVT6/IJlcz2iKyutSvl7a+1 P6dYO0ndT2j9izACbV0=&lt;/SignatureValue&gt;
  
  
  -&lt;KeyInfo&gt;
  
  
  -&lt;X509Data&gt;
  
  
  -&lt;X509IssuerSerial&gt;
  
  &lt;X509IssuerName&gt;CN=TestCertForLOBA&lt;/X509IssuerName&gt;
  
  &lt;X509SerialNumber&gt;180518423413430116130326665604242149948&lt;/X509SerialNumber&gt;
  
  &lt;/X509IssuerSerial&gt;
  
  &lt;X509Certificate&gt;MIICFTCCAYKgAwIBAgIQh86VbnKj77tMYlZ5PHkSPDAJBgUrDgMCHQUAMBoxGDAWBgNVBAMTD1Rl c3RDZXJ0Rm9yTE9CQTAeFw0xNTEyMzExODMwMDBaFw0xNzAzMzAxODMwMDBaMBoxGDAWBgNVBAMT D1Rlc3RDZXJ0Rm9yTE9CQTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqH1D3ycqoUs1N/+p r+xCXtrAhwJWf4XHXM/tNtvuJ/BLfwH8C4kji9TfZz/4PLvF/j5DJO22Pmv2iPYeKGWmrq0y7b8O ++3zTcgO800/gFdM8GztW1E+FUwRq5A7D0UXlxUs7ePib0K4zWOyYOMlf28NYzFD5/qTnYBYjymf HkcCAwEAAaNkMGIwEwYDVR0lBAwwCgYIKwYBBQUHAwEwSwYDVR0BBEQwQoAQ3Nw37mdOlicLkYm1 Kw9DTqEcMBoxGDAWBgNVBAMTD1Rlc3RDZXJ0Rm9yTE9CQYIQh86VbnKj77tMYlZ5PHkSPDAJBgUr DgMCHQUAA4GBAGwurrSEfqVIGnsTyXF5TRbdqEbsSazoFhTWlLLMupPAc2Xmvp5lmMH76TYG/Zsx 9wsZK5cjP/GNBvKLXPtfkElQHFztHotoU29Y9QqNIx5xBX02CJpuDeWm27OjPI39JbgfPBNFyoRI ZIBNcQP2tUXd8JYh/FxXOj/xhfY2OlKp&lt;/X509Certificate&gt;
  
  &lt;/X509Data&gt;
  
  &lt;/KeyInfo&gt;
  
  &lt;/Signature&gt;
  
  &lt;/BharatKoshPayment&gt;
  </code></pre>
  
  <p>My problem is , I am getting mismatch digest value in java.</p>
  
  <p>I also have .net code for generating signed xml </p>
  
  <pre><code>public XmlDocument signedFun(XmlDocument xmlDoc)
  {
      string CertificateKey = ConfigurationManager.AppSettings["CDSCOHealthKey"].ToString();
      X509Store certificate = new X509Store(StoreLocation.CurrentUser);
  
      if (ConfigurationManager.AppSettings["isLocalPdfSign"] != null &amp;&amp; ConfigurationManager.AppSettings["isLocalPdfSign"].ToString() == "True")
      {
          certificate = new X509Store(StoreLocation.LocalMachine);
      }
      certificate.Open(OpenFlags.ReadOnly);
      RSACryptoServiceProvider rsaKey = null;
      X509Certificate2 certx = new X509Certificate2();
      foreach (X509Certificate2 cert in certificate.Certificates)
      {
          if (cert.Subject.Contains(CertificateKey))
          {
              // retrieve private key                  
              rsaKey = (RSACryptoServiceProvider)cert.PrivateKey;
              certx = cert;
              break;
          }
      }
  
  
      if (rsaKey == null)
      {
          throw new Exception("Valid certificate was not found");
  
      }
  
      // Sign the XML document. 
      if (ConfigurationManager.AppSettings["IsNewsXMLSign"] == "True")
      {
          SignXml(xmlDoc, rsaKey, certx);
      }
      return xmlDoc;
  }
  public static void SignXml(XmlDocument xmlDoc, RSA Key, X509Certificate2 Cert)
  {
      // Check arguments. 
      if (xmlDoc == null)
          throw new ArgumentException("xmlDoc");
      if (Key == null)
          throw new ArgumentException("Key");
  
      // Create a SignedXml object.
      System.Security.Cryptography.Xml.SignedXml signedXml = new System.Security.Cryptography.Xml.SignedXml(xmlDoc);
      // SignedXml signedXml = new SignedXml(xmlDoc);
  
      // Add the key to the SignedXml document.
      signedXml.SigningKey = Key;
      // *** Create a KeyInfo structure
      KeyInfo keyInfo = new KeyInfo();
      // *** Specifically use the issuer and serial number for the data rather than the default
      KeyInfoX509Data keyInfoData = new KeyInfoX509Data();
      keyInfoData.AddIssuerSerial(Cert.Issuer, Cert.GetSerialNumberString());
      keyInfoData.AddCertificate(Cert);
  
      keyInfo.AddClause(keyInfoData);
  
  
      // *** provide the certficate info that gets embedded - note this is only
      // *** for specific formatting of the message to provide the cert info
      signedXml.KeyInfo = keyInfo;
  
      // Create a reference to be signed.
      Reference reference = new Reference();
      reference.Uri = "";
  
      // Add an enveloped transformation to the reference.
      XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
      reference.AddTransform(env);
  
      // Add the reference to the SignedXml object.
      signedXml.AddReference(reference);
  
      // Compute the signature.
      signedXml.ComputeSignature();
  
      // Get the XML representation of the signature and save 
      // it to an XmlElement object.
      XmlElement xmlDigitalSignature = signedXml.GetXml();
  
      // Append the element to the XML document.
      xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
  
  }
  </code></pre>
  
  <p>Any One have solution ??</p>