title: XML Digital Signature Java
body: <p>i need to digital sign my XML messages in JAVA:
  The resulting XML signature should have the following format:</p>
  
  <pre><code>&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  &lt;SignedInfo&gt;
  &lt;CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /&gt;
  &lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&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/2001/04/xmlenc#sha256" /&gt;
  &lt;DigestValue&gt;DsP5NLca+plhp9tZvGwykfb2whQYt3CQ5sbsVd9Q9aE=&lt;/DigestValue&gt;
  &lt;/Reference&gt;
  &lt;/SignedInfo&gt;
  &lt;SignatureValue&gt;
  LrfE0po3YPvVxB/m77iBWWiR07Ghiuhuj7tO2C2LKqZK2cLrAiidt+3tjbJ3m16quCFxfh7bmjRtJsGi7a3HKtK
  qY4auqrjNB62AtYrxvm+7Qd/cRacom4e3M9uF9JD1zTfoGun9w4WDfDrDaoZ+ZwUgNtf6sTYO5Ctcj5sYcD0=
  &lt;/SignatureValue&gt;
  &lt;KeyInfo&gt;
  &lt;KeyName&gt;7D665C81ABBE1A7D0E525BFC171F04D276F07BF2&lt;/KeyName&gt;
  &lt;/KeyInfo&gt;
  &lt;/Signature&gt;
  </code></pre>
  
  <p>Can anyone provide some help with code ??</p>
  
  <p>Edit:</p>
  
  <p>i came up with this code :</p>
  
  <pre><code>private static Document sign(Document doc) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
              NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException,
              FileNotFoundException, TransformerException {
  
          String providerName = System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
  
          XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());
  
          Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA256, null));
  
          // Create the SignedInfo
          SignedInfo si = fac.newSignedInfo(
                  fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null),
                  fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));
  
          KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
          kpg.initialize(512);
          KeyPair kp = kpg.generateKeyPair();
  
          KeyInfoFactory kif = fac.getKeyInfoFactory();
          KeyValue kv = kif.newKeyValue(kp.getPublic());
  
          KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
  
          DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement());
  
          XMLSignature signature = fac.newXMLSignature(si, ki);
          signature.sign(dsc);
  
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer trans = tf.newTransformer();
          trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream("mySignedFile")));
  
          return doc;
      }
  </code></pre>
  
  <p>but the Problem is i am getting :</p>
  
  <pre><code>&lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
  </code></pre>
  
  <p>and not :</p>
  
  <pre><code>&lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&gt;
  </code></pre>