C# 在ASP.NET之外的web.config文件中使用RsaProtectedConfigurationProvider解密加密

C# 在ASP.NET之外的web.config文件中使用RsaProtectedConfigurationProvider解密加密,c#,asp.net,.net,encryption,web-config,C#,Asp.net,.net,Encryption,Web Config,对于我的.NET Windows服务,我需要为我自己的ASP.NET web应用解析web.config文件。我使用XmlTextReader进行解析,除了需要解密使用aspnet\u regiis工具加密的web.config部分外,解析效果非常好。下面是我需要解密的数据库连接字符串部分的示例: <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider"> <Encry

对于我的.NET Windows服务,我需要为我自己的ASP.NET web应用解析web.config文件。我使用
XmlTextReader
进行解析,除了需要解密使用
aspnet\u regiis
工具加密的web.config部分外,解析效果非常好。下面是我需要解密的数据库连接字符串部分的示例:

<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>B702tRDVHJjC3CYXt7I0ucCDjdht/Vyk/DdUhwQyt7vepSD85dwCP8ox9Y1BUdjajFeTFfFBsGypbli5HPGRYamQdrVkPo07bBBXNT5H02qxREguGUU4iDtV1Xp8BLVZjQMV4ZgP6Wbctw2xRvPC7GvKHLI4fUN/Je5LmutsijA=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>ME+XJA2TAj3QN3yT4pJq3sRArC0i7Cz3Da71BkaRe9QNfuVuUjcv0jeGUN4wDdOAZ7LPq6UpVrpirY3kQcALDvPJ5nKxk++Mw75rjtIO8eh2goTY9rCK6zanfzaDshFy7IqItpvs/y2kmij25nM3ury6uO0hCf0UbEL1mbT2jXDqvcrHZUobO1Ef6bygBZ/8HpU+VfF9CTCob/BBE9zUkK37EQhcduwsnzBvDblYbF/Rd+F4lxAkZnecGLfCZjOzJB4xH1a0vvWtPR7zNwL/7I0uHzQjyMdWrkBnotMjoR70R7NELBotCogWO0MBimncKigdR3dTTdrCd72a7UJ4LMlEQaZXGIJp4PIg6qVDHII=</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

Rsa密钥
B702tRDVHJjC3CYXt7I0ucCDjdht/Vyk/DDUHWQYT7VEPSD85DWCP8OX9Y1BUDJAJFFFBSGYPBLI5HPGRYAMQDRVKPO07BBBXNT5H02QXReguguu4IDTv1XP8BLVZGJQMV4ZGP6WBCTW2xRVPC7GVKHLI4FUN/Je5LmutsijA=
2.我+XJA2 2对一个2 2 6 6 6 6 6 6 6 6 6个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人+一个中国人(8个中国人)2 8个中国人(8个中国人+一个中国10个中国人(6个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国10个中国的中国的DTTDRCD72A7UJ4LMLEQAZXGIJp4PIg6qVDHII=
所以我的问题是,我如何使用C#解密它,并拥有上面的文本

另外,我在特定的ASP.NET web应用程序之外执行此操作。

尝试一下:

string section = "connectionStrings";

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSection = config.GetSection(section);

if (configSection.SectionInformation.IsProtected)
{
     configSect.SectionInformation.UnprotectSection();
     config.Save();
}

您也可以选择以类似的方式加密连接字符串。

好的,这可能适用于非ASP.NET Windows程序。首先,从项目属性中的
.NET framework 4 Client Profile
将目标框架设置为
.NET framework 4
。然后添加
System.Web
程序集参考。最后,获取节调用
configSection.SectionInformation.GetRawXml()的实际解密XML而不是
config.Save()在上面的代码中。哦,您还可以使用其绝对文件路径打开web.config文件,如下所述: