The version of .NET that I am using (4.5.1) has no GetCngPrivateKey() method in X509Certificate2. Nothing I have found works.
The .NET version I am using is, unfortunately, not negotiable.
I'm trying to pull something like this off:
X509Certificate2 cert = new X509Certificate2(@"alice.p12", "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
CngKey ck = CngKey.Import(cert.PrivateKey) // Fake line - not possible this way
using (var alice = new ECDiffieHellmanCng(ck))
{
// ...
}
Again, there are no methods available to me such as GetCngPrivateKey(), HasCngKey(), GetRSAPrivateKey(), and so on.
* UPDATE * All of these Import attempts fail with exceptions:
RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;
var cspBlob = rsa.ExportCspBlob(true);
CngKey a = CngKey.Import(cspBlob, CngKeyBlobFormat.EccPrivateBlob);
CngKey a = CngKey.Import(cspBlob, CngKeyBlobFormat.GenericPrivateBlob);
cspBlob = rsa.ExportCspBlob(false);
CngKey a = CngKey.Import(cspBlob, CngKeyBlobFormat.EccPrivateBlob);
CngKey a = CngKey.Import(cspBlob, CngKeyBlobFormat.GenericPrivateBlob);