简体   繁体   English

如何将 DER 文件转换为 PEM 文件?

[英]How to convert a DER file to a PEM file?

I have a public key DER file and i wish to concert it to a PEM file but getting errors and Google isn't helping我有一个公钥 DER 文件,我希望将它与 PEM 文件一致,但出现错误,谷歌没有帮助

Any ideas please?请问有什么想法吗?

alan@WW031779:/mnt/c/Users/alan/Downloads/RsaCtfTool$ openssl x509 -inform der -in pubkey.der -out certificate.pem
unable to load certificate
140024363618944:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140024363618944:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=X509
alan@WW031779:/mnt/c/Users/alan/Downloads/RsaCtfTool$ openssl x509 -in pubkey.der -inform DER -out private.pem -outform PEM
unable to load certificate
140228465136256:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140228465136256:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=X509

Even something simple like甚至像这样简单的东西

openssl x509 -in pubkey.der -out cert.pem

Gives me给我

unable to load certificate
139881925907072:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE

I tried to use pkey as per a suggestion below but get我尝试按照以下建议使用 pkey 但得到

139641461281408:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
139641461281408:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:572:
139641461281408:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
139641461281408:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=RSAPrivateKey
139641461281408:error:04093004:rsa routines:old_rsa_priv_decode:RSA lib:../crypto/rsa/rsa_ameth.c:133:
139641461281408:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
139641461281408:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=PKCS8_PRIV_KEY_INFO

Also rsa还有 rsa

alan@WW031779:/mnt/c/Users/alan/Downloads/RsaCtfTool$ openssl rsa -inform der -in pubkey.der -out certificate.pem
unable to load Private Key
140404504073536:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140404504073536:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:572:
140404504073536:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140404504073536:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=RSAPrivateKey
140404504073536:error:04093004:rsa routines:old_rsa_priv_decode:RSA lib:../crypto/rsa/rsa_ameth.c:133:
140404504073536:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140404504073536:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=PKCS8_PRIV_KEY_INFO
alan@WW031779:/mnt/c/Users/alan/Downloads/RsaCtfTool$ openssl rsa -in pubkey.der -out cert.pem
unable to load Private Key
140227099890304:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY
alan@WW031779:/mnt/c/Users/alan/Downloads/RsaCtfTool$ openssl rsa -in pubkey.der -inform DER -out private.pem -outform PEM
unable to load Private Key
140152776102528:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140152776102528:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:572:
140152776102528:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140152776102528:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=RSAPrivateKey
140152776102528:error:04093004:rsa routines:old_rsa_priv_decode:RSA lib:../crypto/rsa/rsa_ameth.c:133:
140152776102528:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140152776102528:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:309:Type=PKCS8_PRIV_KEY_INFO

Content of my DER file is here https://ghostbin.com/nrsmD and a snippet below我的 DER 文件的内容在这里https://ghostbin.com/nrsmD和下面的片段

在此处输入图像描述

After several attempts to convert the DER into PEM the OP provided the DER file in question.在多次尝试将 DER 转换为 PEM 后,OP 提供了有问题的 DER 文件。 Base64 encoded it looks like this: Base64 编码如下:

BEGINPUBLICKEYMIIB...AwENDPUBLICKEY BEGINPUBLICKEYMIIB...AwENDPUBLICKEY

The BEGINPUBLICKEY at the beginning and ENDPUBLICKEY look very fishy.开头的 BEGINPUBLICKEY 和 ENDPUBLICKEY 看起来很可疑。 Obviously someone took the PEM formatted public key and simply converted it from base64, then claimed that this is DER formatted.显然有人拿了 PEM 格式的公钥并简单地将其从 base64 转换,然后声称这是 DER 格式。 Only it is not, since the leading -----BEGIN PUBLIC KEY----- and final -----END PUBLIC KEY----- are not supposed to be included when decoding from base64, only the part between these should be decoded to get to the DER.只有它不是,因为从 base64 解码时不应该包含前导-----BEGIN PUBLIC KEY-----和最终-----END PUBLIC KEY----- ,只有部分这些之间应该被解码以到达DER。

After repairing the base64 encoding by removing the wrong BEGINPUBLICKEY and ENDPUBLICKEY and then properly padding the remaining base64 with == decoding successfully lead to a proper DER file.通过删除错误的 BEGINPUBLICKEY 和 ENDPUBLICKEY 修复 base64 编码,然后使用==正确填充剩余的 base64 编码后,成功生成正确的 DER 文件。

$  openssl base64 -d -in pubkey.b64 -out pubkey.der

This then could be transformed to proper PEM然后可以将其转换为适当的 PEM

$  openssl pkey -pubin -inform der -in pubkey.der -out pubkey.pem

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

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