I have (non-exportable) keys which have been created using RSACryptoServiceProvider
. I want to sign data using RSA-PSS (which is not RSACryptoServiceProvider
). Therefore I want to obtain the same private key as an RSACng
instance.
I tried the following:
// Create key with RSACryptoServiceProvider
var keyId = Guid.NewGuid().ToString();
var providerName = "Microsoft Enhanced RSA and AES Cryptographic Provider";
var key = new RSACryptoServiceProvider(2048, new CspParameters(24) {
ProviderName = providerName,
KeyContainerName = keyId,
KeyNumber = (int) KeyNumber.Signature,
Flags = CspProviderFlags.UseNonExportableKey
});
// Obtain an RSACng reference:
var cngKey = CngKey.Open(keyId, new CngProvider(providerName));
var cngRsaKey = new RSACng(cngKey);
// Sign something using cngRsaKey
[...]
Unfortunately, it always fails when performing CngKey.Open with WindowsCryptographicException: Keyset does not exist
.
How to open the previously created key with RSACng?
Note, that I cannot use the answer provided by https://stackoverflow.com/a/50703729/1400869 because I cannot use exportable private keys. In the end the keys should reside on an HSM (Hardware Security Module).
Any ideas?