将Java密钥库转换为PEM格式 [英] Converting a Java Keystore into PEM Format

查看:375
本文介绍了将Java密钥库转换为PEM格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用keytool和openssl应用程序将Java密钥库文件转换为PEM文件。但是我找不到一个很好的方法来进行转换。有任何想法吗?



而不是将密钥库直接转换为PEM,我尝试先创建一个PKCS12文件,然后转换为相关的PEM文件和密钥库。但是我无法使用它们建立连接。 (请注意,我只需要一个PEM文件和一个Keystore文件来实现一个安全的连接,没有限制,例如从java密钥库文件开始:)所以从其他格式开始,我的情况可以接受)



但是从jks到pem的直接转换方法是比较好的。

解决方案

直接使用jdk6至少...

 
bash $ keytool -keystore foo.jks -genkeypair -alias foo \
-dname'CN = foo.example.com,L =墨尔本,ST =维多利亚州,C = AU'
输入密钥库密码:
重新输入新密码:
输入$密码密码b $ b(如果与密钥库密码相同,则返回):
bash $ keytool -keystore foo.jks -exportcert -alias foo | \
openssl x509 -inform der -text
输入密钥库密码:asdasd
证书:
数据:
版本:3(0x2)
序列号:1237334757(0x49c03ae5)
签名算法:dsaWithSHA1
发行人:C = AU,ST =维多利亚,L =墨尔本,CN = foo.example.com
有效期
不在之前: 3月18日00:05:57 2009 GMT
不在之后:Jun 16 00:05:57 2009 GMT
主题:C = AU,ST =维多利亚,L =墨尔本,CN = foo.example.com
主题公钥信息:
公钥算法:dsaEncryption
DSA公钥:
pub:
00:e2:66:5c:e0:2e:da: e0:6b:a6:aa:97:64:59:14:
7e:a6:2e:5a:45:f9:2f:b5:2d:f4:34:27:e6:53:


bash $ keytool -importkeystore -srckeystore foo.jks \
-destkeystore foo.p12 \
-srcstoretype jks \
-deststoretype PK cs12
输入目标密钥库密码:
重新输入新密码:
输入密码库密码:
成功导入别名foo的条目。
导入命令完成:1条目成功导入,0条失败或取消

bash $ openssl pkcs12 -in foo.p12 -out foo.pem
输入导入密码:
MAC验证OK
输入PEM密码:
验证 - 输入PEM密码:

bash $ openssl x509 -text -in foo.pem
证书:
数据:
版本:3(0x2)
序列号:1237334757(0x49c03ae5)
签名算法:dsaWithSHA1
发行人:C = AU,ST =维多利亚=墨尔本,CN = foo.example.com
有效期
不在之前:3月18日00:05:57 2009 GMT
不在之后:Jun 16 00:05:57 2009 GMT
主题:C = AU,ST =维多利亚,L =墨尔本,CN = foo.example.com
主题公钥信息:
公钥算法:dsaEncryption
DSA公钥:
pub:
00:e2:66:5c:e0:2e:da:e0:6b:a6:aa:97:64:59:14:
7e:a6:2e:5a:45:f9:2f:b5:2d:f4:34:27:e6:53:c7:


bash $ openssl dsa -text -in foo.pem
读取DSA密钥
输入PEM密码:
私钥:(1024位)
priv:
00:8f:b1:af:55 :63:92:7c:d2:0f:e6:f3:a2:f5:ff:
1a:7a:fe:8c:39:dd
pub:
00:e2: 66:5c:e0:2e:da:e0:6b:a6:aa:97:64:59:14:
7e:a6:2e:5a:45:f9:2f:b5:2d: 34:27:e6:53:c7:



/ p>


  • foo.jks - java格式的密钥库。

  • foo.p12 - PKCS#12中的密钥库

  • foo.pem - 密钥库中的所有密钥和证书,以PEM格式。



(如果你喜欢,最后一个文件可以分成密钥和证书。)






命令摘要 - 创建JKS密钥库:

  keytool -keystore foo.jks -genkeypair -alias foo \ 
-dname'CN = foo。 example.com,L =墨尔本,ST =维多利亚州,C = AU'

命令摘要 - 将JKS密钥库转换为PKCS#12密钥库,然后转换为PEM文件:

  keytool -importkeystore -srckeystore foo.jks \ 
-destkeystore foo.p12 \
-srcstoretype jks \
-deststoretype pkcs12

openssl pkcs12 -in foo.p12 - out foo.pem

如果您的JKS密钥库中有多个证书,而您只想导出与其中一个别名相关联的证书和密钥,您可以使用以下变体:

  keytool -importkeystore -srckeystore foo。 jks \ 
-destkeystore foo.p12 \
-srcalias foo \
-srcstoretype jks \
-deststoretype pkcs12

openssl pkcs12 - 在foo.p12 -out foo.pem

命令摘要 - 将JKS密钥库与PEM文件进行比较: / p>

  keytool -keystore foo.jks -exportcert -alias foo | \ 
