【问题标题】:is there a good example out there for XML Sign with XADES-EPES in Java?在 Java 中使用 XADES-EPES 进行 XML 签名有一个很好的例子吗?
【发布时间】:2016-12-05 00:22:08
【问题描述】:

我尝试使用 xades4j 但文档有点笼统。 此刻我有一个基本的签名方法,但没有得到我需要的 xml 标签。

KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);

    ks.load(new FileInputStream(KEYSTORE_FILE), KEYSTORE_PASSWORD.toCharArray());

    PrivateKey privateKey = (PrivateKey) ks.getKey(PRIVATE_KEY_ALIAS, PRIVATE_KEY_PASSWORD.toCharArray());

    File signatureFile = new File("./invoice.xml");

    String  baseURI = signatureFile.toURL().toString(); // BaseURI para las URL Relativas.

    // Instanciamos un objeto XMLSignature desde el Document. El algoritmo de firma será DSA
    // Signature - Required DSAwithSHA1 (DSS)
    XMLSignature xmlSignature = new XMLSignature(document, baseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);

    // Añadimos el nodo de la firma a la raiz antes de firmar.
    // Observe que ambos elementos pueden ser mezclados en una forma con referencias separadas
    document.getDocumentElement().appendChild(xmlSignature.getElement());

    // Creamos el objeto que mapea: Document/Reference
    Transforms transforms = new Transforms(document);
    transforms.addTransform(Transforms.TRANSFORM_BASE64_DECODE); // TRANSFORM_ENVELOPED_SIGNATURE

    // Añadimos lo anterior Documento / Referencia
    // ALGO_ID_DIGEST_SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1";
    xmlSignature.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);

    // Añadimos el KeyInfo del certificado cuya clave privada usamos
    X509Certificate cert = (X509Certificate) ks.getCertificate(PRIVATE_KEY_ALIAS);
    xmlSignature.addKeyInfo(cert);
    xmlSignature.addKeyInfo(cert.getPublicKey());

    // Realizamos la firma
    xmlSignature.sign(privateKey);

【问题讨论】:

    标签: java xml-signature xades4j


    【解决方案1】:

    【讨论】:

    • @mposadar 您显示的代码似乎只使用了 Apache Santuario,它是 xades4j 的底层库。上面共享的链接和github.com/luisgoncalves/xades4j/wiki 应该可以帮助您入门。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多