Exemplo n.º 1
0
        /// <summary>
        /// The get user key.
        /// </summary>
        /// <param name="keySpec">
        /// The key spec.
        /// </param>
        /// <returns>
        /// The <see cref="KeyContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private KeyContext GetUserKey(int keySpec = 0)
        {
            var keyPiarHandler = IntPtr.Zero;

            if (!CryptoApi.CryptGetUserKey(cspHandler, keySpec, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);

            return(keyPairContext);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The generate random key.
        /// </summary>
        /// <param name="keyNumber">
        /// The key number.
        /// </param>
        /// <param name="flags">
        /// The flags.
        /// </param>
        /// <returns>
        /// The <see cref="KeyContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private KeyContext GenerateRandomKey(KeyNumber keyNumber, int flags = 0)
        {
            var keyPiarHandler = IntPtr.Zero;

            if (!CryptoApi.CryptGenKey(cspHandler, (int)keyNumber, flags, ref keyPiarHandler))
            {
                throw new Win32Exception();
            }

            var keyPairContext = new KeyContext(keyPiarHandler);

            return(keyPairContext);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The create hash.
        /// </summary>
        /// <param name="keyContext">
        /// The key context.
        /// </param>
        /// <param name="algid">
        /// The algid.
        /// </param>
        /// <param name="flags">
        /// The flags.
        /// </param>
        /// <returns>
        /// The <see cref="HashContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private HashContext CreateHash(KeyContext keyContext, int algid, int flags)
        {
            var hashHandler = IntPtr.Zero;
            var keyHandler  = IntPtr.Zero;

            if (keyContext != null)
            {
                keyHandler = keyContext.Handler;
            }

            if (!CryptoApi.CryptCreateHash(cspHandler, algid, keyHandler, flags, ref hashHandler))
            {
                throw new Win32Exception();
            }

            var hashContext = new HashContext(hashHandler);

            return(hashContext);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The import key.
        /// </summary>
        /// <param name="protectionKeyContext">
        /// The protection key context.
        /// </param>
        /// <param name="keyData">
        /// The key data.
        /// </param>
        /// <param name="keyDataLength">
        /// The key data length.
        /// </param>
        /// <param name="flags">
        /// The flags.
        /// </param>
        /// <returns>
        /// The <see cref="KeyContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private KeyContext ImportKey(KeyContext protectionKeyContext, byte[] keyData, int keyDataLength, int flags)
        {
            var protectionKeyHandler = IntPtr.Zero;

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

            var keyHandler = IntPtr.Zero;

            if (!CryptoApi.CryptImportKey(cspHandler, keyData, keyDataLength, protectionKeyHandler, flags, ref keyHandler))
            {
                throw new Win32Exception();
            }

            var keyContext = new KeyContext(keyHandler);

            return(keyContext);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Экспорт открытого ключа.
        /// </summary>
        /// <param name="context">
        /// Контекст ключей.
        /// </param>
        /// <returns>
        /// Открытый ключ.
        /// </returns>
        public byte[] ExportPublicKey(KeyContext context = null)
        {
            var result = ExportKey(context, Constants.PublicKeyBlob, 0);

            return(result);
        }