我正在尝试使用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=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:
你最终得到:
foo.jks - java格式的密钥库.
foo.p12 - PKCS#12格式的密钥库.
foo.pem - 密钥库中的所有密钥和证书,采用PEM格式.
(如果您愿意,可以将此最后一个文件拆分为密钥和证书.)
命令摘要 - 创建JKS密钥库:
keytool -keystore foo.jks -genkeypair -alias foo \ -dname 'CN=foo.example.com,L=Melbourne,ST=Victoria,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 -in foo.p12 -out foo.pem
命令摘要 - 将JKS密钥库与PEM文件进行比较:
keytool -keystore foo.jks -exportcert -alias foo | \ openssl x509 -inform der -text openssl x509 -text -in foo.pem openssl dsa -text -in foo.pem
openssl
使用StoBor命令时,我不断收到错误:
MAC verified OK Error outputting keys and certificates 139940235364168:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:535: 139940235364168:error:23077074:PKCS12 routines:PKCS12_pbe_crypt:pkcs12 cipherfinal error:p12_decr.c:97: 139940235364168:error:2306A075:PKCS12 routines:PKCS12_item_decrypt_d2i:pkcs12 pbe crypt error:p12_decr.c:123:
出于某种原因,只有这种命令方式才适用于我的JKS文件
keytool -importkeystore -srckeystore foo.jks \ -destkeystore foo.p12 \ -srcstoretype jks \ -srcalias mykey \ -deststoretype pkcs12 \ -destkeypass DUMMY123
关键是设定destkeypass
,论证的价值无关紧要.
该keytool
命令将不允许您从密钥库中导出私钥.您必须编写一些Java代码才能执行此操作.打开密钥库,获取所需的密钥,并将其保存为PKCS#8格式的文件.同时保存关联的证书.
KeyStore ks = KeyStore.getInstance("jks"); /* Load the key store. */ ... char[] password = ...; /* Save the private key. */ FileOutputStream kos = new FileOutputStream("tmpkey.der"); Key pvt = ks.getKey("your_alias", password); kos.write(pvt.getEncoded()); kos.flush(); kos.close(); /* Save the certificate. */ FileOutputStream cos = new FileOutputStream("tmpcert.der"); Certificate pub = ks.getCertificate("your_alias"); cos.write(pub.getEncoded()); cos.flush(); cos.close();
使用OpenSSL实用程序将这些文件(二进制格式)转换为PEM格式.
openssl pkcs8 -inform der -nocrypt < tmpkey.der > tmpkey.pem openssl x509 -inform der < tmpcert.der > tmpcert.pem
使用keytool从jks直接转换为pem文件
keytool -exportcert -alias selfsigned -keypass password -keystore test-user.jks -rfc -file test-user.pem
将JKS文件转换为PEM和KEY格式(.crt&.key)的简化说明:
keytool -importkeystore -srckeystore-destkeystore -srcstoretype jks -deststoretype pkcs12 -destkeypass openssl pkcs12 -in -out openssl x509 -outform der -in -out openssl rsa -in -out