Exemple #1
0
        /// <summary>
        /// The export key.
        /// </summary>
        /// <param name="protectionKeyContext">
        /// The protection key context.
        /// </param>
        /// <param name="blobType">
        /// The blob type.
        /// </param>
        /// <param name="falgs">
        /// The falgs.
        /// </param>
        /// <returns>
        /// The <see cref="byte[]"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private byte[] ExportKey(KeyContext protectionKeyContext, int blobType, int falgs)
        {
            var protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            var exportBufferSize = 0;

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, null, ref exportBufferSize))
            {
                throw new Win32Exception();
            }

            var result = new byte[exportBufferSize];

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, result, ref exportBufferSize))
            {
                throw new Win32Exception();
            }

            return(result);
        }