如何在 C# 中导入 PKCS#8 RSA 私钥(由 OpenSSL 创建)
我试图找到一种方法来读取在 C# 中使用 OpenSSL PKCS#8 RSA 创建的私钥,而不使用外部库。
有人知道我该怎么做吗?
I'm trying to find a way to read a privateKey created using OpenSSL PKCS#8 RSA in C# without use external library.
Does Someone know how i can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 .Net Core 3 开始:
供参考:https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importpkcs8privatekey?view=net-5.0
As of .Net Core 3:
For reference: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importpkcs8privatekey?view=net-5.0
使用外部库执行此操作的最简单方法是使用(免费)Chillkat 公钥/私钥组件:使用该组件,可以使用 只需几行代码,如果您愿意为该库的其余部分支付 149 美元左右,它也将使处理一般加密概念变得更加容易。
与常规的 Microsoft .NET Framework 不同,Mono 项目确实似乎有一个PKCS8 实现,其中 完整的 C# 源代码可用。如果您绝对不能依赖外部库,假设与 Mono 代码关联的 (LGPL 2.0) 许可证适合您,这可能适合作为起点...
最后, PKCS #8 格式 解析起来并不太困难,RSA/DSA 密钥对对象按照 < a href="ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-30/pkcs-11v2-30m1-d7.pdf" rel="nofollow noreferrer">PKCS #11一旦你弄清楚所有位的去向,就可以相对容易地转换为 .NET X509Certificate ——我实际上不久前在 VB.NET 中做到了这一点,但不幸的是我无法共享该代码。
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.
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. 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...
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.
感谢您的回答。
我使用 OpenSSL 来创建 RSA 密钥的脚本:
(Linux 脚本)
在 C# 中,我们需要 XML 格式的私钥。我使用 this 解析器来执行此操作。
要解密挑战,我们需要使用:
要加密服务器证书,我们需要使用:
感谢 JavaScience 咨询
Thanks for your answer.
My script to create RSA key i used OpenSSL whit:
(Linux Script)
In C# we need privateKey in XML format. I used this parser to do this.
To decrypt de challenge we need to use:
To encrypt whit server certificate we need to use:
Thanks for JavaScience Consulting