簡體   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