Convert .der to .pem using OpenSSL-Net

戏子无情 提交于 2020-01-05 17:40:53

问题


I have a .der certificate that is binary encoded which needs to be converted to a .pem file programatically in .net

This line gives the correct output using OpenSSL on OSX:

openssl x509 -inform der -in cert.crt -out cert.pem

But we need to do the same in .net

We have tried many solutions but are completely stuck.

Would something like this work:

var oc = OpenSSL.X509.X509Certificate.FromDER(bio); 

Any advice very welcome :)


回答1:


In the end we were able to use this to import the DER into a string which we could then export to a PEM:

var oc = OpenSSL.X509.X509Certificate.FromDER(bio);

These pages were useful:

https://github.com/openssl-net/openssl-net/blob/master/ManagedOpenSsl/X509/X509Certificate.cs

https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2.rawdata

Thanks all for your help :)




回答2:


Still use OpenSSL? We are coming to you!

No, seriously, it is done in just 1 line:

String pem = "-----BEGIN CERTIFICATE-----\r\n" + Convert.ToBase64String(cert.RawData, InsertLineBreaks) + "-----END CERTIFICATE-----";

where cert is an X509Certificate2object.



来源:https://stackoverflow.com/questions/35437506/convert-der-to-pem-using-openssl-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!