为什么自签名 PFX X509Certificate2 私钥会引发 NotSupportedException?
Why does self signed PFX X509Certificate2 private key raise a NotSupportedException?
我创建了一个自签名的 PFX X509Certificate2(使用 this answer),但由于某种原因,证书的私钥抛出一个 NotSupportedException 拒绝真正的 HasPrivateKey 属性.
string password = "MyPassword";
ECDsa ecdsa = ECDsa.Create();
CertificateRequest certificateRequest = new CertificateRequest("cn=foobar", ecdsa, HashAlgorithmName.SHA256);
X509Certificate2 cert = certificateRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5));
File.WriteAllBytes("e:\mycert.pfx", cert.Export(X509ContentType.Pfx, password));
//I tried to load the with every flag without success...
X509Certificate2 loadedCert = new X509Certificate2("e:\mycert.pfx", password);
if (loadedCert.HasPrivateKey)
{
//loadedCert.HasPrivateKey is true but loadedCert.PrivateKey raise a NotSupportedException...
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)loadedCert.PrivateKey)
{
byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes("Hello"), false);
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, false);
string result = Encoding.UTF8.GetString(decryptedBytes);
}
}
有人提到调用证书的导出可以修复私钥,但它对我不起作用。我可能遗漏了一些东西,但我不知道它可能是什么。某处是否缺少参数?
您正在创建 ECDSA 密钥对,而 X509Certificate2.PrivateKey
仅支持存储在传统加密服务提供商 (CSP) 中的 DSA 和 RSA 私钥。 ECDSA 始终存储在此 属性 不支持的密钥存储提供程序 (KSP) 中。相反,您必须使用 GetECDsaPrivateKey
扩展方法:GetECDsaPrivateKey(X509Certificate2)
public密钥加密有两种算法(RSA 和 ECC)。问题是您正在创建一个 ECC (I.E ECDsa),然后您试图将其作为 RSA 私钥获取。这绝对是不正确的。你在这里应该做的是在双方都使用一种算法。
2. 如果你只想加密然后解密一段数据,为什么要使用 X509Certificate2
,而是使用 AES。这是为了这个目的。
我创建了一个自签名的 PFX X509Certificate2(使用 this answer),但由于某种原因,证书的私钥抛出一个 NotSupportedException 拒绝真正的 HasPrivateKey 属性.
string password = "MyPassword";
ECDsa ecdsa = ECDsa.Create();
CertificateRequest certificateRequest = new CertificateRequest("cn=foobar", ecdsa, HashAlgorithmName.SHA256);
X509Certificate2 cert = certificateRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5));
File.WriteAllBytes("e:\mycert.pfx", cert.Export(X509ContentType.Pfx, password));
//I tried to load the with every flag without success...
X509Certificate2 loadedCert = new X509Certificate2("e:\mycert.pfx", password);
if (loadedCert.HasPrivateKey)
{
//loadedCert.HasPrivateKey is true but loadedCert.PrivateKey raise a NotSupportedException...
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)loadedCert.PrivateKey)
{
byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes("Hello"), false);
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, false);
string result = Encoding.UTF8.GetString(decryptedBytes);
}
}
有人提到调用证书的导出可以修复私钥,但它对我不起作用。我可能遗漏了一些东西,但我不知道它可能是什么。某处是否缺少参数?
您正在创建 ECDSA 密钥对,而 X509Certificate2.PrivateKey
仅支持存储在传统加密服务提供商 (CSP) 中的 DSA 和 RSA 私钥。 ECDSA 始终存储在此 属性 不支持的密钥存储提供程序 (KSP) 中。相反,您必须使用 GetECDsaPrivateKey
扩展方法:GetECDsaPrivateKey(X509Certificate2)
public密钥加密有两种算法(RSA 和 ECC)。问题是您正在创建一个 ECC (I.E ECDsa),然后您试图将其作为 RSA 私钥获取。这绝对是不正确的。你在这里应该做的是在双方都使用一种算法。
2. 如果你只想加密然后解密一段数据,为什么要使用 X509Certificate2
,而是使用 AES。这是为了这个目的。