C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Encrypt decrypt data within your configuration file


Posted Date: 16 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: MASNSNMember Level: Gold    
Rating: 1 out of 5Points: 10



using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Security.Cryptography;

using System.Configuration;

using System.Windows.Forms;

namespace EncryptConfigurationFile

{

class ConfigurationSectionEncryptor

{

private ConfigurationSectionEncryptor() { }

///

/// This static method helps encrypt the given data set

///


/// String : The configuration file path

public static void Encrypt(string ConfigurationFilePath)

{

//Create a new ExeConfigurationFileMap

ExeConfigurationFileMap oFile = new ExeConfigurationFileMap();

//Precise its path

oFile.ExeConfigFilename = ConfigurationFilePath;



//Create a new configuration object related to the configuration file

Configuration oConfiguration = ConfigurationManager.OpenMappedExeConfiguration(oFile, ConfigurationUserLevel.None);

//Create a section and set it as the targeted section

ConnectionStringsSection oSection = oConfiguration.GetSection("connectionStrings") as ConnectionStringsSection;

//if the section is already encrypted dont ecrypt it again

if (oSection != null && !oSection.SectionInformation.IsProtected)

{

//The section ecryption

oSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

//Update the configuration file

oConfiguration.Save();

MessageBox.Show("Connection string encrypted");

}

}

///

/// This static method helps encrypt the given data set

///


/// String : The configuration file path

public static void Decrypt(string ConfigurationPath)

{

//Create a new ExeConfigurationFileMap

ExeConfigurationFileMap oFile = new ExeConfigurationFileMap();

//Precise its path

oFile.ExeConfigFilename = Application.StartupPath + @"\EncryptConfigurationFile.exe.config";

//Create a new configuration object related to the configuration file

Configuration oConfiguration = ConfigurationManager.OpenMappedExeConfiguration(oFile, ConfigurationUserLevel.None);

//Create a section and set it as the targeted section

ConnectionStringsSection oSection = oConfiguration.GetSection("connectionStrings") as ConnectionStringsSection;

if (oSection != null && oSection.SectionInformation.IsProtected)

{

oSection.SectionInformation.UnprotectSection();

oConfiguration.Save();

MessageBox.Show("Connection string decrypted");

}

}

}

}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Encrypt data configuration file  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Connect to Excel as data source
Previous Resource: Zip and Unzip files using C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use