/// <summary> /// Check the signature of the specified signed document (created with CreateSignedDoc) using the specified public key. /// </summary> /// <param name="signedDoc"></param> /// <param name="keyPub">Public key</param> /// <returns></returns> public static bool CheckSignature(System.Xml.XmlDocument signedDoc, string keyPub) { System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); rsa.FromXmlString(keyPub); // Create a new SignedXml object and pass it // the XML document class. System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(signedDoc); // Load the first <signature> node. sx.LoadXml(GetSignatureFromSignedDoc(signedDoc)); // Check the signature and return the result. return(sx.CheckSignature(rsa)); }
// Verify the signature of an XML file against an asymmetric // algorithm and return the result. private Boolean VerifyXml(XmlDocument Doc, RSA Key) { // Check arguments. if (Doc == null) { throw new ArgumentException("Doc"); } if (Key == null) { throw new ArgumentException("Key"); } // Create a new SignedXml object and pass it // the XML document class. var signedXml = new System.Security.Cryptography.Xml.SignedXml(Doc); // Find the "Signature" node and create a new XmlNodeList object. XmlNodeList nodeList = Doc.GetElementsByTagName("Signature"); // Throw an exception if no signature was found. if (nodeList.Count <= 0) { throw new CryptographicException("Verification failed: No Signature was found in the document."); } // Though it is possible to have multiple signatures on // an XML document, this app only supports one signature for // the entire XML document. Throw an exception // if more than one signature was found. if (nodeList.Count >= 2) { throw new CryptographicException("Verification failed: More that one signature was found for the document."); } // Load the first <signature> node. signedXml.LoadXml((XmlElement)nodeList[0]); // Check the signature and return the result. return(signedXml.CheckSignature(Key)); }
/// <summary> /// Check the signature of the specified signed document (created with CreateSignedDoc) using the specified public key. /// </summary> /// <param name="signedDoc"></param> /// <param name="keyPub">Public key</param> /// <returns></returns> public static bool CheckSignature(System.Xml.XmlDocument signedDoc, string keyPub) { System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(); rsa.FromXmlString(keyPub); // Create a new SignedXml object and pass it // the XML document class. System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(signedDoc); // Load the first <signature> node. sx.LoadXml(GetSignatureFromSignedDoc(signedDoc)); // Check the signature and return the result. return sx.CheckSignature(rsa); }