获取证书KeyContainerName等属性
Get certificate KeyContainerName and other attributes
我正在尝试使用自定义生成的证书签署 XML,该证书可通过此代码访问
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
}
我不知道如何正确使用X509Certificate2Collection scollection
为了填写以下属性。
我也没有得到以下想法:
如果证书存储在这里 My = 5
那么我使用
CspProviderFlags.UseMachineKeyStore
?
如何从 X509Certificate2Collection scollection
获取 KeyContainerName
?
最后,也许我获取填充 CspParameters
class 所需的证书属性的方式完全错误,有什么线索吗?
感谢您的帮助!
// Get the key pair from the key store.
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = ??? CspProviderFlags.UseMachineKeyStore; // Use Machine store
parms.KeyContainerName = ???; //
parms.KeyNumber = 2; // AT_SIGNATURE
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
这里是答案
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
foreach (X509Certificate2 cert in scollection)
{
var rsa = cert.PrivateKey as RSACryptoServiceProvider;
if (rsa == null) continue; // not smart card cert again
if (!string.IsNullOrEmpty(rsa.CspKeyContainerInfo.KeyContainerName))
{
// This is how we can get it! :)
var keyContainerName = rsa.CspKeyContainerInfo.KeyContainerName;
}
}
}
而且我们也正常使用CspProviderFlags.UseMachineKeyStore
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = CspProviderFlags.UseMachineKeyStore; // Use Machine store
我正在尝试使用自定义生成的证书签署 XML,该证书可通过此代码访问
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
}
我不知道如何正确使用X509Certificate2Collection scollection
为了填写以下属性。
我也没有得到以下想法:
如果证书存储在这里 My = 5
那么我使用
CspProviderFlags.UseMachineKeyStore
?
如何从 X509Certificate2Collection scollection
获取 KeyContainerName
?
最后,也许我获取填充 CspParameters
class 所需的证书属性的方式完全错误,有什么线索吗?
感谢您的帮助!
// Get the key pair from the key store.
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = ??? CspProviderFlags.UseMachineKeyStore; // Use Machine store
parms.KeyContainerName = ???; //
parms.KeyNumber = 2; // AT_SIGNATURE
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
这里是答案
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
foreach (X509Certificate2 cert in scollection)
{
var rsa = cert.PrivateKey as RSACryptoServiceProvider;
if (rsa == null) continue; // not smart card cert again
if (!string.IsNullOrEmpty(rsa.CspKeyContainerInfo.KeyContainerName))
{
// This is how we can get it! :)
var keyContainerName = rsa.CspKeyContainerInfo.KeyContainerName;
}
}
}
而且我们也正常使用CspProviderFlags.UseMachineKeyStore
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = CspProviderFlags.UseMachineKeyStore; // Use Machine store