当前位置: 首页>>代码示例>>C#>>正文


C# X509Certificate2.GetCngPrivateKey方法代码示例

本文整理汇总了C#中System.Security.Cryptography.X509Certificates.X509Certificate2.GetCngPrivateKey方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.GetCngPrivateKey方法的具体用法?C# X509Certificate2.GetCngPrivateKey怎么用?C# X509Certificate2.GetCngPrivateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.X509Certificates.X509Certificate2的用法示例。


在下文中一共展示了X509Certificate2.GetCngPrivateKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParsePrivateCertificate

        /// <summary>
        /// Parses the certificate to get access to the underlying ECDsaCng implementation
        /// Requires the private key so that the resulting ECDsaCng can sign
        /// </summary>
        /// <param name="certificate">A certificate from a file or store</param>
        /// <returns>ECDsaCng that can sign AND verify data</returns>
        public static ECDsaCng ParsePrivateCertificate(X509Certificate2 certificate)
        {
            // Get the ECDSA private key (needs CngKey lib)
            var privateKey = certificate.GetCngPrivateKey();
            if (privateKey == null)
            {
                throw new InvalidOperationException("Certificate does not contain a private key, or is not in the right format");
            }

            return new ECDsaCng(privateKey);
        }
开发者ID:KalixHealth,项目名称:Kalix.ApiCrypto,代码行数:17,代码来源:ECDSACertificateParser.cs

示例2: Execute

        public int Execute()
        {
            if (false == File.Exists(Path))
            {
                Console.WriteLine("The file " + Path + " could not be located");
                WriteHelp(Console.Error);
                return 1;
            }
            var cert = new X509Certificate2();
            cert.Import(ReadFully(File.OpenRead(Path)), Password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

            var privateKey = cert.GetCngPrivateKey();
            if (null == privateKey)
            {
                Console.WriteLine("No private key found in pfx");
                return 2;
            }

            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);

            store.Add(cert);
            store.Close();

            GrantAccess(privateKey);
            Console.WriteLine("Imported key successfully");
            Console.WriteLine("\t algorithm: " + privateKey.Algorithm);
            Console.WriteLine("\t keysize: " + privateKey.KeySize);
            Console.WriteLine("\t usages: " + privateKey.KeyUsage);
            Console.WriteLine("\t uniquename: " + privateKey.UniqueName);
            return 0;
        }
开发者ID:holytshirt,项目名称:Jwt4Net,代码行数:32,代码来源:ImportPfx.cs


注:本文中的System.Security.Cryptography.X509Certificates.X509Certificate2.GetCngPrivateKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。