设置X509Certificate2私钥时出错

编程入门 行业动态 更新时间:2024-10-23 03:25:40
本文介绍了设置X509Certificate2私钥时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将.NetFramework 4.6.1库迁移到.NetCore 2.2。 但是我无法设置x509certificate.PrivateKey,如下所示。

I am migrating a .NetFramework 4.6.1 library to a .NetCore 2.2. But i am unable to set x509certificate.PrivateKey as shown below.

我读到这可能是由于RSAServiceProvider引起的,但是我不知道如何设置属性。甚至实例化: x509certificate.PrivateKey = new RSACryptoServiceProvider(); 引发PlatformNotSupportedException。

I have read that may be due to the RSAServiceProvider but i am unaware how to set this property. Even instantiating: x509certificate.PrivateKey = new RSACryptoServiceProvider();throws the PlatformNotSupportedException.

// selfsign certificate Org.BouncyCastle.X509.X509Certificate certificate = certificateGenerator.Generate(signatureFactory); // correponding private key PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(subjectKeyPair.Private); // merge into X509Certificate2 var x509certificate = new X509Certificate2(certificate.GetEncoded()); Asn1Sequence seq = (Asn1Sequence) Asn1Object.FromByteArray(info.ParsePrivateKey().GetDerEncoded() ); RsaPrivateKeyStructure rsa = RsaPrivateKeyStructure.GetInstance(seq); RsaPrivateCrtKeyParameters rsaParams = new RsaPrivateCrtKeyParameters( rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent, rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2, rsa.Coefficient); x509certificate.PrivateKey = DotNetUtilities.ToRSA(rsaParams);

在.NetCore库中,将x509certificate.PrivateKey与DotNetUtilities.ToRSA(rsaParams)中的RSA一起设置PlatformNotSupportedException。

In the .NetCore library setting x509certificate.PrivateKey with the RSA from DotNetUtilities.ToRSA(rsaParams) throws an PlatformNotSupportedException.

System.PlatformNotSupportedException HResult=0x80131539 Message=Operation is not supported on this platform. Source=System.Security.Cryptography.X509Certificates StackTrace: at System.Security.Cryptography.X509Certificates.X509Certificate2.set_PrivateKey(AsymmetricAlgorithm value)

推荐答案

正如LexLi所说,内核中的设计无法在现有证书上设置私钥。

As LexLi said, setting the private key on an existing certificate is not possible by design in core.

按照此处所述,您可以做的是使用方法RSACertificateExtensions.CopyWithPrivateKey。

Following what is described here, what you can do is use the method RSACertificateExtensions.CopyWithPrivateKey.

而不是

x509certificate.PrivateKey = DotNetUtilities.ToRSA(rsaParams);

您可以拥有

var rsa = DotNetUtilities.ToRSA(rsaParams); var cert = x509certificate.CopyWithPrivateKey(rsa); return cert;

要访问 CopyWithPrivateKey扩展方法,请使用:

To get access to the "CopyWithPrivateKey" extension method, add this using :

using System.Security.Cryptography.X509Certificates; /* for getting access to extension methods in RSACertificateExtensions */

( CopyWithPrivateKey)将RSA证书的私钥和公钥组合在一起,以生成新的RSA证书。

"(CopyWithPrivateKey) Combines a private key with the public key of an RSA certificate to generate a new RSA certificate."

https:// docs.microsoft/zh-CN/dotnet/api/system.security.cryptography.x509certificates.rsacertificateextensions.copywithprivatekey?view=netcore-3.0

更多推荐

设置X509Certificate2私钥时出错

本文发布于:2023-11-05 08:23:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:私钥时

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!