解密App.config中的连接字符串

时间:2012-09-28 16:44:54

标签: c# .net

我有一个控制台应用程序,其中我有一些加密的连接字符串,如下所示:

<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>soemvalue here</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>some valye here</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

我尝试使用控制台应用程序访问连接字符串,如下所示:

var connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; 

我收到以下错误:

  

使用提供程序'RsaProtectedConfigurationProvider'无法解密。来自提供程序的错误消息:无法打开RSA密钥容器。

当我尝试从app.config访问相同的连接字符串而不解密时,它工作正常。加密有问题吗?我认为加密后我只需要以正常方式获取连接字符串,它就会自动解密。

1 个答案:

答案 0 :(得分:4)

您需要在同一台计算机上加密和解密,或者需要导出/导入密钥。

参见这篇文章:

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d43a4bd7-7cc1-40cf-8269-82c92894df43/