internal static extern ErrorCode BCryptExportKey([In] SafeBCryptKeyHandle hKey, [In] IntPtr hExportKey, [In][MarshalAs(UnmanagedType.LPWStr)] string pszBlobType, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbOutput, [In] int cbOutput, [In] ref int pcbResult, [In] int dwFlags);
internal static byte[] ExportBCryptKey(SafeBCryptKeyHandle hKey, string blobType) { byte[] keyBlob = null; int length = 0; ErrorCode error = UnsafeNativeMethods.BCryptExportKey(hKey, IntPtr.Zero, blobType, null, 0, ref length, 0); if (error != ErrorCode.BufferToSmall && error != ErrorCode.Success) { throw new CryptographicException(Marshal.GetLastWin32Error()); } keyBlob = new byte[length]; error = UnsafeNativeMethods.BCryptExportKey(hKey, IntPtr.Zero, blobType, keyBlob, length, ref length, 0); if (error != ErrorCode.Success) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(keyBlob); }