gpt4 book ai didi

java - 如何从 Xades4J 签名中的签名元素中删除 namespace ?

转载 作者:行者123 更新时间:2023-12-01 18:01:17 24 4
gpt4 key购买 nike

我有以下代码,它将 content.xml 导入到 document.xml 中并签署 document.xml 根元素。

try {
KeyingDataProvider kp = new FileSystemKeyStoreKeyingDataProvider(
"pkcs12",
"C:\\workspace\\tests\\my\\LG.pfx",
new FirstCertificateSelector(),
new DirectPasswordProvider("mykeypass"),
new DirectPasswordProvider("mykeypass"),
true);
XadesSigningProfile p = new XadesBesSigningProfile(kp);
XadesSigner signer = p.newSigner();

javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();


Document doc1 = builder.parse(new File("C:\\workspace\\tests\\document.xml"));
Document doc2 = builder.parse(new File("C:\\workspace\\tests\\content.xml"));

Node contentElement = doc2.getDocumentElement();
Node parentElement = doc1.getDocumentElement();
Node adoptedContentElement = doc1.adoptNode(contentElement);
parentElement.appendChild(adoptedContentElement);

Node nodeToSign = doc1.getDocumentElement().getFirstChild();
Node nodeToAttachSignature = doc1.getDocumentElement();

IndividualDataObjsTimeStampProperty dataObjsTimeStamp = new IndividualDataObjsTimeStampProperty();
AllDataObjsCommitmentTypeProperty globalCommitment = AllDataObjsCommitmentTypeProperty.proofOfApproval();
CommitmentTypeProperty commitment = CommitmentTypeProperty.proofOfCreation();

DataObjectDesc obj = new EnvelopedXmlObject(nodeToSign, "text/xml", null);
SignedDataObjects dataObjs = new SignedDataObjects(obj).withCommitmentType(globalCommitment);

signer.sign(dataObjs, nodeToAttachSignature);

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("C:\\workspace\\tests\\signedDocument.xml"));
Source input = new DOMSource(doc1);
transformer.transform(input, output);


} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (XadesProfileResolutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XAdES4jException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

产生如下签名:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-26102a68-cfea-43fd-a40e-9682ae7da4a1">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>

问题是 - 如何从签名元素中删除 namespace ds:

最佳答案

我看到在名为 CreateSignature 的示例中设置了前缀“ds”的行,用于 Java 中的 Apache Santuario 库。

ElementProxy.setDefaultPrefix(Constants.SignatureSpecNS, "ds");

在库开发者写的书中,他指定了如何定义签名,因为XAdES4j主要是构建签名而不是整个文档:

5.2 Signature Production: as previously discussed, a signature can be seen as having two parts: the first consists of the characteristics of the signer and the signature operation itself; the second, the resources being signed. If the signer is seen as a regular signature producer, he’s likely to have a set of characteristics that are used whenever a signature is created, i.e a signature profile. These characteristics are fixed between signatures, while the signed resources vary. Thus, producing a signature is to combine a profile and a set of resources in order to create the final XML structure. This process comes down to three major tasks: gather the needed information (signature and data objects properties, algorithms, keying data) in appropriate order; create the core signature structure using the Apache XML Security API; and create the qualifying properties DOM tree to be appended to the signature. Note that Apache XML Security creates the DOM tree for the core signature structure. However, the XAdES elements are unknown to the Apache API, which means that the last task has to be completely supported by the library.

引用自:XAdES4j — XAdES 签名服务的 Java 库。作者:路易斯·菲利佩·多斯桑托斯·贡萨尔维斯

关于java - 如何从 Xades4J 签名中的签名元素中删除 namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com