简体   繁体   English

如何使用OpenSSL将Java生成的公钥转换为PEM格式

[英]How to convert a java generated public key to pem format with openssl

I've got a public RSA key which is as following: 我有一个公共RSA密钥,如下所示:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHn/hfvTLRXViBXTmBhNYEIJeGGGDkmrYBxCRelri
LEYEcrwWrzp0au9nEISpjMlXeEW4+T82bCM22+JUXZpIga5qdBrPkjU08Ktf5n7Nsd7n9ZeI0YoAKCu
b3ulVExcxGeS3RVxFai9ozERlavpoTOdUzEH6YWHP4reFfpMpLzwIDAQAB

I need to convert it into PEM format so that I can write my own python code using this public key, I see there're many answers to the opposite operation, but right now I need to convert it from JAVA style to PEM, can anyone help? 我需要将其转换为PEM格式,以便可以使用此公钥编写自己的python代码,我看到相反操作的答案很多,但是现在我需要将其从JAVA样式转换为PEM,任何人都可以救命?

You could create a bash script like this: 您可以创建一个bash脚本,如下所示:

#!/bin/bash

# Loop through all the certs in the current folder that have the .cer extension
for cert in *.cer
do

#get filename wihtout ext
filename="${cert%.*}"
#echo ${filename} 

#convert to PEM
openssl x509 -inform der -in ${cert} -outform pem -out ${filename}.crt

done

I used this to convert certificates to pem format. 我用它来将证书转换为pem格式。 Hopefully it can help 希望它可以帮助

Just convert back the base64 encoded pub key and throw it to openssl: 只需转换回以base64编码的pub键,然后将其扔给openssl:

echo "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHn/hfvTLRXViBXTmBhNYEIJeGGGDkmrYBxCRelriLEYEcrwWrzp0au9nEISpjMlXeEW4+T82bCM22+JUXZpIga5qdBrPkjU08Ktf5n7Nsd7n9ZeI0YoAKCub3ulVExcxGeS3RVxFai9ozERlavpoTOdUzEH6YWHP4reFfpMpLzwIDAQAB" | base64 -d | openssl rsa -inform der -pubin -out rsakey.pub

-inform der is telling to openssl to take the key as binary (default is PEM) -inform der告诉openssl将密钥作为二进制(默认为PEM)

-pubin tells to expect a public (default is private) -pubin告诉您期望公开(默认为私有)

-out is to write the key -out是写密钥


Another way is to append the openssl tags before and after the base64 encoded rsa public key: 另一种方法是在base64编码的rsa公钥之前和之后附加openssl标记:

sed 's/^/-----BEGIN PUBLIC KEY-----\n/;s/$/\n-----END PUBLIC KEY-----/' <<< "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHn/hfvTLRXViBXTmBhNYEIJeGGGDkmrYBxCRelriLEYEcrwWrzp0au9nEISpjMlXeEW4+T82bCM22+JUXZpIga5qdBrPkjU08Ktf5n7Nsd7n9ZeI0YoAKCub3ulVExcxGeS3RVxFai9ozERlavpoTOdUzEH6YWHP4reFfpMpLzwIDAQAB" | openssl rsa -pubin

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

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