扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱先忠编译 来源:天极开发 2007年10月13日
关键字:
在本页阅读全文(共6页)
public class Sign { public static void main(String[] args) throws Exception { String input = "./etc/invoice.xml "; String output = "./etc/signature.xml"; if (args.length > 2) { input = args[0]; output = args[1]; } //准备 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); //步骤1 String providerName = System.getProperty("jsr105Provider","org.jcp.XML.dsig.internal.dom.XMLDSigRI"); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",(Provider) Class.forName(providerName).newInstance()); //步骤2 Reference ref = fac.newReference("#invoice",fac.newDigestMethod(DigestMethod.SHA1, null)); //步骤3 Document XML = dbf.newDocumentBuilder().parse(new File(input)); Node invoice = XML.getDocumentElement(); XMLStructure content = new DOMStructure(invoice); XMLObject obj = fac.newXMLObject(Collections.singletonList(content),"invoice", null, null); //步骤4 SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), Collections.singletonList(ref)); //步骤5,分为情形5.0或5.1 PrivateKey privateKey = null; //情形5.0 privateKey = KeyStoreInfo.getPrivateKey("./etc/bizkeystore","sp1234","kp1234", "biz"); //情形5.1,分为情形5.1.1或5.1.2 //情形5.1.1 //KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); //kpg.initialize(512); //KeyPair kp = kpg.generateKeyPair(); //情形5.1.2 // KeyPair kp = KeyStoreInfo.getKeyPair("./etc/bizkeystore", "sp1234", // "kp1234","biz"); //如果针对情形5.1,请去掉下面一行中的注释 // privateKey = kp.getPrivate(); //步骤6,分为情形6.0,6.1或6.2 //情形6.0,如果针对情形6.1或6.2也使用下面这一行 KeyInfo ki = null; //如果针对情形6.1或6.2请去掉下面一行中的注释 // KeyInfoFactory kif = fac.getKeyInfoFactory(); //情形6.1 // KeyValue kv = kif.newKeyValue(kp.getPublic()); // ki = kif.newKeyInfo(Collections.singletonList(kv)); //情形6.2 // CertificateFactory cf = CertificateFactory.getInstance("X.509"); // FileInputStream fis = new FileInputStream("./etc/biz.cer"); // java.security.cert.Certificate cert = cf.generateCertificate(fis); // fis.close(); // X509Data x509d = kif.newX509Data(Collections.singletonList(cert)); // ki = kif.newKeyInfo(Collections.singletonList(x509d)); //步骤7 XMLSignature signature = fac.newXMLSignature(si, ki,Collections.singletonList(obj), null, null); //步骤8 Document doc = dbf.newDocumentBuilder().newDocument(); DOMSignContext dsc = new DOMSignContext(privateKey, doc); //步骤9 signature.sign(dsc); //转换成一个xml文档 TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(new DOMSource(doc),new StreamResult(new FileOutputStream(output))); } } |
public abstract DigestMethod newDigestMethod(String algorithm, DigestMethodParameterSpec params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException |
public abstract Reference newReference(String uri, DigestMethod dm); public abstract Reference newReference(String uri, DigestMethod dm, List transforms, String type, String id); public abstract Reference newReference(String uri, DigestMethod dm, List transforms, String type, String id, byte[] digestValue); ...... |
<element name="Reference" type="ds:ReferenceType"/> <complexType name="ReferenceType"> <sequence> <element ref="ds:Transforms" minOccurs="0"/> <element ref="ds:DigestMethod"/> <element ref="ds:DigestValue"/> </sequence> <attribute name="Id" type="ID" use="optional"/> <attribute name="URI" type="anyURI" use="optional"/> <attribute name="Type" type="anyURI" use="optional"/> </complexType> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者