有没有更简单的方法来在Java中签署XML文档?

我正在尝试使用 Java 对 XML 文档进行数字签名。我有一个实现,使用我发现的一些参考,这些参考在javax.xml.crypto.dsig包中使用各种实现。

但是,我当前的实现就像我看过的许多示例一样 - 它相当冗长,并且涉及使用来自 、和包等的不少于 23 个不同的 API 类。感觉就像我进入了工厂工厂的工厂土地,我花了几个小时才弄清楚发生了什么。java.xml.crypto.dsigjavax.xml.transformjava.security

我的问题是,有没有更简单的方法可以做到这一点?如果我有公钥/私钥文件,并且我想向XML文档添加一个,那么是否有一个库可以让我调用如下内容:<Signature/>

OutputStream signFile(InputStream xmlFile, File privateKey)

...没有所有的XMLSignatureFactory/CanonicalizationMethod/DOMSignContext疯狂?

我不是很精通密码学,Java提供的API对于像我这样试图熟悉数字签名的开发人员来说似乎相当令人生畏。如果所有这些都是必要的,或者目前没有更友好的API,那很好,我愿意接受这个答案。我只想知道我是否不必要地在这里走上了艰难的道路。


答案 1

看看Apache XML Security。要使用该包生成和验证签名,请查看src_samples/org/apache/xml/security/samples/signature/ 中的示例


答案 2

从Apache Santuario的例子构建,我能想到的最短的东西就是这个。没有及其伴随,它是20行CreateSignaturemain()output()

import java.io.*;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.io.IOUtils;
import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.transforms.Transforms;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.ElementProxy;
import org.w3c.dom.Document;

public class CreateSignature {

    private static final String PRIVATE_KEY_ALIAS = "test-alias";
    private static final String PRIVATE_KEY_PASS = "test";
    private static final String KEY_STORE_PASS = "test";
    private static final String KEY_STORE_TYPE = "JKS";

    public static void main(String... unused) throws Exception {
        final InputStream fileInputStream = new FileInputStream("test.xml");
        try {
            output(signFile(fileInputStream, new File("keystore.jks")), "signed-test.xml");
        }
        finally {
            IOUtils.closeQuietly(fileInputStream);
        }
    }

    public static ByteArrayOutputStream signFile(InputStream xmlFile, File privateKeyFile) throws Exception {
        final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
        Init.init();
        ElementProxy.setDefaultPrefix(Constants.SignatureSpecNS, "");
        final KeyStore keyStore = loadKeyStore(privateKeyFile);
        final XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_RSA);
        final Transforms transforms = new Transforms(doc);
        transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
        sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
        final Key privateKey = keyStore.getKey(PRIVATE_KEY_ALIAS, PRIVATE_KEY_PASS.toCharArray());
        final X509Certificate cert = (X509Certificate)keyStore.getCertificate(PRIVATE_KEY_ALIAS);
        sig.addKeyInfo(cert);
        sig.addKeyInfo(cert.getPublicKey());
        sig.sign(privateKey);
        doc.getDocumentElement().appendChild(sig.getElement());
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(doc));
        return outputStream;
    }

    private static KeyStore loadKeyStore(File privateKeyFile) throws Exception {
        final InputStream fileInputStream = new FileInputStream(privateKeyFile);
        try {
            final KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
            keyStore.load(fileInputStream, KEY_STORE_PASS.toCharArray());
            return keyStore;
        }
        finally {
            IOUtils.closeQuietly(fileInputStream);
        }
    }

    private static void output(ByteArrayOutputStream signedOutputStream, String fileName) throws IOException {
        final OutputStream fileOutputStream = new FileOutputStream(fileName);
        try {
            fileOutputStream.write(signedOutputStream.toByteArray());
            fileOutputStream.flush();
        }
        finally {
            IOUtils.closeQuietly(fileOutputStream);
        }
    }
}

推荐