在使用Java签名的Xades BES中,无法将FirstCertificateSelector解析为一种类型

时间:2019-03-03 16:09:57

标签: java xml signing

我在Java的安全性问题上是新手。我有一个代码,但无法运行,因为有错误

  

FirstCertificateSelector无法解析为类型

     

DirectPasswordProvider无法解析为一种类型。

代码如下:

public class Main {

    private static final String CERT_FOLDER = "C:/Certs/";
    private static final String CERT        = "mycert.pfx";
    private static final String PASS        = "Password"; //the same in cert and keystorage

    private static final String UNSIGNED    = "C:/Test/sign-verify/unsigned.xml";
    private static final String SIGNED      = "C:/Test/sign-verify/signed-bes.xml";
    private static final String DOCUMENT    = "C:/Test/sign-verify/document.xml";

    public static void main(String[] args) throws Exception {
        System.out.println("______________________");
        System.out.println("\tSign");
        System.out.println("______________________");
        signBes();

        //System.out.println("______________________");
        //System.out.println("\tVerify");
        //System.out.println("______________________");
        //verifyBes();

    }

    private static void signBes() throws Exception {
        Document doc = DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .parse(new File(DOCUMENT));
        Element elem = doc.getDocumentElement();
        DOMHelper.useIdAsXmlId(elem);


        KeyingDataProvider kdp = new FileSystemKeyStoreKeyingDataProvider(
                "pkcs12",
                CERT_FOLDER + CERT,
                new FirstCertificateSelector(),
                new DirectPasswordProvider(PASS),
                new DirectPasswordProvider(PASS),
                true);
        DataObjectDesc obj = new DataObjectReference("#" + elem.getAttribute("Id"))
                .withTransform(new EnvelopedSignatureTransform());
        SignedDataObjects dataObjs = new SignedDataObjects().withSignedDataObject(obj);

        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);

        XadesSigner signer = new XadesBesSigningProfile(kdp).newSigner();
        signer.sign(dataObjs, elem);

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);        
        StreamResult result = new StreamResult(new File(SIGNED));
        transformer.transform(source, result);
    }
}

0 个答案:

没有答案