Encrypt App.Config

4

I'm trying to encrypt my connection string , which is 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();
        }
    }
}

So far 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 I look at app.config is unencrypted.

    
asked by anonymous 26.07.2016 / 22:26

1 answer

5
  • Make sure the .NET Framework is in your PATH (here I 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"
    

Once you've done these steps, your web.config will be encrypted.

The full explanation is here .

To decrypt:

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

See more here .

    
27.07.2016 / 21:29