使用Java的XAdES XML签名(不使用任何第三方jar,如XAdES4j)

时间:2018-05-03 14:22:38

标签: java digital-signature xml-signature

我们需要使用CAdES / XAdES-T签署XML。 我们使用SAP PI / PO中间件,这是一个基于Java的系统。我能够签署XML但最简单的。我无法达到CAdES / XAdES-T。在互联网上,我发现有一个XADES4j解决方案,但我们不想在这里使用任何第三方解决方案。

以下是XAdES XMl签名的XSD http://uri.etsi.org/01903/v1.4.1/

这实际上是我到目前为止所尝试的:

package xml_signature;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;

import javax.xml.crypto.MarshalException;
import javax.xml.crypto.dsig.CanonicalizationMethod;
import javax.xml.crypto.dsig.DigestMethod;
import javax.xml.crypto.dsig.Reference;
import javax.xml.crypto.dsig.SignedInfo;
import javax.xml.crypto.dsig.Transform;
import javax.xml.crypto.dsig.XMLSignature;
import javax.xml.crypto.dsig.XMLSignatureException;
import javax.xml.crypto.dsig.XMLSignatureFactory;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Test {
    public static void main(String args[]) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, MarshalException, XMLSignatureException, TransformerException, KeyException{
        DocumentBuilderFactory dbf =
                  DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true); 
        DocumentBuilder builder = dbf.newDocumentBuilder();  
        Document doc = builder.parse(new FileInputStream("Output.xml"));


        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.generateKeyPair();


        DOMSignContext dsc = new DOMSignContext
                  (kp.getPrivate(), doc.getDocumentElement());


        XMLSignatureFactory fac = 
                  XMLSignatureFactory.getInstance("DOM");

        Reference ref = fac.newReference
                  ("", fac.newDigestMethod(DigestMethod.SHA256, null),
                    Collections.singletonList
                      (fac.newTransform(Transform.ENVELOPED,
                        (TransformParameterSpec) null)), null, null); 


        SignedInfo si = fac.newSignedInfo
                  (fac.newCanonicalizationMethod
                    (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                      (C14NMethodParameterSpec) null),
                    //fac.newSignatureMethod("http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd", null),
                    fac.newSignatureMethod("http://www.w3.org/2009/xmldsig11#dsa-sha256", null),
                    Collections.singletonList(ref));


        KeyInfoFactory kif = fac.getKeyInfoFactory(); 
        KeyValue kv = kif.newKeyValue(kp.getPublic());
        KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));

        XMLSignature signature = fac.newXMLSignature(si, ki); 

        signature.sign(dsc);

        OutputStream os;
        if (args.length > 1) {
          os = new FileOutputStream(args[1]);
        } else {
          os = System.out;
        } 
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.transform(new DOMSource(doc), new StreamResult(os)); 
    }
}

1 个答案:

答案 0 :(得分:0)