private static void SetPrivateKeyProperty(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, ICspAsymmetricAlgorithm asymmetricAlgorithm)
        {
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;

            if (asymmetricAlgorithm != null)
            {
                CAPIBase.CRYPT_KEY_PROV_INFO structure = new CAPIBase.CRYPT_KEY_PROV_INFO {
                    pwszContainerName = asymmetricAlgorithm.CspKeyContainerInfo.KeyContainerName,
                    pwszProvName      = asymmetricAlgorithm.CspKeyContainerInfo.ProviderName,
                    dwProvType        = (uint)asymmetricAlgorithm.CspKeyContainerInfo.ProviderType,
                    dwFlags           = asymmetricAlgorithm.CspKeyContainerInfo.MachineKeyStore ? 0x20 : 0,
                    cProvParam        = 0,
                    rgProvParam       = IntPtr.Zero,
                    dwKeySpec         = (uint)asymmetricAlgorithm.CspKeyContainerInfo.KeyNumber
                };
                invalidHandle = CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(CAPIBase.CRYPT_KEY_PROV_INFO))));
                Marshal.StructureToPtr(structure, invalidHandle.DangerousGetHandle(), false);
            }
            try
            {
                if (!CAPI.CertSetCertificateContextProperty(safeCertContextHandle, 2, 0, invalidHandle))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (!invalidHandle.IsInvalid)
                {
                    Marshal.DestroyStructure(invalidHandle.DangerousGetHandle(), typeof(CAPIBase.CRYPT_KEY_PROV_INFO));
                    invalidHandle.Dispose();
                }
            }
        }
        internal static bool GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters)
        {
            SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;
            uint pcbData = 0;

            if (!CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData))
            {
                if (Marshal.GetLastWin32Error() != -2146885628)
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                return(false);
            }
            invalidHandle = CAPI.LocalAlloc(0, new IntPtr((long)pcbData));
            if (!CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData))
            {
                if (Marshal.GetLastWin32Error() != -2146885628)
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                return(false);
            }
            CAPIBase.CRYPT_KEY_PROV_INFO crypt_key_prov_info = (CAPIBase.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(CAPIBase.CRYPT_KEY_PROV_INFO));
            parameters.ProviderName     = crypt_key_prov_info.pwszProvName;
            parameters.KeyContainerName = crypt_key_prov_info.pwszContainerName;
            parameters.ProviderType     = (int)crypt_key_prov_info.dwProvType;
            parameters.KeyNumber        = (int)crypt_key_prov_info.dwKeySpec;
            parameters.Flags            = ((crypt_key_prov_info.dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags;
            invalidHandle.Dispose();
            return(true);
        }