0

After I setup a project, there are some .config files in bin folder. I want to make them non human-readable formats. 1. I know I can use command such as asp net_ reg i i s -p e ..., but the project is a Win Form Project, and in my config file there is no parts like Web.config. My config file formats is like below:

<...>
  <add key="..." value="...." />
  <add key="..." value="..." />
</...>
  1. I also tried to encrypt the config files, however there are so many places that call the config files. It seems too complex to do so.

So, is there some easy way to make my config files in bin folder into non human-readable formats using C#?

Song
  • 9
  • 2
  • 2
    What's your goal in making them non human-readable? Is there something particular in those files that deserve protection? – Alejandro Feb 24 '14 at 03:11
  • I do not want the user to change the content of Key or Value in the config file – Song Feb 24 '14 at 06:12
  • They will be always able to do so, just making it more difficult won't stop someone who's really determined. Besides, someone editing such a file may known presumably that it can have unwanted side-effects, but in any case your program may assume some reasonable default if the configuration is missing or invalid. – Alejandro Feb 24 '14 at 19:05

1 Answers1

1

You have two options.. If you are interested in protecting some special parameter, e.g. a password, you can just encrypt it, and only have the encrypted value. If you really want to make the whole thing protected, you can use SectionInformation.ProtectSection.

MSDN Sample code for that:

static public void ProtectSection()
{

    // Get the current configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);


    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");


    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration  
    // section. Note, the system 
    // uses the Rsa provider to decrypt 
    // the section transparently. 
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);

}
XeroxDucati
  • 5,130
  • 2
  • 37
  • 66