2
我想知道证书的私钥是否存储在硬件设备中。
假设下面的应用找出没有设备的硬件设备上是否有私钥
class Program
{
static void Main(string[] args)
{
try
{
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 x509 in store.Certificates)
{
if (x509.HasPrivateKey)
{
AsymmetricAlgorithm a = x509.PrivateKey;
RSACryptoServiceProvider r = a as RSACryptoServiceProvider;
if (null != r)
{
System.Console.WriteLine("hardware: " + r.CspKeyContainerInfo.HardwareDevice);
System.Console.WriteLine("Subject: " + x509.Subject);
System.Console.WriteLine("container: " + r.CspKeyContainerInfo.KeyContainerName);
System.Console.WriteLine("---");
}
}
}
}
catch (CryptographicException ex)
{
Console.WriteLine("Information could not be written out for this certificate.");
}
}
}
我正在寻找的信息是r.CspKeyContainerInfo.HardwareDevice
。
但不幸的是,对于由基本智能卡csp提供的商店,一旦执行AsymmetricAlgorithm a = x509.PrivateKey
(如果smardcard当时不存在),系统会提示我插入设备。
有没有办法获得相同的信息没有这个烦人的“请插入智能卡”对话框弹出?