繁体   English   中英

加密连接字符串 - 允许其他机器解密

[英]Encrypting connection string - allowing other machines to decrypt

我正在尝试开发一个连接到 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,所以只有加密文件的机器才能解密它,其他机器都做不到。

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

先感谢您!

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

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

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

aspnet_regiis -pc "MyKeys" -exp

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

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 完成时出错

任何帮助将不胜感激。

暂无
暂无

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

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