简体   繁体   English

加密 app.config 文件

[英]Encrypting app.config File

I have an app.config file that I need to distribute with my application.我有一个 app.config 文件,需要与我的应用程序一起分发。 It was created because of a Service Reference to an ASMX web service I added.它是由于对我添加的 ASMX Web 服务的服务引用而创建的。

It isn't a huge deal if this file is modified/viewed, but I still would like to make it secure.如果修改/查看此文件并不是什么大问题,但我仍然希望使其安全。 I already check the hash of the config and make sure it is valid, but I still want an added layer of protection.我已经检查了配置的哈希值并确保它有效,但我仍然想要一个额外的保护层。

Here is my config: http://pastie.org/private/zjdzadnfwrjvwkmlbdsqw这是我的配置: http : //pastie.org/private/zjdzadnfwrjvwkmlbdsqw

So is there anything in there that I can encrypt or anything?那么里面有什么我可以加密的东西吗?

You cannot encrypt the entire <system.serviceModel> - it's a configuration section group, which contains configuration sections.您不能加密整个<system.serviceModel> - 它是一个配置节组,其中包含配置节。

The aspnet_regiis will only encrypt configuration sections - so you need to selectively encrypt those parts you need, like this: aspnet_regiis只会加密配置部分 - 因此您需要有选择地加密您需要的那些部分,如下所示:

cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
aspnet_regiis.exe -pef "system.serviceModel/bindings" .
aspnet_regiis.exe -pef "system.serviceModel/services" .

etc.等等。

With this, you can encrypt what you need easily - what isn't too important, can be left in clear text.有了这个,您可以轻松加密您需要的内容 - 不太重要的内容可以以明文形式保留。

Word of warning: since it's aspnet_regiis , it expects to be dealing with a web.config file - copy your app.config to a location and call it web.config , encrypt your sections, and copy those encrypted sections back into your own app.config .警告:因为它是aspnet_regiis ,所以它需要处理web.config文件 - 将您的app.config复制到一个位置并将其命名为web.config ,加密您的部分,并将这些加密的部分复制回您自己的app.config

Or write your own config section encrypter/decrypter - it's really just a few lines of code!或者编写您自己的配置部分加密器/解密器 - 这实际上只是几行代码! Or use mine - I wrote a small ConfigSectionCrypt utility, come grab it off my OneDrive - with full source (C# - .NET 3.5 - Visual Studio 2008).或者使用我的 - 我编写了一个小的ConfigSectionCrypt实用程序,从我的OneDrive 中获取它 - 带有完整源代码(C# - .NET 3.5 - Visual Studio 2008)。 It allows you to encrypt and decrypt sections from any config file - just specify the file name on the command line.它允许您加密和解密任何配置文件中的部分 - 只需在命令行上指定文件名。

I use the following to encrypt my connection strings in web.config, why not use the same for yourself.我使用以下内容在 web.config 中加密我的连接字符串,为什么不自己使用相同的内容。 I am not sure though.我不确定。

To Encrypt:加密:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" "\myWebSitePath"

To Decrypt:解密:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" "\myWebsitePath" 

Put them in bat files so you can encrypt or decrypt on the fly.将它们放在 bat 文件中,以便您可以即时加密或解密。

You must set a reference to System.Configuration.dll in your project for the code to run.您必须在项目中设置对 System.Configuration.dll 的引用才能运行代码。

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                configFileMap.ExeConfigFilename = exeConfigName;
                System.Configuration.Configuration myConfig = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

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

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

                myConfig.Save();

Well the file will be read by the program when it is run so changing the file could be a bad idea, you could add checksums to each line to make sure it's valid by checking it in your application or checking for modifications since last run or something.好吧,该文件将在运行时被程序读取,因此更改文件可能是一个坏主意,您可以向每一行添加校验和,以通过在您的应用程序中检查它或检查自上次运行以来的修改或其他内容来确保它有效. I've never heard of encrypting an app.config before to be honest.老实说,我以前从未听说过对 app.config 进行加密。

It isn't a huge deal if this file is modified/viewed...如果修改/查看此文件,这不是什么大问题...

In that case, what is the security for?在这种情况下,安全性有什么用?

You can programmatically encrypt sections of a config file with SectionInformation.ProtectSection .您可以使用SectionInformation.ProtectSection以编程方式加密配置文件的部分。

