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();
                }
            }
        }
        private static unsafe void SetFriendlyNameExtendedProperty(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, string name)
        {
            SafeLocalAllocHandle handle = System.Security.Cryptography.X509Certificates.X509Utils.StringToUniPtr(name);

            using (handle)
            {
                CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = new CAPIBase.CRYPTOAPI_BLOB {
                    cbData = (uint)(2 * (name.Length + 1)),
                    pbData = handle.DangerousGetHandle()
                };
                if (!CAPI.CertSetCertificateContextProperty(safeCertContextHandle, 11, 0, new IntPtr((void *)&cryptoapi_blob)))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
        }