Monday, April 11, 2011

SignedXml.CheckSignature works in .NET 1.1, but fails in .NET 3.5

Hi folks,

I have the following SignedXml code. This code works just perfectly in .NET 1.1, but when i run this in .NET 3.5, the method signedXml.CheckSignature() keeps returning false. With regard to the SignedXml class, did something change between 1.1 and 3.5? I'm puzzeled here! Can you guys help me out here?

Thanks! Ben

        SignedXml signedXml = new SignedXml();

        // Get public key
        Assembly assembly = Assembly.LoadFile("MyLib.Application.dll");
        Stream keyStream =
            assembly.GetManifestResourceStream("MyKey.xml");
        StreamReader keyReader = new StreamReader(keyStream, Encoding.UTF8);
        string publicKeyXml = keyReader.ReadToEnd();
        keyReader.Close();

        // Load and set key
        DSA dsaKeyPair = DSA.Create();
        dsaKeyPair.FromXmlString(publicKeyXml);
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new DSAKeyValue(dsaKeyPair));
        signedXml.KeyInfo = keyInfo;


        // Load and set data
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.PreserveWhitespace = true;
        xmlDocument.LoadXml(signedXmlData);

        XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
        signedXml.LoadXml((XmlElement)nodeList[0]);

        string returnXml = null;
        if (signedXml.CheckSignature())
        {
            XmlElement dataElement = signedXml.GetIdElement(xmlDocument, "LicenseElement");
            returnXml = dataElement.InnerXml;
        }
From stackoverflow
  • I found that signedXml.KeyInfo = keyInfo; was the problem for me. When I used the key as an argument to CheckSignature() then it worked - I was able to load 1.1 signed xml in a 2.0 application.

    Just remove the KeyInfo altogether:

      string returnXml = null;
        if (signedXml.CheckSignature(dsaKeyPair))
        {
            XmlElement dataElement = signedXml.GetIdElement(xmlDocument, "LicenseElement");
            returnXml = dataElement.InnerXml;
        }
    

0 comments:

Post a Comment