Use DataReference to encrypt and decrypt multiple XML elements using different session keys. : Encryption « Security « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Security » Encryption 
35.14.5.Use DataReference to encrypt and decrypt multiple XML elements using different session keys.
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;

class Program
{
    static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.PreserveWhitespace = true;
        xmlDoc.Load("test.xml");
        RSA rsaKey = new RSACryptoServiceProvider();
        // Encrypt the "creditcard" element.
        Encrypt(xmlDoc, "creditcard""EncryptedElement1", rsaKey, "rsaKey");

        // Encrypt the "creditcard2" element.
        Encrypt(xmlDoc, "creditcard2""EncryptedElement2", rsaKey, "rsaKey");

        Console.WriteLine("Encrypted XML:");
        Console.WriteLine(xmlDoc.OuterXml);

        // Decrypt the "creditcard" element.
        Decrypt(xmlDoc, rsaKey, "rsaKey");
        
        Console.WriteLine("Decrypted XML:");
        Console.WriteLine(xmlDoc.OuterXml);
        rsaKey.Clear();
    }
    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string KeyName)
    {
        XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0as XmlElement;

        RijndaelManaged sessionKey = new RijndaelManaged();
        sessionKey.KeySize = 256;

        EncryptedXml eXml = new EncryptedXml();

        byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);

        EncryptedData edElement = new EncryptedData();
        edElement.Type = EncryptedXml.XmlEncElementUrl;
        edElement.Id = EncryptionElementID;

        edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);

        EncryptedKey ek = new EncryptedKey();

        byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);

        ek.CipherData = new CipherData(encryptedKey);
        ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
        edElement.KeyInfo = new KeyInfo();

        KeyInfoName kin = new KeyInfoName();
        kin.Value = KeyName;
        ek.KeyInfo.AddClause(kin);

        DataReference dRef = new DataReference();
        dRef.Uri = "#" + EncryptionElementID;

        ek.AddReference(dRef);
        edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
        edElement.CipherData.CipherValue = encryptedElement;
        EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
    }

    public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
    {
        EncryptedXml exml = new EncryptedXml(Doc);
        exml.AddKeyNameMapping(KeyName, Alg);
        exml.DecryptDocument();

    }

}

//  <root>
//     <creditcard xmlns="myNamespace" Id="tag1">
//         <number>A</number>
//         <expiry>02/02/2011</expiry>
//     </creditcard>
//     <creditcard2 xmlns="myNamespace" Id="tag2">
//         <number>2</number>
//         <expiry>02/02/2012</expiry>
//     </creditcard2>
// </root>
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.