I have created key pairs for elliptic curve DSA signatures using BouncyCastle and managed to import the public key into ECDsaCng using an XMLString accoding to RFC4050. Now I want to also move the private key and have not managed to find a solution. The closest I have got is using CngKey.Import.
CngKey.Import supports PKCS#8 format so if you can get your keys into valid pkcs8 then it should work. Unfortunately the following code does not quite work.
var privatekey = (ECPrivateKeyParameters) keyPair.Private;
var pkinfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privatekey);
byte[] pkcs8Blob = pkinfo.GetDerEncoded();
var importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob);
This throws an exception:
System.Security.Cryptography.CryptographicException: ASN1 bad tag value met.
GetDerEncoded should return a valid Pkcs8 blob as far as I can tell.
How can I use the private key created with BouncyCastle in a ECDsaCng object?