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

发布于 2024-08-10 19:33:38 字数 81 浏览 2 评论 0原文

我试图找到一种方法来读取在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情换悲伤 2024-08-17 19:33:38

从 .Net Core 3 开始:

using System.Security.Cryptography;

// ...

// I had my PK in base64
var pkBase64 = "MIIEvQI...";

var rsa = RSA.Create(); 
rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(pkBase64), out _);

供参考:https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importpkcs8privatekey?view=net-5.0

As of .Net Core 3:

using System.Security.Cryptography;

// ...

// I had my PK in base64
var pkBase64 = "MIIEvQI...";

var rsa = RSA.Create(); 
rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(pkBase64), out _);

For reference: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importpkcs8privatekey?view=net-5.0

坚持沉默 2024-08-17 19:33:38

使用外部库执行此操作的最简单方法是使用(免费)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.

携余温的黄昏 2024-08-17 19:33:38

感谢您的回答。

我使用 OpenSSL 来创建 RSA 密钥的脚本:

(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

在 C# 中,我们需要 XML 格式的私钥。我使用 this 解析器来执行此操作。

要解密挑战,我们需要使用:

  byte[] challange = server.getChallenge();

  RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();

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

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

要加密服务器证书,我们需要使用:

  RSACryptoServiceProvider rsaProvider = x509.PublicKey.Key as RSACryptoServiceProvider;

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

感谢 JavaScience 咨询

Thanks for your answer.

My script to create RSA key i used OpenSSL whit:

(Linux Script)

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. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文