首页 > 解决方案 > 加密连接字符串 - 允许其他机器解密

问题描述

我正在尝试开发一个连接到 SQL 数据库的 C# Winform 应用程序。

我的配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="myConnNameString"
providerName="System.Data.SqlClient"
connectionString="Data Source=myServerName;Initial 
Catalog=myDefDatabase;User=myUser;Password=myPassword;Application Name=myAppName" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

我能够通过以下方式成功加密此文件:

aspnet_regiis.exe -pef "connectionStrings" path_to_config_file

但是一旦我这样做了,我就不能再正常运行我的应用程序了。现在我只能通过以管理员身份运行它来做到这一点

Cannot decrypt with provider RsaProtectedConfigurationProvider.
Cannot open key container RSA

现在据我了解: https://docs.microsoft.com/en-us/previous-versions/msp-np/ff650304(v=pandp.10)

我创建了一个带有机器级容器的 RSA,所以只有加密文件的机器可以解密它,没有其他机器可以解密。

我怎样才能让其他机器也解密这个文件?我的应用程序存储在远程网络驱动器上的单个目录中,每个人都可以访问该目录。该应用程序通过快捷方式在他们的计算机上运行。

先感谢您!

编辑:还值得一提的是,并非我们公司的每个人都有本地管理员权限。

标签: c#sql-serverwinformsconnection-stringaspnet-regiis.exe

解决方案


好的,这就是我到目前为止所完成的:

创建可导出的 RSA 机器级密钥容器

aspnet_regiis -pc "MyKeys" -exp

现在我授予 NT AUTHORITY\NETWORK SERVICE 帐户对该 conatiner 的读取访问权限(无论它是什么)

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"

然后我用我的自定义提供程序加密了我的配置文件

aspnet_regiis -pef "connectionStrings" "path_to_config_file" - 
prov "myCustomProvider"

配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configProtectedData>
<providers>
  <add name="myCustomProvider"
       type="System.Configuration.RsaProtectedConfigurationProvider, 
                System.Configuration, Version=2.0.0.0,
                Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
                processorArchitecture=MSIL"
                keyContainerName="MyKeys"
                useMachineContainer="true" />
</providers>
</configProtectedData>
<configSections>
</configSections>
<connectionStrings>
    <add name="myConnectionString"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=myServerName;Initial 
    Catalog=myDefDatabase;User=myUser;Password=myPassword;Application 
    Name=myAppName" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

然后我将我的 RSA 密钥容器导出到一个文件:

-px "MyKeys" "C:\Users\name.surname\Desktop\test\test.xml" -pri

我现在应该在哪里导入这个 xml 文件?我的程序的每个用户都必须执行这一行吗?

aspnet_regiis -pi "MyKeys" test.xml

现在,当我尝试运行我的 Winform 程序(甚至在执行加密的机器上)时,我收到以下错误消息:

无法使用提供程序 myCustomProvider 解密。提供者错误消息。解码 OAEP 完成时出错

任何帮助将不胜感激。


推荐阅读