Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以最快的方式使用Java对XML进行签名_Java_Oracle11g - Fatal编程技术网

如何以最快的方式使用Java对XML进行签名

如何以最快的方式使用Java对XML进行签名,java,oracle11g,Java,Oracle11g,需要一些关于(优化)以及如何以最快的方式使用证书对XML进行签名的提示/建议。在某些数据库(Oracle)上,签名文件需要30毫秒。。但在另一个数据库上,它需要1.2秒。此函数/方法在循环中调用。它对一些XML数据进行签名,并以字符串形式返回签名的XML。(Java方法公开为PL/SQL函数) 首先要做的是通过将密钥相关对象存储在字段中来删除一些IO操作,因为读取密钥库以获取密钥需要时间。已将密钥库处理移到方法之外。现在,执行时间从1秒到800毫秒。。它的速度几乎是其他数据库的4倍。我已经调试了

需要一些关于(优化)以及如何以最快的方式使用证书对XML进行签名的提示/建议。在某些数据库(Oracle)上,签名文件需要30毫秒。。但在另一个数据库上,它需要1.2秒。此函数/方法在循环中调用。它对一些XML数据进行签名,并以字符串形式返回签名的XML。(Java方法公开为PL/SQL函数)


首先要做的是通过将密钥相关对象存储在字段中来删除一些IO操作,因为读取密钥库以获取密钥需要时间。

已将密钥库处理移到方法之外。现在,执行时间从1秒到800毫秒。。它的速度几乎是其他数据库的4倍。我已经调试了我方法中的每个语句,并注意到8xx毫秒与signature.sign(dsc)一起使用;更深入地说,您可以使用像jProfiler这样的探查器来了解时间花费在哪里。840毫秒的时间在signature.sign(dsc)中是“花费”的。“signature”是javax.xml.crypto.dsig.XMLSignature。我在我的机器上做了一个测试:signXML用了495ms(DocumentBuidlerFActory 148ms,XMLSignatureFactory.getInstance 94ms,Transformer.transform 50ms,XMLSignature.sign 46ms,KeyStore.load:35ms)
public static String signXML(String vhodniXml) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    Document doc =  dbFactory.newDocumentBuilder().parse(new ByteArrayInputStream(vhodniXml.getBytes()));

    NodeList nl = doc.getElementsByTagName("fu:BusinessPremiseRequest");
    Node node = nl.item(0);
    ((Element) node).setIdAttribute("Id", true);

    Enumeration e = p12.aliases();
    String alias = (String) e.nextElement();

    Key privateKey = p12.getKey(alias, geslo.toCharArray());

    KeyStore.PrivateKeyEntry keyEntry
            = (KeyStore.PrivateKeyEntry) p12.getEntry(alias, new KeyStore.PasswordProtection(geslo.toCharArray()));

    X509Certificate cert = (X509Certificate) keyEntry.getCertificate();

    PublicKey publicKey = cert.getPublicKey();
    final XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM");
    // Create a Reference to the enveloped document
    Reference ref = sigFactory.newReference("#data",
            sigFactory.newDigestMethod(DigestMethod.SHA256, null),
            Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
            null,
            null);

    SignedInfo si = sigFactory.newSignedInfo(sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), sigFactory.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null), Collections.singletonList(ref));

    // Create a KeyValue containing the RSA PublicKey
    KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
    X509IssuerSerial x509IssuerSerial = keyInfoFactory.newX509IssuerSerial(cert.getSubjectX500Principal().getName(), cert.getSerialNumber());

    List x509Content = new ArrayList();

    x509Content.add(cert.getSubjectX500Principal().getName());
    x509Content.add(x509IssuerSerial);

    KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey);
    X509Data xd = keyInfoFactory.newX509Data(x509Content);

    // Create a KeyInfo and add the KeyValue to it
    KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(xd));

    // Create a DOMSignContext and specify the RSA PrivateKey and
    // location of the resulting XMLSignature's parent element
    DOMSignContext dsc = new DOMSignContext(
            privateKey,
            node
    );

    // Create the XMLSignature (but don't sign it yet)
    XMLSignature signature = sigFactory.newXMLSignature(si, keyInfo);

    // Marshal, generate (and sign) the enveloped signature
    signature.sign(dsc);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Transformer trans = TransformerFactory.newInstance()
            .newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));

    return new String(os.toByteArray());
}