问题
This is a newbie question. I'm trying to load a .der certificate using:
X509Certificate2 cert = new X509Certificate2(@"c:\temp\mycert.der");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key
But I get a "The certificate key algorithm is not supported" error on the 2nd line. When I import this certificate to MMC I can see the public key like .
Is it valid? How do I get it in code?
回答1:
Prior to .NET 4.6.1 ECDSA keys were not supported. For legacy/compatibility reasons (such as your sample here where you're converting to an RSACryptoServiceProvider) the PublicKey.Key property and X509Certificate2.PrivateKey property still cannot ECDSA. There's instead a new, more type-safe, path:
using (ECDsa ecdsa = cert.GetECDsaPublicKey())
{
if (ecdsa != null)
{
// I had to do something with it in this example...
bool verified = ecdsa.VerifyData(data, signature, HashAlgorithmName.SHA256);
}
}
来源:https://stackoverflow.com/questions/32873851/load-a-certificate-using-x509certificate2-with-ecc-public-key