This is a mirror of official site: http://jasper-net.blogspot.com/

System.Security.Cryptography.Xml Namespace

| Tuesday, April 6, 2010
The System.Security.Cryptography.Xml namespace contains a full implementation of the World Wide Web Consortium standard for digitally signing XML data and files. In other words, the namespace helps you to sign any XML object with a digital signature. Refer to the XML-Signature Syntax and Processing page at http://www.w3.org/TR/xmldsig-core/  for details on this progressing standard.

The sample code in Listing 22.37 shows how to sign XML data and produce an envelope for it via the RSA algorithm.

Listing 22.37: SignXML1.cs, Compute Signature for XML Data

using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

public class DigitalSignSample
{
   public static void Main()
   {
       // generate XML data
       XmlDocument document = new XmlDocument();
       XmlNode node = document.CreateNode(XmlNodeType.Element, "", @"Visual Studio .NET", "sign xml samples");
       node.InnerText = @"C# wimps the lama's bass...";
       document.AppendChild(node);
       Console.WriteLine("OriginalXML data:\r\n" + document.OuterXml + "\r\n");

       // create signedxml variable
       RSA rsa = System.Security.Cryptography.RSA.Create();
       SignedXml signedXml = new SignedXml();
       signedXml.SigningKey = rsa;

       // create dataobject
       DataObject dataObject = new System.Security.Cryptography.Xml.DataObject();
       dataObject.Data = document.ChildNodes;
       dataObject.Id = "goo";

       // add dataobject and reference
       signedXml.AddObject(dataObject);
       signedXml.AddReference(new Reference("#goo"));

Read more: C# Corner

Posted via email from jasper22's posterous

0 comments: