Kyr*_*o M 6 c# security rsa web-config
我的应用程序有可能保护所选的配置文件.这是使用SectionInformation.ProtectSection加载的指定部分的方法完成的Configuration.我正在使用标准提供商RsaProtectedConfigurationProvider.
有没有办法设置提供商应该使用的密钥大小?据我所知,RSA的默认值是1024.我需要将其设置为2048或更大.
当我们使用asp_regiis.exe时,可以使用命令行选项-size完成类似的操作.但我需要从代码中做到这一点.也许有任何方法可以配置或预先创建密钥并以某种方式将其注入默认密钥存储区,以便下次使用它将赶上它...RsaProtectedConfigurationProviderSectionInformation.ProtectSection
感谢您的任何建议或示例.
RSAProtectedConfigurationProvider提供了两种不同的方法。一个调用AddKey可用于在容器内创建密钥。如果您将密钥标记为可导出,您可以ExportKey稍后使用方法来获取该密钥并将其存储在其他地方。
如果您已有密钥,则可以使用该ImportKey方法。它将接受一个 XML blob,就像从ExportKey.
RsaProtectedConfigurationProvider如果未提供,则使用默认容器名称NetFrameworkConfigurationKey 。因此,如果您预先创建密钥并将其添加到该容器中,那么提供商应该在您使用它时拾取它。
// 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();
Run Code Online (Sandbox Code Playgroud)