未实现System.Security.Cryptography.X509Certificates.X509Certificate2.GetECDsaPublicKey

问题描述

我们已经在.Net Core Web api中使用.pfx证书文件上的X509Certificate2.GetECDsaPrivateKey创建了签名,并且需要使用公共密钥(.crt)在我们的Xamarin Android应用中验证该签名。我可以在正常的Windows .Net Framework 4.6.1应用程序上运行代码,并且可以正常运行。如果我将Framework版本更改为更低的版本,则我的应用程序将无法编译,因为GetECDsaPublicKey方法仅在4.6.1中实现。在我的Xamarin应用程序中,我可以编译该应用程序并使其运行,但是,当我到达执行GetECDsaPublicKey方法的行时,就会收到NotImplemented异常。除了Microsoft文档外,互联网上实际上没有关于此的任何信息。任何人都可以阐明发生此错误的原因吗?以下是几行代码,用于加载证书和验证签名(我已取出一些不必要的错误检查代码):

string FileName = "public_key.crt";
string certPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),FileName);
System.Security.Cryptography.X509Certificates.X509Certificate2 certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certPath);
System.Security.Cryptography.ECDsa eCDsa = certificate.GetECDsaPublicKey(); // This line causes the exception

即使只是执行此行,也会出现相同的错误

System.Security.Cryptography.ECDsa eCDsa = System.Security.Cryptography.ECDsa.Create();

解决方法

因此,如果有人遇到这个问题也很挣扎,那么有一篇帖子可以回答这个问题-Are ECDSA and ECDH available for mono?

遗憾的是,我们将不得不使用其他加密/签名算法或使用类似BouncyCastle的东西...