2 yinghuashiqing yinghuashiqing 于 2014.03.30 20:14 提问

java实现XML的数字签名,但是老是提示签名算法不存在

这是写的关于XML的数字签名用java实现,我对于这个是小白,对于XML除了的遍历其他的都不太懂,错误老是提示签名算法不存在,我都要撞墙了,最近急需这个,拜托各位大大,帮帮忙我快哭死了

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm;
import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
import com.sun.org.apache.xml.internal.security.transforms.Transforms;
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;

public class pres03 {
public static void main(String[] args) {
try {
//声明密钥库格式
String keystoreType="JKS";
//声明密钥库存储文件路径及名称
String keystoreFile="src/xmlkey.jks";
//声明密钥库口令
String keystorePass="password";
//声明私钥别名
String privateKeyAlias="xmlkey";
//声明私钥口令
String privateKeyPass="password";
//声明证书别名
String certificateAlias="xmlkey";

        //获取私钥
        //获得KeyStore类的一个实例
        KeyStore ks=KeyStore.getInstance(keystoreType);
        //创建一个输入流,使用keystoreFile中的数据初始化KeyStore实例
        FileInputStream fis=new FileInputStream(keystoreFile);
        //加载密钥库文件
        ks.load(fis, keystorePass.toCharArray());
        //获取签名私钥
        PrivateKey privatekey=(PrivateKey)ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());

        //创建文件对象
        File file=new File("src/pre.xml");
        //构建文档对象工厂
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //构建文档对象
        DocumentBuilder db=dbf.newDocumentBuilder();
        //创建文件流转换为文档对象
        Document doc=db.parse(file);

        Element root=doc.getDocumentElement();

        //创建XML签名文件
        File signatureFile=new File("src/CI-certified.xml");

        //创建XML签名实例,指定的签名算法为DSA
        XMLSignature signature=new XMLSignature(doc, 
                signatureFile.toURL().toString(), 
                XMLSignature.ALGO_ID_SIGNATURE_DSA);
        //设置签名对象的ID
        String id1="100";
        signature.setId(id1);

        //创建转换处理对象
        Transforms transforms=new Transforms(doc);
        //指定签名类型为封内签名
        transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
        //计算摘要值并加入到签名对象中
        signature.addDocument("#"+id1, transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);

        //将签名者的数字证书存入KeyInfo元素中
        X509Certificate cert=(X509Certificate) ks.getCertificate(certificateAlias);
        signature.addKeyInfo(cert);
        //将签名者的公钥存入KeyInfo元素中
        signature.addKeyInfo(cert.getPublicKey());
        //将签名本身存入CI文档中,构造封内签名
        root.appendChild(signature.getElement());
        //使用签名者的私钥对signature对象签名
        signature.sign(privatekey);

        //输出签名后的XML文件
        //创建输出流
        FileOutputStream fos=new FileOutputStream(signatureFile);
        //输出XML签名文件
        XMLUtils.outputDOMc14nWithComments(doc, fos);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

然后一运行就出现了下面的错误

com.sun.org.apache.xml.internal.security.signature.XMLSignatureException: The requested algorithm http://www.w3.org/2000/09/xmldsig#dsa-sha1 does not exist. Original Message was: null
Original Exception was java.lang.NullPointerException
at com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.SignedInfo.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.XMLSignature.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.XMLSignature.(Unknown Source)
at XML_02.pres07.main(pres07.java:64)
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.SignedInfo.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.XMLSignature.(Unknown Source)
at com.sun.org.apache.xml.internal.security.signature.XMLSignature.(Unknown Source)
at XML_02.pres07.main(pres07.java:64)

错误中64行指的是XMLSignature signature=new XMLSignature(doc, 这句

下面是我要签名的XML文档
CSDN移动问答

还有就是那个什么秘钥和签名证书什么我也是看网上的生成的,也是DSA的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!