我正在尝试使用 XADES4j 签署一个 xml 文件。我还使用智能卡认证(将从 windows 商店加载)。
有没有符合我搜索的示例,因为我是 XML 签名和智能卡的新手。我搜索了几个星期的修复程序但没有成功。
还有其他例子,但不是很清楚:
我在https://github.com/luisgoncalves/xades4j/wiki/DefiningKeyingData找到了这个演示,但我不知道如何设置函数PKCS11KeyStoreKeyingDataProvider
以应用 Windows 证书参数及其 pin 码:
KeyingDataProvider kp = new PKCS11KeyStoreKeyingDataProvider(
"path/to/native/lib",
"MS SABRI", // CERTIFICATE NAME
new FirstCertificateSelector(),
null,
null,false);,
我的代码:
try {
// >>> TEST N°1
// KeyingDataProvider kp = new DirectKeyingDataProvider((X509Certificate) certExemple, PrivateKEY);
// >>> TEST N°2
KeyingDataProvider kp = new PKCS11KeyStoreKeyingDataProvider(
"path/to/native/lib",
"name", // CERTIFICATE NAME
new FirstCertificateSelector(),
new DirectPasswordProvider("123456"), // PIN CODE
new DirectPasswordProvider("123456"), // PIN CODE
false);
// XADES
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();
// XML FILE TO BE SIGNED
Document doc1 = builder.parse(new File("FileNotSigned.xml"));
// NODE
Node parentElement = doc1.getDocumentElement();
Node nodeToSign = doc1.getDocumentElement().getFirstChild();
Node nodeToAttachSignature = doc1.getDocumentElement();
IndividualDataObjsTimeStampProperty dataObjsTimeStamp = new IndividualDataObjsTimeStampProperty();
AllDataObjsCommitmentTypeProperty globalCommitment = AllDataObjsCommitmentTypeProperty.proofOfApproval();
CommitmentTypeProperty commitment = CommitmentTypeProperty.proofOfCreation();
// XPATH STRING
String xpathHeader ="/InvoiceHeader";
String xpathBody ="/InvoiceBody";
// OBJECT
DataObjectDesc obj1 = new DataObjectReference("");
obj1.withTransform(XPath2Filter.intersect( xpathHeader ).intersect(xpathBody));
SignedDataObjects dataObjs = new SignedDataObjects( obj1 );
// SIGN
signer.sign(dataObjs, nodeToAttachSignature);
// TRANSFORMER
Transformer transformer = TransformerFactory.newInstance().newTransformer();
// XML SIGNED
Result output = new StreamResult(new File("FileSigned.xml"));
Source input = new DOMSource(doc1);
transformer.transform(input, output);