Pradeep Loganathan

Distributed
posts - 17 , comments - 74 , trackbacks - 1

XML Encryption - Sample - 5

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Xml;

using System.Security;

using System.Security.Cryptography;

using System.Security.Cryptography.Xml;

 

namespace XMLDIGSIG

{

    class Class1

    {

 

        static void Main(string[] args)

        {

 

            TripleDES tdes = new TripleDESCryptoServiceProvider();

            EncryptXML(tdes);

            DecryptXML(tdes);

            Console.ReadKey();

        }

 

 

        public static void EncryptXML(TripleDES tdes)

        {

            try

            {

                XmlDocument doc = new XmlDocument();

                doc.Load("xmlfile1.xml");

                EncryptedXml encxml = new EncryptedXml(doc);

 

 

 

                byte[] encpayment = encxml.EncryptData((XmlElement)doc.SelectSingleNode("/order/payment"), tdes, false);

 

 

                EncryptedData ed = new EncryptedData();

                ed.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl);

                ed.Type = EncryptedXml.XmlEncElementUrl;

                ed.CipherData = new CipherData();

                ed.CipherData.CipherValue = encpayment;

                EncryptedXml.ReplaceElement((XmlElement)doc.SelectSingleNode("/order/payment"), ed, false);

                doc.Save("encxml.xml");

            }

            catch (CryptographicException ce)

            {

                Console.WriteLine(ce.ToString());

            }

        }

 

        public static void DecryptXML(TripleDES tdes)

        {

            try

            {

                XmlDocument doc = new XmlDocument();

                doc.Load("encxml.xml");

 

                XmlElement encelement = (XmlElement)(doc.GetElementsByTagName("EncryptedData")[0]);

 

                EncryptedData ed = new EncryptedData();

                ed.LoadXml(encelement);

 

                EncryptedXml exml = new EncryptedXml();

                byte[] decpay = exml.DecryptData(ed, tdes);

                exml.ReplaceData(encelement, decpay);

                doc.Save("decxml.xml");

            }

            catch(CryptographicException ce)

            {

                Console.WriteLine(ce.ToString());

            }

 

 

           

        }

    }

}

Print | posted on Monday, August 7, 2006 8:03 AM | Filed Under [ WS-Security ]

Powered by: