本文整理汇总了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);
}
示例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;
}