RSAProtectedConfigurationProvider provides two different methods. One of them is called AddKey , which can be used to create a key inside the container. If you mark a key as exportable, you can use the ExportKey method later to capture that key and save it somewhere else.
If you already have an existing key, you can use the ImportKey method. It will accept an XML block similar to the one coming out of ExportKey .
RSAProtectedConfigurationProvider uses the default container name NetFrameworkConfigurationKey if not specified. So, if you pre-create your key and add it to this container, the provider should pick it up when you use it.
// 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();
source share