简体   繁体   English

如何在C#中导入PKCS#8 RSA私钥(由OpenSSL创建)

[英]How to import PKCS#8 RSA privateKey (created by OpenSSL) in C#

I'm trying to find a way to read a privateKey created using OpenSSL PKCS#8 RSA in C# without use external library. 我试图找到一种无需使用外部库即可读取使用C#中的OpenSSL PKCS#8 RSA创建的privateKey的方法。

Does Someone know how i can do this? 有人知道我该怎么做吗?

The easiest way to do this with an external library, is using the (free) Chillkat Public / Private Key Component : using that, importing the key can be done using just a few lines of code and if you're willing to pay the $149 or so for the rest of the library, it will make dealing with general crypto concepts a lot easier as well. 使用外部库执行此操作的最简单方法是使用(免费的) Chillkat公钥/私钥组件 :使用该组件 ,导入密钥就可以使用几行代码来完成,如果您愿意支付149美元,大约对于库的其余部分,这也将使处理一般的加密概念也变得容易得多。

And unlike the regular Microsoft .NET Framework, the Mono project does seem to have a PKCS8 implementation for which the full C# source is available. 而且与常规的Microsoft .NET Framework不同,Mono项目似乎确实具有PKCS8实现 ,可以使用其完整的C#源代码 This may be suitable as a starting point in case you absolutely cannot rely on external libraries, assuming the (LGPL 2.0) license associated with the Mono code works for you... 如果您绝对不能依赖外部库,这可能是一个起点,假设与Mono代码相关的(LGPL 2.0)许可证对您有效。

Finally, the PKCS #8 format is not too difficult to parse, and the RSA/DSA key pair objects are as per PKCS #11 and relatively easy to convert to a .NET X509Certificate once you figure out where all the bits go -- I actually did this in VB.NET a while ago, but unfortunately am not able to share that code. 最后, PKCS#8格式并不是很难解析,并且RSA / DSA密钥对对象按照PKCS#11 ,一旦确定所有位在哪里,相对容易转换为.NET X509证书-我确实是在VB.NET中这样做的,但是不幸的是无法共享该代码。

Thanks for your answer. 感谢您的回答。

My script to create RSA key i used OpenSSL whit: 我创建RSA密钥的脚本我使用了OpenSSL whit:

(Linux Script) (Linux脚本)

openssl genrsa -out ${NAME}_openssl.key 2048
openssl pkcs8 -topk8 -in ${NAME}_openssl.key -nocrypt > ${NAME}.key
openssl req -new -x509 -key ${NAME}.key -out ${NAME}.crt -outform DER

In C# we need privateKey in XML format. 在C#中,我们需要XML格式的privateKey。 I used this parser to do this. 我用这个解析器来做到这一点。

To decrypt de challenge we need to use: 要解密挑战,我们需要使用:

  byte[] challange = server.getChallenge();

  RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();

  rsaProvider.FromXmlString(Demo.Properties.Resources.XmlPrivateKey);

  byte[] plaintext = rsaProvider.Decrypt(challange, false);

To encrypt whit server certificate we need to use: 要加密白色服务器证书,我们需要使用:

  RSACryptoServiceProvider rsaProvider = x509.PublicKey.Key as RSACryptoServiceProvider;

  byte[] answer = RsaProvider.Encrypt(plaintext, false);

Thanks for JavaScience Consulting 感谢JavaScience Consulting

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

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