我尝试签名Xml文件。这是代码(来自MSDN):
RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
SignXmlFile(XmlStart, XmlEnd, Key);
如何以密钥形式发送X509Certificate2? 坦克! 弗朗西斯
答案 0 :(得分:0)
在这里,您将使用证书中的密钥(公钥/私钥)。
选项1)
X509Certificate2 cert = RetrieveCertificate("abcd");
var key = cert.PrivateKey;
private static X509Certificate2 RetrieveCertificateFromStore(string certificateName)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly);
var cert = store.Certificates.OfType<X509Certificate2>().AsEnumerable().FirstOrDefault(c => c.FriendlyName == certificateName);
return cert;
}
从文件中检索证书:
private static X509Certificate2 RetrieveCertificateFromFile(string certPath)
{
// string certPath = @"C:\Certificates\myCert.pfx";
string certPass = "mycertPass";
// Create a collection object and populate it using the PFX file
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
// Instead of foreach you can directly retrieve the certificate from collection as well.
foreach (X509Certificate2 cert in collection)
{
// Import the certificates into X509Store objects
return cert;
}
return null;
}
选项2)
RSACryptoServiceProvider key = RetrieveKey(cert, EnumKeyType.Private);