Java使用DSA密钥对生成XML签名的方法
程序员文章站
2024-03-03 18:55:28
本文实例讲述了java使用dsa密钥对生成xml签名的方法。分享给大家供大家参考。具体实现方法如下:
mainclass.java代码如下:
复制代码 代码如下:imp...
本文实例讲述了java使用dsa密钥对生成xml签名的方法。分享给大家供大家参考。具体实现方法如下:
mainclass.java代码如下:
复制代码 代码如下:
import java.security.keypair;
import java.security.keypairgenerator;
import java.security.securerandom;
import java.util.collections;
import javax.xml.crypto.dsig.canonicalizationmethod;
import javax.xml.crypto.dsig.digestmethod;
import javax.xml.crypto.dsig.reference;
import javax.xml.crypto.dsig.signaturemethod;
import javax.xml.crypto.dsig.signedinfo;
import javax.xml.crypto.dsig.xmlsignature;
import javax.xml.crypto.dsig.xmlsignaturefactory;
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;
public class mainclass {
public static void main(string[] args) throws exception {
keypairgenerator kpg = keypairgenerator.getinstance("dsa");
kpg.initialize(1024, new securerandom());
keypair dsakeypair = kpg.generatekeypair();
xmlsignaturefactory sigfactory = xmlsignaturefactory.getinstance();
reference ref = sigfactory.newreference("#body", sigfactory.newdigestmethod(digestmethod.sha1, null));
signedinfo signedinfo = sigfactory.newsignedinfo(sigfactory.newcanonicalizationmethod(
canonicalizationmethod.inclusive_with_comments, (c14nmethodparameterspec) null), sigfactory.newsignaturemethod(signaturemethod.dsa_sha1, null), collections.singletonlist(ref));
keyinfofactory kif = sigfactory.getkeyinfofactory();
keyvalue kv = kif.newkeyvalue(dsakeypair.getpublic());
keyinfo keyinfo = kif.newkeyinfo(collections.singletonlist(kv));
xmlsignature xmlsig = sigfactory.newxmlsignature(signedinfo, keyinfo);
}
}
import java.security.keypairgenerator;
import java.security.securerandom;
import java.util.collections;
import javax.xml.crypto.dsig.canonicalizationmethod;
import javax.xml.crypto.dsig.digestmethod;
import javax.xml.crypto.dsig.reference;
import javax.xml.crypto.dsig.signaturemethod;
import javax.xml.crypto.dsig.signedinfo;
import javax.xml.crypto.dsig.xmlsignature;
import javax.xml.crypto.dsig.xmlsignaturefactory;
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;
public class mainclass {
public static void main(string[] args) throws exception {
keypairgenerator kpg = keypairgenerator.getinstance("dsa");
kpg.initialize(1024, new securerandom());
keypair dsakeypair = kpg.generatekeypair();
xmlsignaturefactory sigfactory = xmlsignaturefactory.getinstance();
reference ref = sigfactory.newreference("#body", sigfactory.newdigestmethod(digestmethod.sha1, null));
signedinfo signedinfo = sigfactory.newsignedinfo(sigfactory.newcanonicalizationmethod(
canonicalizationmethod.inclusive_with_comments, (c14nmethodparameterspec) null), sigfactory.newsignaturemethod(signaturemethod.dsa_sha1, null), collections.singletonlist(ref));
keyinfofactory kif = sigfactory.getkeyinfofactory();
keyvalue kv = kif.newkeyvalue(dsakeypair.getpublic());
keyinfo keyinfo = kif.newkeyinfo(collections.singletonlist(kv));
xmlsignature xmlsig = sigfactory.newxmlsignature(signedinfo, keyinfo);
}
}
希望本文所述对大家的java程序设计有所帮助。