gpt4 book ai didi

c# - 使用提供程序 'RsaProtectedConfigurationProvider' 解密失败?

转载 作者:太空狗 更新时间:2023-10-29 19:59:54 29 4
gpt4 key购买 nike

在我的 Windows 应用程序中,我试图加密 app.config 文件的连接字符串部分,我的 app.config 文件的连接字符串部分是

<connectionStrings>
<add name="SQLiteDB" connectionString="Data Source=|DataDirectory|database.s3db;
Version=3;password=mypassword;" providerName="System.Data.Sqlite"/>
</connectionStrings>

在 .cs 文件中,我正在加密它

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
ConfigurationSection section = config.GetSection("connectionStrings") as ConnectionStringsSection; // could be any section

if (!section.IsReadOnly())
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}

运行此代码后,我在不同的 app.config 中获得加密连接字符串,此 app.config 驻留在 bin\debug 文件夹中,此 .config 文件的名称为 nameofapplication.exe.config。

问题是当我设置这个应用程序并在其他机器上运行时如果出现错误:

System.Configuration.ConfigurationErrorsException: Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: The RSA key container could not be opened.

我是第一次做所以不知道怎么解决这个问题,卡得很厉害。

最佳答案

app.config 文件将使用本地计算机上的证书进行加密。该证书不会出现在另一台机器上。因此,您将无法解密 app.config 文件。

为此,您需要在您的机器上导出加密 key ,然后在另一台机器上导入它。以下文章演示了如何做到这一点:演练:Creating and Exporting an RSA Key Container

关于c# - 使用提供程序 'RsaProtectedConfigurationProvider' 解密失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16124389/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com