Esempio n. 1
0
 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);
Esempio n. 2
0
        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);
        }