openssl x509 -inform der -text

openssl x509 -text -in foo.pem

openssl dsa -text -in foo.pem


I am trying to convert from a Java keystore file into a PEM file using keytool and openssl applicactions. But I could not find a good way to do the conversion. Any ideas?

Instead of converting the keystore directly into PEM I tried to create a PKCS12 file first and then convert into relevant PEM file and Keystore. But I could not establish a connection using them. (Note that I just need a PEM file and a Keystore file to implement a secured connection. There is no restriction like "Start from a java keystore file". :) So starting from other formats is acceptable with my case)

But a direct conversion method from jks to pem is preferable.

解决方案

It's pretty straightforward, using jdk6 at least...

bash$ keytool -keystore foo.jks -genkeypair -alias foo \
        -dname 'CN=foo.example.com,L=Melbourne,ST=Victoria,C=AU'
Enter keystore password:  
Re-enter new password: 
Enter key password for 
        (RETURN if same as keystore password):  
bash$ keytool -keystore foo.jks -exportcert -alias foo | \
       openssl x509 -inform der -text
Enter keystore password:  asdasd
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 1237334757 (0x49c03ae5)
        Signature Algorithm: dsaWithSHA1
        Issuer: C=AU, ST=Victoria, L=Melbourne, CN=foo.example.com
        Validity
            Not Before: Mar 18 00:05:57 2009 GMT
            Not After : Jun 16 00:05:57 2009 GMT
        Subject: C=AU, ST=Victoria, L=Melbourne, CN=foo.example.com
        Subject Public Key Info:
            Public Key Algorithm: dsaEncryption
            DSA Public Key:
                pub: 
                    00:e2:66:5c:e0:2e:da:e0:6b:a6:aa:97:64:59:14:
                    7e:a6:2e:5a:45:f9:2f:b5:2d:f4:34:27:e6:53:c7:
 

bash$ keytool -importkeystore -srckeystore foo.jks \
       -destkeystore foo.p12 \
       -srcstoretype jks \
       -deststoretype pkcs12
Enter destination keystore password:  
Re-enter new password: 
Enter source keystore password:  
Entry for alias foo successfully imported.
Import command completed:  1 entries successfully imported, 0 entries failed or cancelled

bash$ openssl pkcs12 -in foo.p12 -out foo.pem
Enter Import Password:
MAC verified OK
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

bash$ openssl x509 -text -in foo.pem
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 1237334757 (0x49c03ae5)
        Signature Algorithm: dsaWithSHA1
        Issuer: C=AU, ST=Victoria, L=Melbourne, CN=foo.example.com
        Validity
            Not Before: Mar 18 00:05:57 2009 GMT
            Not After : Jun 16 00:05:57 2009 GMT
        Subject: C=AU, ST=Victoria, L=Melbourne, CN=foo.example.com
        Subject Public Key Info:
            Public Key Algorithm: dsaEncryption
            DSA Public Key:
                pub: 
                    00:e2:66:5c:e0:2e:da:e0:6b:a6:aa:97:64:59:14:
                    7e:a6:2e:5a:45:f9:2f:b5:2d:f4:34:27:e6:53:c7:
 

bash$ openssl dsa -text -in foo.pem
read DSA key
Enter PEM pass phrase:
Private-Key: (1024 bit)
priv:
    00:8f:b1:af:55:63:92:7c:d2:0f:e6:f3:a2:f5:ff:
    1a:7a:fe:8c:39:dd
pub: 
    00:e2:66:5c:e0:2e:da:e0:6b:a6:aa:97:64:59:14:
    7e:a6:2e:5a:45:f9:2f:b5:2d:f4:34:27:e6:53:c7:



You end up with:

  • foo.jks - keystore in java format.
  • foo.p12 - keystore in PKCS#12 format.
  • foo.pem - all keys and certs from keystore, in PEM format.

(This last file can be split up into keys and certificates if you like.)


Command summary - to create JKS keystore:

keytool -keystore foo.jks -genkeypair -alias foo \
    -dname 'CN=foo.example.com,L=Melbourne,ST=Victoria,C=AU'

Command summary - to convert JKS keystore into PKCS#12 keystore, then into PEM file:

keytool -importkeystore -srckeystore foo.jks \
   -destkeystore foo.p12 \
   -srcstoretype jks \
   -deststoretype pkcs12

openssl pkcs12 -in foo.p12 -out foo.pem

if you have more than one certificate in your JKS keystore, and you want to only export the certificate and key associated with one of the aliases, you can use the following variation:

keytool -importkeystore -srckeystore foo.jks \
   -destkeystore foo.p12 \
   -srcalias foo \
   -srcstoretype jks \
   -deststoretype pkcs12

openssl pkcs12 -in foo.p12 -out foo.pem

Command summary - to compare JKS keystore to PEM file:

keytool -keystore foo.jks -exportcert -alias foo | \
   openssl x509 -inform der -text

openssl x509 -text -in foo.pem

openssl dsa -text -in foo.pem

这篇关于将Java密钥库转换为PEM格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