0

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);
ts90
  • 1
  • 2
  • According to MSDN, you can just use the property `PrivateKey` to get the value you want, the documentation says this works with `.Net 4.5.1` (https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.privatekey?view=netframework-4.7.2) – Ryan Wilson Jan 16 '19 at 15:47
  • @RyanWilson If so, I cannot figure it out. Some of the info in that article references a X509Certificate2Collection store which I have no idea what this even means. I have a p12 file that I load using the X509Certificate2() method and that much works. But I cannot seem to figure out how to bridge the gap between that and CngKey or ECDiffieHellmanCng. – ts90 Jan 16 '19 at 15:56
  • I think you are trying to pass the wrong thing into `CngKey.Import`, (https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cngkey.import?view=netframework-4.7.2), according to the documentation, there are two versions of `CngKey.Import`, `Import(Byte[], CngKeyBlobFormat, CngProvider)` and `Import(Byte[], CngKeyBlobFormat)` – Ryan Wilson Jan 16 '19 at 16:08

0 Answers0