title: How to do Enveloping digital signature to a xml file in java?
body: <p>I have a requirement to do enveloping digital signature to a xml file using  java, currently i can able to sign with enveloped digital signature. Can any one help how to do enveloping signature?? My code for enveloped signature:  </p>
  
  <pre><code>//code   
  XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");       
  Transform exc14nTranform = fac.newTransform("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", (TransformParameterSpec) null);
      Transform envTransform = fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null);
  
      List&lt;Transform&gt; transformList = new ArrayList();
      transformList.add(exc14nTranform);
      transformList.add(envTransform);
  
      Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), transformList,null, null);
  
      SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,(C14NMethodParameterSpec) null),fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));
  
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream("C:\\kstore.jks"), "kspassword".toCharArray());
      KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry ("aliasname", new KeyStore.PasswordProtection("kspassword".toCharArray()));     
      X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
  
      KeyInfoFactory kif = fac.getKeyInfoFactory();
      List x509Content = new ArrayList();
  
      X509IssuerSerial issuer = kif.newX509IssuerSerial(cert.getIssuerDN().toString(), cert.getSerialNumber());
      System.out.println(cert.getSubjectAlternativeNames().toString());
      x509Content.add(cert.getSubjectX500Principal().getName());
      x509Content.add(issuer);
      x509Content.add(cert);
  
      X509Data xd = kif.newX509Data(x509Content);
      KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
  
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      Document doc = dbf.newDocumentBuilder().parse(new FileInputStream("C:\\myfile.xml"));
  
      DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement());
      XMLSignature signature = fac.newXMLSignature(si, ki);
      signature.sign(dsc);
  
      OutputStream os = new FileOutputStream("C:\\mysignedfile.xml");
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer trans = tf.newTransformer();
      trans.transform(new DOMSource(doc), new StreamResult(os));
  }
  }
  </code></pre>