keystool export private key

keystore to openssl .

java -jar ExportPrivateKey.zip {keystore_path} JKS {keystore_password} {alias} {target_file}

This would export the key to PKCS #8 PEM format. Now run openssl to convert it to the format apache modssl expects the file in
openssl pkcs8 -inform PEM -nocrypt -in exported-pkcs8.key -out exported.key

The java code for exporting the private key in PKCS #8 format

1.
import java.io.File;
2.
import java.io.FileInputStream;
3.
import java.io.FileWriter;
4.
import java.security.Key;
5.
import java.security.KeyPair;
6.
import java.security.KeyStore;
7.
import java.security.KeyStoreException;
8.
import java.security.NoSuchAlgorithmException;
9.
import java.security.PrivateKey;
10.
import java.security.PublicKey;
11.
import java.security.UnrecoverableKeyException;
12.
import java.security.cert.Certificate;
13.

14.
import sun.misc.BASE64Encoder;
15.

16.
public class ExportPrivateKey {
17.
private File keystoreFile;
18.
private String keyStoreType;
19.
private char[] password;
20.
private String alias;
21.
private File exportedFile;
22.

23.
public static KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
24.
try {
25.
Key key=keystore.getKey(alias,password);
26.
if(key instanceof PrivateKey) {
27.
Certificate cert=keystore.getCertificate(alias);
28.
PublicKey publicKey=cert.getPublicKey();
29.
return new KeyPair(publicKey,(PrivateKey)key);
30.
}
31.
} catch (UnrecoverableKeyException e) {
32.
} catch (NoSuchAlgorithmException e) {
33.
} catch (KeyStoreException e) {
34.
}
35.
return null;
36.
}
37.

38.
public void export() throws Exception{
39.
KeyStore keystore=KeyStore.getInstance(keyStoreType);
40.
BASE64Encoder encoder=new BASE64Encoder();
41.
keystore.load(new FileInputStream(keystoreFile),password);
42.
KeyPair keyPair=getPrivateKey(keystore,alias,password);
43.
PrivateKey privateKey=keyPair.getPrivate();
44.
String encoded=encoder.encode(privateKey.getEncoded());
45.
FileWriter fw=new FileWriter(exportedFile);
46.
fw.write(“—–BEGIN PRIVATE KEY—–\n“);
47.
fw.write(encoded);
48.
fw.write(“\n“);
49.
fw.write(“—–END PRIVATE KEY—–”);
50.
fw.close();
51.
}
52.

53.

54.
public static void main(String args[]) throws Exception{
55.
ExportPrivateKey export=new ExportPrivateKey();
56.
export.keystoreFile=new File(args[0]);
57.
export.keyStoreType=args[1];
58.
export.password=args[2].toCharArray();
59.
export.alias=args[3];
60.
export.exportedFile=new File(args[4]);
61.
export.export();
62.
}
63.
}

虽然有错 ,, 还是记录下~!
,, 居然 搞出来是 空指针错误,,

KeyPair keyPair=getPrivateKey(keystore,alias,password);
PrivateKey privateKey=keyPair.getPrivate();

这里读取不到,,

还有Keystore Explorer 这个工具 是可以出私钥的。

大家可以试试。 找到你的 keystore 文件就可以了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值