从ECC创建X509Certificate2 X509Certificate在C#中引发'System.NotSupportedException' - c#

我需要将ECC证书导入C#中的Windows密钥库中。第一步,我使用BouncyCastle生成EC密钥对,使用公钥创建X509证书,并使用ECDSA和私钥对它进行签名,即:

            var ecKeyPairGenerator = new ECKeyPairGenerator("ECDSA");
            ECKeyGenerationParameters ecKeyGenParams =
                new ECKeyGenerationParameters(SecObjectIdentifiers.SecP384r1, new SecureRandom());
            ecKeyPairGenerator.Init(ecKeyGenParams);
            AsymmetricCipherKeyPair pair = ecKeyPairGenerator.GenerateKeyPair();
            PrivateKeyInfo privKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
            SubjectPublicKeyInfo pubKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pair.Public);

            X509V3CertificateGenerator bcX509Gen = new X509V3CertificateGenerator();
// set cert fields
...
            bcX509Gen.SetPublicKey(pair.Public);
            Asn1SignatureFactory bcSigFactory =
                    new Asn1SignatureFactory(X9ObjectIdentifiers.ECDsaWithSha384.Id, pair.Private);
            X509Certificate bcCert = bcX509Gen.Generate(bcSigFactory);

然后,使用上面创建的证书创建一个X509Certificate2,即:

    SystemX509.X509Certificate2 msCert2 = 
        new SystemX509.X509Certificate2(bcCert.GetEncoded(), (string)null);

但是,在创建X509Certificate2时会引发异常:

'msCert2.PublicKey.Key' threw an exception of type 'System.NotSupportedException'
"The certificate key algorithm is not supported."

使用BC的DotNetUtilities.ToX509Certificate()会导致相同的异常。

我知道Windows / .NET上对ECC证书的支持可能不完整,但是我在网络上的搜索似乎暗示应该可以做到这一点?有什么想法我做错了吗?

仅供参考,我正在使用VS Community 2017,我的项目的目标是.NET Framework 4.6.2。

谢谢!

参考方案

PublicKey.Key被正式弃用(以及PrivateKey)。它不支持ECC,也不会产生能够进行OAEP-SHA-2加密的RSA密钥或能够进行FIPS 186-3 DSA的DSA密钥。

相反,您想使用不需要强制转换的扩展方法:

// GetECDsaPublicKey returns a unique object every call,
// so you're responsible for Disposing it (lest it end up on the Finalizer queue)
using (ECDsa ecdsa = msCert2.GetECDsaPublicKey())
{
    // do stuff with the public key object
}

无法将类型'System.ValueType'隐式转换为'int?' - c#

我有一个C ++ / CLI包装器类,可在C#和本机C ++之间互操作。我收到与System.Nullable相关的奇怪错误。我了解,对于基本类型,System.Nullable<T>等效于T?。所以我这样做:C#:public int? RoboticsArmRotation { get { return mRobotics.ArmRotati…

如何解析Dapper System.Data.DataException HResult = 0x80131501 InvalidCastException从'System.String'到'System.Uri'的无效转换 - c#

最近,我重构了一些代码,这些代码导致将实体的某些属性的类型从System.String更改为System.URI。有问题的属性名称包含子字符串URI或URL,并且SonarLint静态代码分析器建议将代码重构为对这些属性使用System.URI类型而不是System.String,这在我们的解决方案中很有意义。在这个项目中,我们利用StackExchange…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。