Пример #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);
Пример #2
0
 internal static extern ErrorCode BCryptImportKey(
     SafeBCryptAlgorithmHandle hAlgorithm,
     IntPtr hImportKey,
     string pszBlobType,
     out SafeBCryptKeyHandle hKey,
     IntPtr pbKeyObject,
     int cbKeyObject,
     byte[] pbInput,
     int cbInput,
     int dwFlags);
Пример #3
0
 public static extern unsafe ErrorCode BCryptDecrypt(
     SafeBCryptKeyHandle hKey,
     byte *pbInput,
     int cbInput,
     IntPtr paddingInfo,
     [In, Out] byte[] pbIV,
     int cbIV,
     byte *pbOutput,
     int cbOutput,
     out int cbResult,
     int dwFlags);
Пример #4
0
        public static int BCryptDecrypt(
            SafeBCryptKeyHandle hKey,
            byte[] input,
            int inputOffset,
            int inputCount,
            byte[] iv,
            byte[] output,
            int outputOffset,
            int outputCount)
        {
            Debug.Assert(input != null);
            Debug.Assert(inputOffset >= 0);
            Debug.Assert(inputCount >= 0);
            Debug.Assert(inputCount <= input.Length - inputOffset);
            Debug.Assert(output != null);
            Debug.Assert(outputOffset >= 0);
            Debug.Assert(outputCount >= 0);
            Debug.Assert(outputCount <= output.Length - outputOffset);

            unsafe
            {
                fixed(byte *pbInput = input)
                {
                    fixed(byte *pbOutput = output)
                    {
                        int       cbResult;
                        ErrorCode error = UnsafeNativeMethods.BCryptDecrypt(
                            hKey,
                            pbInput inputOffset,
                            inputCount,
                            IntPtr.Zero,
                            iv,
                            iv == null ? 0 : iv.Length,
                            pbOutput outputOffset,
                            outputCount,
                            out cbResult,
                            0);

                        if (error != ErrorCode.Success)
                        {
                            throw new CryptographicException((int)error);
                        }

                        return(cbResult);
                    }
                }
            }
        }
Пример #5
0
        internal static SafeBCryptKeyHandle ImportAsymmetricPublicKey(X509Native.CERT_PUBLIC_KEY_INFO certPublicKeyInfo, int dwFlag)
        {
            SafeBCryptKeyHandle keyHandle = null;
            int error = UnsafeNativeMethods.CryptImportPublicKeyInfoEx2(
                X509Native.X509_ASN_ENCODING,
                ref certPublicKeyInfo,
                dwFlag,
                IntPtr.Zero,
                out keyHandle);

            if (error == 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(keyHandle);
        }
Пример #6
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);
        }
Пример #7
0
 internal static extern int CryptImportPublicKeyInfoEx2([In] uint dwCertEncodingType,
                                                        [In] ref X509Native.CERT_PUBLIC_KEY_INFO pInfo,
                                                        [In] int dwFlags,
                                                        [In] IntPtr pvAuxInfo,
                                                        [Out] out SafeBCryptKeyHandle phKey);
Пример #8
0
 internal static extern ErrorCode BCryptGetProperty(SafeBCryptKeyHandle hObject,
                                                    string pszProperty,
                                                    [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
                                                    int cbOutput,
                                                    [Out] out int pcbResult,
                                                    int flags);