相关疑难解决方法(0)

如何从PEM文件中获取私钥?

我有一个.PEM文件,其中包含用于SSL数据传输的公钥和私钥,如下所示:

-----BEGIN RSA PRIVATE KEY-----
      private key data
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
      public key data
-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)

当我想通过以下代码加载.PEM文件时:

X509Certificate2 xx = new X509Certificate2("c:\\myKey.pem");
Run Code Online (Sandbox Code Playgroud)

我得到一个例外,说:"找不到请求的对象." ,满堆栈:

System.Security.Cryptography.CryptographicException was unhandled
  Message=Cannot find the requested object.

  Source=mscorlib
  StackTrace:
       at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
       at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
       at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
       at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName)
       at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
       at DLLTest.SSL_Test.test() in E:\Projects\DLLTest\DLLTest\SSL_Test.cs:line 165
       at DLLTest.SSL_Test.Run() in E:\Projects\DLLTest\DLLTest\SSL_Test.cs:line 21
       at DLLTest.Program.Main(String[] args) in E:\Projects\DLLTest\DLLTest\Program.cs:line 21
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, …
Run Code Online (Sandbox Code Playgroud)

.net ssl cryptography pem private-key

31
推荐指数
5
解决办法
6万
查看次数

将私钥/公钥从X509证书导出到PEM

有没有方便的方法从使用.NET Core的 PEM格式的.p12证书导出私钥/公钥?没有在低级别操作字节?我用谷歌搜索了几个小时,几乎没有任何东西在.net核心中可用,或者它没有记录在任何地方..

我们有一个X509Certificate2

var cert = new X509Certificate2(someBytes, pass);
var privateKey = cert.GetRSAPrivateKey();
var publicKey = cert.GetRSAPublicKey();
// assume everything is fine so far
Run Code Online (Sandbox Code Playgroud)

现在我需要将密钥导出为两个单独的PEM密钥.我已经尝试PemWriter过BouncyCastle,但是这些类型与Core的System.Security.Cryptography不兼容..没有运气.

- -编辑 - -

换句话说,我找到了一种方法来写这个:

$ openssl pkcs12 -in path/to/cert.p12 -out public.pub -clcerts -nokeys
$ openssl pkcs12 -in path/to/cert.p12 -out private.key -nocerts
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

谢谢 ...

c# x509certificate2 pem .net-core

8
推荐指数
2
解决办法
8872
查看次数

使用 ASP.NET Core 3.0 内置 API 从 .crt 和 .key 文件创建 X509Certificate2

我正在使用 ASP.NET Core 3.0 预览版 8 开发 Web 应用程序。我想要实现的是直接从 .crt 和 .key (PKCS#1) 文件创建一个 X509Certificate2,以便使用新的 .NET Core 3.0 与 Kestrel 一起使用内置在此链接中介绍的 Api。

.NET Core 3.0 中的新增功能(预览版 8)

目前我正在使用此命令将证书转换为 .pfx 文件

openssl pkcs12 -export -in $CERTIFICATE_FILE -inkey $KEY_FILE -out $OUTPUT_CERTIFICATE_FILE -passout pass:$OUTPUT_CERTIFICATE_PASSWORD
Run Code Online (Sandbox Code Playgroud)

然后我创建 X509Certificate2 并将其与 Kestrel 一起使用:

webBuilder.ConfigureKestrel((context, options) =>
{
    options.ConfigureHttpsDefaults(httpsOptions =>
    {
        if (!context.HostingEnvironment.IsProduction())
            return;

            string outputCertificateFile = context.Configuration["OUTPUT_CERTIFICATE_FILE"];
            string outputCertificatePassword = context.Configuration["OUTPUT_CERTIFICATE_PASSWORD"];

            var tlsCertificate = new X509Certificate2(outputCertificateFile, outputCertificatePassword);
            httpsOptions.ServerCertificate = tlsCertificate;
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑:-

我已经根据上面链接中的示例实现了新的 Api:

using (var …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core kestrel-http-server asp.net-core

6
推荐指数
1
解决办法
1743
查看次数

Pem证书中的私钥如何加密?

作为调试问题的一部分,我试图了解pem证书中私钥的加密方式,因为我想知道是否curl无法对私钥进行解密。

我的-----BEGIN ENCRYPTED PRIVATE KEY-----部分中有一个pem

它用密码加密了吗?还有其他某种加密方案吗?

更确切地说

一位同事建议,pem即使没有密码,也可以对私钥进行加密。这是对的吗?

encryption ssl curl pem

1
推荐指数
1
解决办法
6393
查看次数