String keypass = "password"; // this is a new password, you need to come up with to protect your java key store file
String defaultalias = "importkey";
KeyStore ks = KeyStore.getInstance("JKS", "SUN");
// this section does not make much sense to me,
// but I will leave it intact as this is how it was in the original example I found on internet:
ks.load( null, keypass.toCharArray());
ks.store( new FileOutputStream ( "mykeystore" ), keypass.toCharArray());
ks.load( new FileInputStream ( "mykeystore" ), keypass.toCharArray());
// end of section..
// read the key file from disk and create a PrivateKey
FileInputStream fis = new FileInputStream("pkey.der");
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
byte[] key = new byte[bais.available()];
KeyFactory kf = KeyFactory.getInstance("RSA");
bais.read(key, 0, bais.available());
bais.close();
PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec ( key );
PrivateKey ff = kf.generatePrivate (keysp);
// read the certificates from the files and load them into the key store:
Collection col_crt1 = CertificateFactory.getInstance("X509").generateCertificates(new FileInputStream("cert1.pem"));
Collection col_crt2 = CertificateFactory.getInstance("X509").generateCertificates(new FileInputStream("cert2.pem"));
Certificate crt1 = (Certificate) col_crt1.iterator().next();
Certificate crt2 = (Certificate) col_crt2.iterator().next();
Certificate[] chain = new Certificate[] { crt1, crt2 };
String alias1 = ((X509Certificate) crt1).getSubjectX500Principal().getName();
String alias2 = ((X509Certificate) crt2).getSubjectX500Principal().getName();
ks.setCertificateEntry(alias1, crt1);
ks.setCertificateEntry(alias2, crt2);
// store the private key
ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), chain );
// save the key store to a file
ks.store(new FileOutputStream ( "mykeystore" ),keypass.toCharArray());
#!/bin/bash
pemToJks()
{
# number of certs in the PEM file
pemCerts=$1
certPass=$2
newCert=$(basename "$pemCerts")
newCert="${newCert%%.*}"
newCert="${newCert}"".JKS"
##echo $newCert $pemCerts $certPass
CERTS=$(grep 'END CERTIFICATE' $pemCerts| wc -l)
echo $CERTS
# For every cert in the PEM file, extract it and import into the JKS keystore
# awk command: step 1, if line is in the desired cert, print the line
# step 2, increment counter when last line of cert is found
for N in $(seq 0 $(($CERTS - 1))); do
ALIAS="${pemCerts%.*}-$N"
cat $pemCerts |
awk "n==$N { print }; /END CERTIFICATE/ { n++ }" |
$KEYTOOLCMD -noprompt -import -trustcacerts \
-alias $ALIAS -keystore $newCert -storepass $certPass
done
}
pemToJks <pem to import> <pass for new jks>
8 回答
首先,以DER格式转换证书:
之后,将其导入密钥库:
如果您只想将PEM格式的证书导入密钥库,则keytool将完成以下任务:
我开发了http://code.google.com/p/java-keyutil/,它直接将PEM证书导入Java密钥库 . 其主要目的是导入多部分PEM操作系统证书包,例如ca-bundle.crt . 这些通常包括keytool无法处理的标头
在我的情况下,我有一个pem文件,其中包含两个证书和一个用于相互SSL身份验证的加密私钥 . 所以我的pem文件看起来像这样:
这就是我做的
将文件拆分为三个单独的文件,以便每个文件只包含一个条目,以
---BEGIN..
开头,以---END..
行结束 . 让我们假设我们现在有三个文件:cert1.pem
,cert2.pem
和pkey.pem
.使用openssl和以下语法将
pkey.pem
转换为DER格式:请注意,如果私钥被加密,您需要提供密码(从原始pem文件的供应商处获取)以转换为DER格式,
openssl
将要求您输入如下密码:“输入pkey.pem
的密码: ” .如果转换成功,您将获得一个名为
pkey.der
的新文件 .创建一个新的Java密钥库并导入私钥和证书:
(可选)验证新密钥库的内容:
密钥库类型:JKS密钥库提供商:SUN
您的密钥库包含3个条目:
cn = ...,ou = ...,o = ..,2014年9月2日,trustedCertEntry,证书指纹(SHA1):2C:B8:...
importkey,2014年9月2日,PrivateKeyEntry,证书指纹(SHA1):9C:B0:...
cn = ...,o = ....,2014年9月2日,trustedCertEntry,证书指纹(SHA1):83:63:...
(可选)针对您的SSL服务器测试新密钥存储区中的证书和私钥:(您可能希望启用调试作为VM选项:-Djavax.net.debug = all)
如果计划使用它,最后使用HttpsURLConnection注册您的证书:
我总是忘记如何做到这一点,因为这是我偶尔会做的事情,这是一种可能的解决方案,它只是起作用:
转到您喜欢的浏览器并从安全网站下载主证书 .
执行以下两行代码:
步骤2的另一个选项是仅使用
keytool
命令 . 贝娄是一个证书链的例子:如果您需要一种简单的方法来加载Java without having to deal with external tools (opensll, keytool) 中的PEM文件,这是我在 生产环境 中使用的代码:
玩得开心 .
还有一个GUI工具,允许可视化JKS创建和证书导入 .
http://portecle.sourceforge.net/
我是从网上得到的 . 它适用于包含多个条目的pem文件 .