0

I have connection string in my c# code, so i can get data from database using a stored procedure.

The connection string is like this:

connection.ConnectionString = "Data Source=192.168.4.33;Initial Catalog=database;Persist Security Info=True;User ID=test;Password=@test312321";

How can i Encrypt the connection, and then when i want to use it decrypt?

Phill
  • 407
  • 1
  • 8
  • 30

1 Answers1

0

You can use this :

try
{
    // Open the configuration file and retrieve 
    // the connectionStrings section.
    Configuration config = ConfigurationManager.
        OpenExeConfiguration(exeConfigName);

    ConnectionStringsSection section =
        config.GetSection("connectionStrings")
        as ConnectionStringsSection;

    if (section.SectionInformation.IsProtected)
    {
        // Remove encryption.
        section.SectionInformation.UnprotectSection();
    }
    else
    {
        // Encrypt the section.
        section.SectionInformation.ProtectSection(
            "DataProtectionConfigurationProvider");
    }
    // Save the current configuration.
    config.Save();

    Console.WriteLine("Protected={0}",
        section.SectionInformation.IsProtected);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

or you Enterprise Library Data Access Application Block to perform the encryption using RSAProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider. the link: http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx