B
bouchia.nazha
Hello
I want to sign a xml file using MD5withRSA. I saw a sample in this
forum :
public static void main(String unused[]) throws Exception {
String keystoreType = "JKS";
String keystoreFile =
"data/org/apache/xml/security/samples/input/keystore.jks";
String keystorePass = "xmlsecurity";
String privateKeyAlias = "test";
String privateKeyPass = "xmlsecurity";
String certificateAlias = "test";
// create DOM document
javax.xml.parsers.DocumentBuilderFactory dbf =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder db =
dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.newDocument();
// output file "signature.XML"
java.io.File signatureFile =
new File("signature.xml");
// URI
String BaseURI = signatureFile.toURL().toString();
// prefix
org.apache.xml.security.utils.Constants.setSignatureSpecNSprefix("ds");
// signature
org.apache.xml.security.signature.XMLSignature sig =
new XMLSignature(doc, BaseURI,
XMLSignature.ALGO_ID_SIGNATURE_DSA);
// add signature into document
doc.appendChild(sig.getElement());
// resolver
String xml_to_sign = http://localhost/sample.xml;
org.apache.xml.security.samples.signature.NullURIReferenceResolver
null_URI =
new NullURIReferenceResolver(xml_to_sign.getBytes());
// add resolver to signature
sig.addResourceResolver(null_URI);
// transformations i canonicalization C14n
org.apache.xml.security.transforms.Transforms transforms =
new Transforms(doc);
// make transformations
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
// add document "http://localhost/sample.xml" to signature
sig.addDocument(xml_to_sign, transforms,
Constants.ALGO_ID_DIGEST_SHA1);
// contener of certificates and keys
java.security.KeyStore ks = KeyStore.getInstance(keystoreType);
java.io.FileInputStream fis =
new FileInputStream(keystoreFile);
// initialization contener
ks.load(fis, keystorePass.toCharArray());
// certificate X509
java.security.cert.X509Certificate cert =
(X509Certificate) ks.getCertificate(certificateAlias);
// public key
java.security.PublicKey publicKey = cert.getPublicKey();
// private key
java.security.PrivateKey privateKey =
(PrivateKey) ks.getKey(privateKeyAlias,
privateKeyPass.toCharArray());
sig.addKeyInfo(cert);
sig.addKeyInfo(publicKey);
System.out.println("Start signing");
sig.sign(privateKey);
System.out.println("Finished signing");
// save into output file "signature.xml" DOM document
java.io.FileOutputStream fos =
new FileOutputStream(signatureFile);
XMLUtils.outputDOMc14nWithComments(doc, fos);
fos.close();
System.out.println("Wrote signature to " + BaseURI);
}
My question is why we use the classe Transform. It's necessary ?
Thanks for your help.
I want to sign a xml file using MD5withRSA. I saw a sample in this
forum :
public static void main(String unused[]) throws Exception {
String keystoreType = "JKS";
String keystoreFile =
"data/org/apache/xml/security/samples/input/keystore.jks";
String keystorePass = "xmlsecurity";
String privateKeyAlias = "test";
String privateKeyPass = "xmlsecurity";
String certificateAlias = "test";
// create DOM document
javax.xml.parsers.DocumentBuilderFactory dbf =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder db =
dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.newDocument();
// output file "signature.XML"
java.io.File signatureFile =
new File("signature.xml");
// URI
String BaseURI = signatureFile.toURL().toString();
// prefix
org.apache.xml.security.utils.Constants.setSignatureSpecNSprefix("ds");
// signature
org.apache.xml.security.signature.XMLSignature sig =
new XMLSignature(doc, BaseURI,
XMLSignature.ALGO_ID_SIGNATURE_DSA);
// add signature into document
doc.appendChild(sig.getElement());
// resolver
String xml_to_sign = http://localhost/sample.xml;
org.apache.xml.security.samples.signature.NullURIReferenceResolver
null_URI =
new NullURIReferenceResolver(xml_to_sign.getBytes());
// add resolver to signature
sig.addResourceResolver(null_URI);
// transformations i canonicalization C14n
org.apache.xml.security.transforms.Transforms transforms =
new Transforms(doc);
// make transformations
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
// add document "http://localhost/sample.xml" to signature
sig.addDocument(xml_to_sign, transforms,
Constants.ALGO_ID_DIGEST_SHA1);
// contener of certificates and keys
java.security.KeyStore ks = KeyStore.getInstance(keystoreType);
java.io.FileInputStream fis =
new FileInputStream(keystoreFile);
// initialization contener
ks.load(fis, keystorePass.toCharArray());
// certificate X509
java.security.cert.X509Certificate cert =
(X509Certificate) ks.getCertificate(certificateAlias);
// public key
java.security.PublicKey publicKey = cert.getPublicKey();
// private key
java.security.PrivateKey privateKey =
(PrivateKey) ks.getKey(privateKeyAlias,
privateKeyPass.toCharArray());
sig.addKeyInfo(cert);
sig.addKeyInfo(publicKey);
System.out.println("Start signing");
sig.sign(privateKey);
System.out.println("Finished signing");
// save into output file "signature.xml" DOM document
java.io.FileOutputStream fos =
new FileOutputStream(signatureFile);
XMLUtils.outputDOMc14nWithComments(doc, fos);
fos.close();
System.out.println("Wrote signature to " + BaseURI);
}
My question is why we use the classe Transform. It's necessary ?
Thanks for your help.