【问题标题】:How to make config files in Bin folder into non human-readable formats?(c#)如何将 Bin 文件夹中的配置文件制作成非人类可读的格式?(c#)
【发布时间】:2014-02-24 03:05:36
【问题描述】:

设置项目后,bin 文件夹中有一些 .config 文件。我想让它们成为非人类可读的格式。 1.我知道我可以使用asp net_ reg i i s -p e ...之类的命令,但是该项目是一个Win Form项目,并且在我的配置文件中没有像Web.config这样的部分。我的配置文件格式如下:

<...>
  <add key="..." value="...." />
  <add key="..." value="..." />
</...>
  1. 我也尝试加密配置文件,但是调用配置文件的地方太多了。这样做似乎太复杂了。

那么,有没有一些简单的方法可以使用 C# 将 bin 文件夹中的配置文件转换为非人类可读的格式?

【问题讨论】:

  • 使它们不可读的目标是什么?这些文件中有什么特别值得保护的东西吗?
  • 我不希望用户更改配置文件中 Key 或 Value 的内容
  • 他们总能做到,只是让事情变得更加困难并不能阻止真正有决心的人。此外,编辑此类文件的人可能知道它可能会产生不必要的副作用,但无论如何,如果配置丢失或无效,您的程序可能会假定一些合理的默认值。

标签: c# encryption configuration-files bin-folder


【解决方案1】:

您有两个选择。如果您对保护某些特殊参数感兴趣,例如密码,你可以只加密它,并且只有加密的值。如果你真的想让整个事情受到保护,你可以使用 SectionInformation.ProtectSection。

MSDN 示例代码:

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);

}

【讨论】:

    猜你喜欢
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多