C# 设置X509Certificate2私钥时出错

C# 设置X509Certificate2私钥时出错,c#,.net-core,C#,.net Core,我正在将.NetFramework 4.6.1库迁移到.NetCore 2.2。 但我无法设置x509certificate.PrivateKey,如下所示 我已经读到这可能是由于RSAServiceProvider,但我不知道如何设置此属性。甚至实例化:x509certificate.PrivateKey=new RSACryptoServiceProvider()抛出PlatformNotSupportedException // selfsign certificate Org.Bounc

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

我已经读到这可能是由于RSAServiceProvider,但我不知道如何设置此属性。甚至实例化:
x509certificate.PrivateKey=new RSACryptoServiceProvider()
抛出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);
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)
在.NetCore库设置x509certificate.PrivateKey中,使用DotNetUtilities.ToRSA(rsaparms)中的RSA抛出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);
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所说,通过.NETCore中的设计,在现有证书上设置私钥是不可能的

按照描述,您可以使用RSACertificateExtensions.CopyWithPrivateKey方法

而不是

x509certificate.PrivateKey = DotNetUtilities.ToRSA(rsaParams);
你本来可以的

var rsa = DotNetUtilities.ToRSA(rsaParams);
var cert = x509certificate.CopyWithPrivateKey(rsa);
return cert;
要访问“CopyWithPrivateKey”扩展方法,请使用以下方法添加此方法:

using System.Security.Cryptography.X509Certificates; /* for getting access to extension methods in RSACertificateExtensions */
“(CopyWithPrivateKey)将RSA证书的私钥与公钥组合以 生成新的RSA证书。“


正如LexLi所说,在现有证书上设置私钥在.net core中是不可能的

按照描述,您可以使用RSACertificateExtensions.CopyWithPrivateKey方法

而不是

x509certificate.PrivateKey = DotNetUtilities.ToRSA(rsaParams);
你本来可以的

var rsa = DotNetUtilities.ToRSA(rsaParams);
var cert = x509certificate.CopyWithPrivateKey(rsa);
return cert;
要访问“CopyWithPrivateKey”扩展方法,请使用以下方法添加此方法:

using System.Security.Cryptography.X509Certificates; /* for getting access to extension methods in RSACertificateExtensions */
“(CopyWithPrivateKey)将RSA证书的私钥与公钥组合以 生成新的RSA证书。“


提供的解决方案对我没有帮助,因此我将把此解决方案留在这里,希望它能帮助下一个遇到此问题的人

使用CertBuilder().ConvertBouncyCert,可以将BouncyCastle X509Certificate转换为嵌入公钥/私钥的X509Certificate2

X509Certificate2 _x509certificate2 = new CertBuilder().ConvertBouncyCert(_bouncyCertificate, subjectKeyPair);
我在其中使用的完整示例(基于这里提供的答案:)

公共静态X509Certificate2创建自签名CertificateBasedOnCertificateAuthoritypPrivateKey(字符串ipAddress、字符串issuerName、AsymmetricKeyParameter issuerPrivKey)
{
const int键强度=4096;
//生成随机数
CryptoApiranomGenerator randomGenerator=新CryptoApiranomGenerator();
SecureRandom random=新的SecureRandom(随机生成器);
ISignatureFactory signatureFactory=新的ASN1 signatureFactory(“SHA512 with RSA”,issuerPrivKey,随机);
//证书生成器
X509V3CertificateGenerator certificateGenerator=新X509V3CertificateGenerator();
certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage,true,new ExtendedKeyUsage((新列表(){new DerObjectIdentifier(“1.3.6.1.5.5.7.3.1”)),new DerObjectIdentifier(“1.3.6.1.5.5.7.3.2”));
//序列号
BigInteger serialNumber=BigIntegers.CreateRandomInRange(BigInteger.One,BigInteger.ValueOf(Int64.MaxValue),random);
证书生成器。设置序列号(序列号);
//发行人和主题名称
X509Name subjectDN=新的X509Name(“CN=”+ipAddress);
X509Name issuerDN=新的X509Name(issuerName);
certificateGenerator.SetIssuerDN(发行人N);
certificateGenerator.SetSubjectDN(subjectDN);
//适用于
DateTime notBefore=DateTime.UtcNow.Date;
DateTime notAfter=notBefore.AddYears(2);
证书生成器。SetNotBefore(notBefore);
证书生成器。SetNotAfter(notAfter);
//主题公钥
非对称密码密钥对主体密钥对;
var keyGenerationParameters=新的keyGenerationParameters(随机、keyStrength);
var keyPairGenerator=new rsakypairgenerator();
keyPairGenerator.Init(keyGenerationParameters);
subjectKeyPair=keyPairGenerator.GenerateKeyPair();
certificateGenerator.SetPublicKey(subjectKeyPair.Public);
GeneralNames subjectAltName=新的GeneralName(新的GeneralName(GeneralName.IPAddress,IPAddress));
certificateGenerator.AddExtension(X509Extensions.SubjectAlternativeName,false,SubjectAlterName);
//自签名证书
Org.BouncyCastle.X509.X509Certificate certificate=certificateGenerator.Generate(signatureFactory);
X509Certificate2 certificate2=新的CertBuilder().ConvertBouncyCert(证书,subjectKeyPair);
返回证书2;
}
使用的nuget软件包:

  • Oren Novotny版本1.8.5的Portable.BouncyCastle
  • Jermy Peterson的CryptLink.CertBuilder版本1.1.0

提供的解决方案对我没有帮助,因此我将把此解决方案留在这里,希望它能帮助下一个遇到此问题的人

使用CertBuilder().ConvertBouncyCert,可以将BouncyCastle X509Certificate转换为嵌入公钥/私钥的X509Certificate2

X509Certificate2 _x509certificate2 = new CertBuilder().ConvertBouncyCert(_bouncyCertificate, subjectKeyPair);
我在其中使用的完整示例(基于这里提供的答案:)

公共静态X509Certificate2创建自签名CertificateBasedOnCertificateAuthoritypPrivateKey(字符串ipAddress、字符串issuerName、AsymmetricKeyParameter issuerPrivKey)
{
const int键强度=4096;
//生成随机数
CryptoApiranomGenerator randomGenerator=新CryptoApiranomGenerator();
SecureRandom random=新的SecureRandom(随机生成器);
ISignatureFactory signatureFactory=新的ASN1 signatureFactory(“SHA512 with RSA”,issuerPrivKey,随机);
//证书生成器
X509V3CertificateGenerator certificateGenerator=新X509V3CertificateGenerator();
certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage,true,new ExtendedKeyUsage((新列表(){new DerObjectIdentifier(“1.3.6.1.5.5.7.3.1”)),new DerObjectIdentifier(“1.3.6.1.5.5.7.3.2”));
//序列号
BigInteger serialNumber=BigIntegers.CreateRandomInRange(BigInteger.One,BigInteger.ValueOf(Int64.MaxV