这很简单,至少使用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