Exception exporting an EC private key from keystore

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Exception exporting an EC private key from keystore

This post was updated on .

I have an ECDSA pair keys stored from a PKCS#12 file.
I am trying to get them using .NET through CngKey class:

  CngKey key = CngKey.Open(container);
  byte[] ecPubKey = key.Export(CngKeyBlobFormat.EccPublicBlob);
  byte[] ecPriKey = key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob);

I have no trouble with the public key. But I have this exception with the private one:

{"The requested operation is not supported.\r\n"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: 0x80090029
    HelpLink: null
    InnerException: null
    Message: "The requested operation is not supported.\r\n"
    Source: "System.Core"
    StackTrace: "  
      at System.Security.Cryptography.NCryptNative.ExportKey(SafeNCryptKeyHandle key, String format)
      at System.Security.Cryptography.CngKey.Export(CngKeyBlobFormat format)..."
    TargetSite: {Byte[] ExportKey(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle, System.String)}

I have seen the key has the parameter ExportPolicy: AllowExport although it was created as ExportPolicy: AllowPlainTextExport.
I think my trouble is because of this.

I was wondering if I can open the keystore and get the keys using BouncyCastle library instead of CngKey class. Could someone tell me if this is possible, please?

Thanks in advance!