我正在尝试用java验证数字签名。下面是代码。
Document doc = convertStringToXMLDocument(message);
NodeList nl = doc.getElementsByTagName(SIGNATURE);
KeyStore ks = KeyStore.getInstance(JKS);
Resource resource = resourceLoader.getResource("file.jks");
ks.load(resource.getInputStream(), "Passwor".toCharArray());
X509Certificate trustedcertificate = (X509Certificate) ks.getCertificate(alias);
DOMValidateContext valContext = new DOMValidateContext(trustedcertificate.getPublicKey(),nl.item(0));
valContext.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
boolean coreValidity = signature.validate(valContext);
字符串到文档方法:
public static Document convertStringToXMLDocument(String xmlString) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
} catch (Exception e) {
logger.error("Exception {} ", e.getMessage());
}
return null;
}
这是工作正常的消息完美,但我的一个消息有笑脸在它。因此它表示无效消息(消息肯定有效)。有人能告诉我哪里出了问题吗。
暂无答案!
目前还没有任何答案,快来回答吧!