XML 签名验证失败

这是我正在尝试验证xml签名的代码。对于一个包含使用签名算法证书签名的 xml 的 xml 文件md2RSA,它可以正常工作。但是另一个包含使用签名算法证书签名的xml的xml SHA256withRSA,它失败了。


这是我试图用来验证签名验证的代码:


 public static void main(String[] args)

            throws ParserConfigurationException, SAXException, IOException, XMLSignatureException, XMLSecurityException

    {

        File f = new File("F:\\workspace\\signeddocument2.xml");

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

        dbFactory.setNamespaceAware(true);

        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

        org.w3c.dom.Document doc = dBuilder.parse(f);

        doc.getDocumentElement().normalize();


        Element sigElement = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE)

                .item(0);


        XMLSignature signature = new XMLSignature(sigElement, f.toURI().toURL().toString());

        boolean verified = false;


        verified = signature.checkSignatureValue(signature.getKeyInfo().getPublicKey());


        if (verified)

        {

            System.out.println("Signature verified successfully");

        }

        else

        {

            System.out.println("Signature verification failed.");

        }


    }


30秒到达战场
浏览 246回答 2
2回答

潇潇雨雨

似乎您的 xml 中缺少一个标签。我在这里找到了一些信息:https ://www.w3.org/TR/2009/WD-xmldsig-core2-20091022/如果您搜索“signname”,您会看到该规范需要一个标签:<C ID="signme" xmlns="&baz;"/>有完整的规范:<A xmlns:n1="&foo;">&nbsp; &nbsp; &nbsp;<B xmlns:n2="&bar;">&nbsp; &nbsp; &nbsp; &nbsp;<Signature xmlns="&dsig;">&nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Reference URI="#signme"/> ...&nbsp; &nbsp; &nbsp; &nbsp;</Signature>&nbsp; &nbsp; &nbsp; &nbsp;<C ID="signme" xmlns="&baz;"/>&nbsp; &nbsp; &nbsp;</B>&nbsp; &nbsp;</A>您的 XML 没有此标记。可能是原因。

阿晨1998

我弄清楚了signature验证失败的原因。XML与我试图验证的 XML 相比,我正在签名的名称空间不同signature。这transformation导致了signature失败。一旦我弄清楚了转换,signature验证就起作用了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java