简体   繁体   English

如何将 BouncyCastle X509Certificate 转换为 X509Certificate2?

[英]How can I convert a BouncyCastle X509Certificate to an X509Certificate2?

Is there any way to convert a Org.BouncyCastle.X509.X509Certificate to System.Security.Cryptography.X509Certificates.X509Certificate2 ?有没有办法将Org.BouncyCastle.X509.X509Certificate转换为System.Security.Cryptography.X509Certificates.X509Certificate2

The inverse operation is easy, combining Org.BouncyCastle.X509.X509CertificateParser with System.Security.Cryptography.X509Certificates.X509Certificate2.Export() .逆操作很简单,将Org.BouncyCastle.X509.X509CertificateParserSystem.Security.Cryptography.X509Certificates.X509Certificate2.Export()结合起来。

Easy!!简单!!

using B = Org.BouncyCastle.X509; //Bouncy certificates
using W = System.Security.Cryptography.X509Certificates;

W.X509Certificate2 certificate = new W.X509Certificate2(pdfCertificate.GetEncoded());

And now I can validate certificate chain in the server:现在我可以验证服务器中的证书链:

W.X509Chain ch = new W.X509Chain();
ch.ChainPolicy.RevocationMode = W.X509RevocationMode.NoCheck;
if (!ch.Build(certificate))
   res |= ErroresValidacion.CAInvalida; 

Useful to validate pdf certifcates extracted with iTextSharp.用于验证使用 iTextSharp 提取的 pdf 证书。

From https://github.com/dotnet/corefx/wiki/ApiCompat :https://github.com/dotnet/corefx/wiki/ApiCompat

Most users of X509Certificate and X509Certificate2 objects assume that the object is immutable except for the Reset()/Dispose() methods, the use of Import violates this assumption. X509Certificate 和 X509Certificate2 对象的大多数用户都假设该对象是不可变的,除了 Reset()/Dispose() 方法,Import 的使用违反了这个假设。

In other words, trying to use import throws an exception in .net core.换句话说,尝试使用 import 会在 .net core 中引发异常。 You should now use:你现在应该使用:

new X509Certificate(cert.GetEncoded());

but, according to the .net API analyzer ( https://docs.microsoft.com/en-us/dotnet/standard/analyzers/api-analyzer ),但是,根据 .net API 分析器( https://docs.microsoft.com/en-us/dotnet/standard/analyzers/api-analyzer ),

warning PC001: X509Certificate2.X509Certificate2(byte[]) isn't supported on macOS警告 PC001:X509Certificate2.X509Certificate2(byte[]) 在 macOS 上不受支持

I guess that is the best answer:我想这是最好的答案:

var cert = pdf.Certificates[0];//Org.BouncyCastle.X509.X509Certificate
        var cert50 = new X509Certificate();
        cert50.Import(cert.GetEncoded());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM