On .NET Core (3.1), on both Windows and Linux platforms, I want to decrypt a message using the private keys of X509Certificiate2 instances. The certificates are retreived from certificate store on Windows platform, and from PFX file on Linux platforms respectively.
I wish to decrypt the message using something like:
static byte[] Decrypt(byte[] data, RSAParameters privateKey)
{
using (var rsa = RSA.Create())
{
rsa.ImportParameters(privateKey);
return rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256);
}
}
But when I try to extract the private key, I the an exception: "System.Security.Cryptography.CryptographicException: The requested operation is not supported."
var privateKey = x509cert.GetRSAPrivateKey();
var privateKeyParams = privateKey.ExportParameters(true); // <-- throws CryptographicException
What am I missing?