/// <summary> /// Signs the electronic invoice using the given certificate & RSA key /// </summary> /// <param name="eInvoice">The electronic invoice</param> /// <param name="certificate">The certificate</param> /// <param name="key">The RSA Key</param> /// <returns></returns> public XAdESSignatureVerifier Sign(X509Certificate2 certificate, RSA key) { if (certificate == null) { throw new ArgumentNullException("certificate cannot be null"); } if (key == null) { throw new ArgumentNullException("key cannot be null"); } var document = this.ToXmlDocument(); var signedXml = new XAdESSignedXml(document); // Set the key to sign signedXml.SigningKey = key; signedXml.SetSignatureInfo() .SetKeyInfo(certificate, (RSA)certificate.PublicKey.Key) // Key Info .SetQualifyingPropertiesObject(certificate) // Add XAdES references .ComputeSignature(); // Compute Signature // Import the signed XML node document.DocumentElement.AppendChild(document.ImportNode(signedXml.GetXml(), true)); return(new XAdESSignatureVerifier(document)); }
public void Sign(Stream xml, Stream signature, string signedElementXPath, X509Certificate certificate, AsymmetricKeyParameter key, PolicyIdentifier policyId) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(xml); XAdESSignedXml signedXML = new XAdESSignedXml(xmlDoc); signedXML.SigningKey = key; signedXML.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; signedXML.Certificate = certificate; signedXML.SignedElementXPath = signedElementXPath; signedXML.PolicyId = policyId; signedXML.SignedInfo.CanonicalizationMethod = "http://www.w3.org/2001/10/xml-exc-c14n#"; signedXML.ComputeXAdESSignature(); XmlElement xmlDigitalSignature = signedXML.GetXml(); //xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true)); XmlTextWriter wr = new XmlTextWriter(signature, Encoding.UTF8); wr.Formatting = Formatting.None; xmlDigitalSignature.WriteTo(wr); wr.Flush(); signature.Position = 0; }
/// <summary> /// Verify the signature against an asymetric /// algorithm and return the result. /// </summary> /// <param name="eInvoice"></param> /// <param name="Key"></param> /// <returns></returns> /// <remarks>http://social.msdn.microsoft.com/Forums/hu-HU/netfxbcl/thread/d6a4fe9f-7d2e-419c-ab19-9e57c75ba90f</remarks> public bool CheckSignature() { XAdESSignedXml signedXml = new XAdESSignedXml(this.signedDocument); XmlNamespaceManager nsmgr = XsdSchemas.CreateXadesNamespaceManager(this.signedDocument); // Load the signature node. signedXml.LoadXml((XmlElement)this.signedDocument.SelectSingleNode("//ds:Signature", nsmgr)); // Check the signature against the passed asymetric key // and return the result. return(signedXml.CheckSignature()); }