Encrypt App.Config

Asked

Viewed 1,276 times

5

I’m trying to encrypt my Connection string, that’s in my app.config.

After reading some forums, I saw that 2 methods need to be created:

class proteger_app
{
    public static void Criptografar()
    {
        Configuration config =
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ConfigurationSection section =
            config.ConnectionStrings;

        if (!section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
            section.SectionInformation.ForceSave = true;
            config.Save();
        }
    }

    public static void Decriptografar()
    {
        Configuration config =
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ConnectionStringsSection section =
            config.ConnectionStrings;

        if (section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            section.SectionInformation.ForceSave = true;
            config.Save();
        }
    }
}

Until then beauty, methods created, and then?

public void chek()
    {
        string sqconn, _sql;
        int th;

        proteger_app.Decriptografar();
        sqconn = ConfigurationManager.ConnectionStrings["sql brayton"].ConnectionString;
        proteger_app.Criptografar();

        _sql = @"SELECT id FROM base64";

        SqlConnection con = new SqlConnection(sqconn);

        try
        {

But when will I look at the app.config is unencrypted.

  • 2

    It doesn’t make much sense. Why do you want to encrypt inside the application? The correct is outside, per command line.

  • Haaaaaa... could you give me an explanation? or some link that directs me to some documentation? would you have to create within my application, a console? and when I execute ?

1 answer

6


  • Make sure that the . NET Framework is in your PATH (here put C:\Windows\Microsoft.NET\Framework\v4.0.30319 in the path);
  • Open a Powershell or command prompt;
  • Use the following command:

    > aspnet_regiis -pef "connectionStrings" "C:\Caminho\Do\Seu\Projeto"
    

Done these steps, you web.config will be encrypted.

The full explanation is here.

To decrypt:

> aspnet_regiis -ped "connectionStrings" "C:\Caminho\Do\Seu\Projeto"

See more here.

  • Gypsy, is a winform application, this method we passed and for web application with right Asp.NET?

  • No. Any application.

  • Um.. Then for my application to read, normal? if you need to edit?

  • I updated the response with the crypto reversal.

  • Dude, I was able to encrypt, but now I can’t read the connectionString on my app, could you give me a help?

  • Ask another question, please. Also put the code used (if any) if you are using to read the Connection string.

  • help: http://answall.com/questions/152303/app-config-howto use connectionstring-encrypted

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.