The answer from @marc_s is awesome, but I had a little trouble understanding exactly how to do what he had mentioned. @marc_s 的回答很棒,但我在理解他提到的具体如何做时有点困难。 This MSDN forum answer does a really good job of explaining the same process in simpleton format for people like myself who are not programming big shots yet. 这个 MSDN 论坛的答案很好地为像我这样还没有编程大人物的人以简单的格式解释了相同的过程。 Here is the breakdown:这是细分:

The best way to do this is to protect it using the aspnet_regiis.exe application.执行此操作的最佳方法是使用 aspnet_regiis.exe 应用程序保护它。 Even if the application is not an ASP.NET application, this will still work.即使应用程序不是 ASP.NET 应用程序,这仍然可以工作。 Here's how.就是这样。

  1. Rename the app.config in your directory to web.config (don't worry, this is just temporary, we'll rename it back later).将您目录中的 app.config 重命名为 web.config(别担心,这只是暂时的,我们稍后会重新命名)。
  2. Go to the command prompt.转到命令提示符。
  3. Type the following (replace the last argument with the path containing the directory to the app.config, currently renamed to web.config. For example, if the full path to the web config is "C:\\documents and settings\\bob\\projects\\myproject\\web.config", you would use "C:\\documents and settings\\bob\\projects\\myproject")键入以下内容(用包含 app.config 目录的路径替换最后一个参数,当前重命名为 web.config。例如,如果 web 配置的完整路径是“C:\\documents and settings\\bob\\projects \\myproject\\web.config”,您将使用“C:\\documents and settings\\bob\\projects\\myproject”)

%windir%\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_regiis -pef "connectionStrings" %windir%\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_regiis -pef "connectionStrings"

  1. You should see it say "Succeeded!"你应该看到它说“成功!”
  2. Reopen the web.config, it should look something like this:重新打开 web.config,它应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">

    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"

      xmlns="http://www.w3.org/2001/04/xmlenc#">

      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />

      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">

        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">

          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />

          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">

            <KeyName>Rsa Key</KeyName>

          </KeyInfo>

          <CipherData>

            <CipherValue>rUmEA8h02uMZ5M4uEVtL+5M/UvPuyJ4UJz5d/Pd4h4jpFPGVf29ha4d+BMt/iOupVisXDxuZY1jzyc6O0ZixGcCkZqbynwKjouVANQVWUnDgIFgVap2ohsxjblAMtWHTUWDlL0ST5tqSVHNQE+r9G59Bnrp5HkuU3Eg09/8j6Jo=</CipherValue>

          </CipherData>

        </EncryptedKey>

      </KeyInfo>

      <CipherData>

        <CipherValue>U2JEHzJ+WjSdlocT00cC9TE3+Dn3v7xE1RwX7bKAPuISO2f3hm18MZHnm1hXEBlnDS6iBNRPN87+BJJvZXYz+Sis/ZD4xBZEP4jBG2F8tqlLUbagv3W4epbuTSp2aalb5rdcBoycdIzSj2CApOzSaSKkMDvZrX8yoJI9RfuGnOWmNa4bncHkUEDvWq+uCK/8uaQ48J5uRoq7O0YgIe9jDg==</CipherValue>

      </CipherData>

    </EncryptedData>

  </connectionStrings>

</configuration>
  1. Lastly, rename the file from web.config to app.config.最后,将文件从 web.config 重命名为 app.config。

That's it!就是这样! You shouldn't have to do anything else to get this to work.你不应该做任何其他事情来让它工作。 If you use the ConfigurationManager in your code to retrieve the setting, it should fetch it just fine for you using the same code.如果您在代码中使用 ConfigurationManager 来检索设置,它应该可以使用相同的代码为您获取它。

That being said, if you're going to install this on several computers, you might want to consider providing user-specific logins to your server for each user who uses the application.话虽如此,如果您要在多台计算机上安装它,您可能需要考虑为每个使用该应用程序的用户提供特定于您的服务器的登录信息。 That way you can still track what they do, and you can prevent them from logging on at all on a user-by-user basis.这样,您仍然可以跟踪他们所做的事情,并且可以在逐个用户的基础上完全阻止他们登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM