如何使用C#从PKCS#12(.p12)文件获取私钥
我试图使用PKCS#12证书签署一些数据,但是我从PKCS#12(.p12)文件获取私钥时遇到问题。
public byte[] sign(string text)
{
string password = "1111";
X509Certificate2 cert = new X509Certificate2("c:\certificate.p12",password);
byte[] certData = cert.Export(X509ContentType.Pfx,password);
X509Certificate2 newCert = new X509Certificate2(certData, password);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)newCert.PrivateKey;
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
问题是newCert.PrivateKey为null但是如果我使用.pfx certicitae以类似的方式工作。
public byte[] sign(string text)
{
string password = "1234";
X509Certificate2 cert = new X509Certificate2("c:\certificate.pfx", password);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
所以问题是如何从.p12文件中获取私钥?
没有找到相关结果
已邀请:
4 个回复
弦砂牧扁
而不是
最后,如果可能,为什么不尝试将其导入certstore。我相信这更安全。步骤在上面的链接中。
屉杆绊
播匣扦阔食
不支持
类型,只有
,
和
。
勘掸府迫路