Create a new instance of the CipherData class using CipherReference information. : Encryption « Security « C# / CSharp Tutorial






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

    class Sample1
    {
        [STAThread]
        static void Main(string[] args)
        {
            String uri = "http://www.yourdomain.com/document.xml";
            Transform base64 = new XmlDsigBase64Transform();
            TransformChain tc = new TransformChain();
            tc.Add(base64);
            CipherReference reference = new CipherReference(uri, tc);
            CipherData cd = new CipherData(reference);

            EncryptedData ed = new EncryptedData();

            ed.Id = "ED";
            ed.EncryptionMethod = new EncryptionMethod("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
            ed.CipherData = cd;

            KeyInfo ki = new KeyInfo();
            ki.AddClause(new KeyInfoRetrievalMethod("#EK", "http://www.w3.org/2001/04/xmlenc#EncryptedKey"));
            ed.KeyInfo = ki;

            XmlDocument doc = new XmlDocument();
            XmlElement encryptionPropertyElement = (XmlElement)doc.CreateElement("EncryptionProperty", EncryptedXml.XmlEncNamespaceUrl);
            EncryptionProperty ep = new EncryptionProperty(encryptionPropertyElement);
            ed.AddProperty(ep);

            try{
                string path = @"c:\MyTest.xml";
                File.WriteAllText(path, ed.GetXml().OuterXml);
            }
            catch (IOException e)
            {
                Console.WriteLine("File IO error. {0}", e);
            }
        }
    }








35.14.Encryption
35.14.1.Symmetric Encryption
35.14.2.PublicKey Cryptography
35.14.3.Secret Key Cryptography: RijndaelManaged
35.14.4.Create a new instance of the CipherData class using CipherReference information.
35.14.5.Use DataReference to encrypt and decrypt multiple XML elements using different session keys.