Load a Certificate Using X509Certificate2 with ECC Public Key

梦想与她 提交于 2019-12-24 04:56:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!