Created
December 23, 2013 16:21
-
-
Save yaronn/8099920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void SignXmlFile(string FileName, string SignedFileName, RSA Key) | |
| { | |
| // Create a new XML document. | |
| XmlDocument doc = new XmlDocument(); | |
| // Load the passed XML file using its name. | |
| doc.Load(new XmlTextReader(FileName)); | |
| // Create a SignedXml object. | |
| SignedXml signedXml = new SignedXml(doc); | |
| // Add the key to the SignedXml document. | |
| signedXml.SigningKey = Key; | |
| // Create a reference to be signed. | |
| Reference reference = new Reference(); | |
| reference.Uri = ""; | |
| // Add an enveloped transformation to the reference. | |
| XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); | |
| reference.AddTransform(env); | |
| // Add the reference to the SignedXml object. | |
| signedXml.AddReference(reference); | |
| // Compute the signature. | |
| signedXml.ComputeSignature(); | |
| // Get the XML representation of the signature and save | |
| // it to an XmlElement object. | |
| XmlElement xmlDigitalSignature = signedXml.GetXml(); | |
| // Append the element to the XML document. | |
| doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); | |
| if (doc.FirstChild is XmlDeclaration) | |
| { | |
| doc.RemoveChild(doc.FirstChild); | |
| } | |
| // Save the signed XML document to a file specified | |
| // using the passed string. | |
| XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false)); | |
| doc.WriteTo(xmltw); | |
| xmltw.Close(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment