You must Sign In to post a response.
  • Category: Windows

    Encryption of the app.config file

    Hi,


    I am using the app.config file in that i have all my connection strings .

    Now i want to encrypt the app.config and decrypt it wile installing my application to all the other users in their system.

    So please can anyone tell me how can i do this and if possible please provide me a sample application .


    Thanks
    Divya
  • #648564
    hi

    here is code for Encryption


    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("config.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)[0] as 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();

    }

    }

    Regards
    Sagar pawar
    Software developer

  • #648598
    Check below code:


    To Encrypt:

    Configuration conf = ConfigurationManager.OpenExeConfiguration(appConfig);

    ConfigurationSection section = this.conf.GetSection("connectionStrings");

    if (!section.SectionInformation.IsProtected) {

    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

    section.SectionInformation.ForceSave = true;

    conf.Save(ConfigurationSaveMode.Modified);

    }

    To decrypt just do the oposite:

    if (section.SectionInformation.IsProtected) {

    section.SectionInformation.UnprotectSection();


    }

    Regards
    Mr.Rajesh
    Software Engg.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.