我的应用程序可以保护所选的配置文件。这是使用 SectionInformation.ProtectSection
完成的加载指定部分的方法Configuration
.我正在使用标准提供程序 RsaProtectedConfigurationProvider
.
代码非常简单 - 与 example on MSDN 非常相似.
有什么方法可以设置提供商应该使用的 key 大小吗?据我所知,RSA 的默认值为 1024。我需要将其设置为 2048 或更大。
当我们使用 asp_regiis.exe 时,可以使用命令行选项 -size 来完成类似的操作。但我需要从代码中做到这一点。也许有任何方法可以配置 RsaProtectedConfigurationProvider
或预先创建 key 并将其以某种方式注入(inject)默认 key 存储,因此接下来使用 SectionInformation.ProtectSection
将 catch 它......
感谢您提供任何建议或示例。
最佳答案
RSAProtectedConfigurationProvider
提供两种不同的方法。一个叫 AddKey
可用于在容器内创建 key 。如果将 key 标记为可导出,则可以使用 ExportKey
稍后获取该 key 并将其存储在其他地方的方法。
如果您已有 key ,则可以使用 ImportKey
方法。它将接受一个 XML blob,很像来自 ExportKey
的 XML blob。
RsaProtectedConfigurationProvider
使用默认容器名称 NetFrameworkConfigurationKey。因此,如果您预先创建 key 并将其添加到该容器中,那么提供商应该在您使用它时获取它。
// Same properties as .NET uses to load the key
CspParameters csp = new CspParameters();
csp.KeyContainerName = "NetFrameworkConfigurationKey";
csp.KeyNumber = 1;
csp.ProviderType = 1;
// Create the new key, and save it in the key store
rsa = new RSACryptoServiceProvider(2048, csp);
rsa.PersistKeyInCsp = true;
rsa.Clear();
关于c# - 如何从代码中设置 RSAProtectedConfigurationProvider 的 key 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948202/