public static void ExportToP12(CngKey key, X509Certificate cert, string outputFile, string password, string name) { // Normalise the name string nName = name.Replace(' ', '_'); // Use the FIPS-140 system prng SecureRandom random = new SecureRandom(new CryptoApiRandomGenerator()); // Build PKCS#12 Pkcs12StoreBuilder p12 = new Pkcs12StoreBuilder(); Pkcs12Store pkcs = p12.Build(); byte[] privateKey = key.Export(CngKeyBlobFormat.EccPrivateBlob); ECPrivateKeyParameters privateParams = EccKeyBlob.UnpackEccPrivateBlob(privateKey); pkcs.SetKeyEntry(nName, new AsymmetricKeyEntry(privateParams), new X509CertificateEntry[] { new X509CertificateEntry(cert) }); Stream stream = new FileStream(outputFile, FileMode.Create); pkcs.Save(stream, password.ToCharArray(), random); stream.Close(); }
public static ECPublicKeyParameters getPublicKey(CngKey key) { return(EccKeyBlob.UnpackEccPublicBlob(key.Export(CngKeyBlobFormat.EccPublicBlob))); }