openssl - 将PEM传统私钥转换为PKCS8私钥

标签 openssl certificate

我已获得一个带有证书和公钥/私钥的 PEM 文件。具体来说,它包括标题

-----BEGIN CERTIFICATE-----   
-----END CERTIFICATE-----   
-----BEGIN RSA PRIVATE KEY-----   
-----END RSA PRIVATE KEY-----   
-----BEGIN RSA PUBLIC KEY-----   
-----END RSA PUBLIC KEY-----

按照特定的顺序。

我的理解是,在 BEGIN RSA PRIVATE KEY header 之后没有 header ,该 pem 文件包含传统格式 (PKCS1) 的私钥,无需加密。

我需要将此私钥转换为 DER 编码的 PKCS8 未加密格式,以便与 java 服务器代码(特别是 PKCS8EncodedKeySpec)一起使用。我尝试过使用 rsa 和 pkcs8 命令的 OpenSSL,但没有成功。如果有更简单的方法,则没有特定需要使用 openssl。

具体:

openssl rsa -in IServer_Key.orig.prikey.pem -out IServer_Key.pkcs8.pem
openssl rsa -in IServer_Key.orig.prikey.pem -out IServer_Key.pkcs8.pem -pubin openssl pkcs8 -in IServer_Key.orig.prikey.pem -out IServer_Key.pkcs8.pem -nocrypt

我也尝试过指定 inform 和 outform,但没有成功。

user@ubuntu:~/TestCerts$ openssl rsa -in IServer_Key.pem -out IServer_Key.pkcs8.pem -pubin 
unable to load Public Key 
5925:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:650:
Expecting: PUBLIC KEY

user@ubuntu:~/TestCerts$ openssl rsa -in IServer_Key.orig.prikey.pem -out IServer_Key.pkcs8.pem 
unable to load Private Key 
5993:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1316: 
5993:error:0D06C03A:asn1 encoding routines:ASN1_D2I_EX_PRIMITIVE:nested asn1 error:tasn_dec.c:828:
5993:error:0D08303A:asn1 encoding routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 error:tasn_dec.c:748:Field=n, Type=RSA 
5993:error:0D09A00D:asn1 encoding routines:d2i_PrivateKey:ASN1 lib:d2i_pr.c:99: 
5993:error:0907B00D:PEM routines:PEM_READ_BIO_PRIVATEKEY:ASN1 lib:pem_pkey.c:125:

user@ubuntu:~/TestCerts$ openssl pkcs8 -in IServer_Key.orig.prikey.pem -out IServer_Key.pkcs8.pem -nocrypt 
Error decrypting key 
6022:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:650:
Expecting: PRIVATE KEY

此时,我们将非常感谢任何帮助。

最佳答案

尝试使用以下命令。我还没有尝试过,但我认为它应该有效。

openssl pkcs8 -topk8 -inform PEM -outform DER -in filename -out filename -nocrypt

关于openssl - 将PEM传统私钥转换为PKCS8私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290435/

相关文章:

linux - 如何在 Linux 上使用 TPM 实现平台许可证 "dongle"?

apache - Ubuntu 10.04 为 apache 更新 openssl

php - 从 Azure Active Directory 验证 JWT

java - 将外部服务器的自签名证书添加到我的 Tomcat 的受信任证书中

android - 使用多个证书签署应用程序

ios私钥和证书不配对

c++ - Nextcloud 桌面客户端构建过程

macos - macOS 上的 OpenSSL 1.0.2m

java - 将公私 key 对导入 keystore

c# - 将证书作为嵌入资源加载到程序集中