读书人

PFX证件转JKS格式

发布时间: 2012-09-22 21:54:54 作者: rapoo

PFX证书转JKS格式

项目需要把PFX证书转成jks格式, 在网上找到一段代码可以做这件是. 现把这段代码粘贴在下面, 做个记号, 以被后用.

<<代码转贴>>

import java.security.KeyStore;
import java.security.Key;
import java.security.cert.Certificate;
import java.io.*;
import java.util.*;

public class PFXToJKSConvert {
??? public static final String PKCS12 = "PKCS12";
??? public static final String JKS = "JKS";
??? public static final String INPUT_KEYSTORE_FILE = "**.pfx";
??? public static final String KEYSTORE_PASSWORD = "pwd";
??? public static final String OUTPUT_KEYSTORE_FILE = "***.jks";

??? public static void main(String[] args) {
??? ??? try {
??? ??? ??? KeyStore inputKeyStore = KeyStore.getInstance("PKCS12");
??? ??? ??? FileInputStream fis = new FileInputStream(INPUT_KEYSTORE_FILE);
??? ??? ??? char[] nPassword = null;
??? ??? ??? if ((KEYSTORE_PASSWORD == null)
??? ??? ??? ??? ??? || KEYSTORE_PASSWORD.trim().equals("")) {
??? ??? ??? ??? nPassword = null;
??? ??? ??? } else {
??? ??? ??? ??? nPassword = KEYSTORE_PASSWORD.toCharArray();
??? ??? ??? }

??? ??? ??? inputKeyStore.load(fis, nPassword);
??? ??? ??? fis.close();
??? ??? ??? KeyStore outputKeyStore = KeyStore.getInstance("JKS");
??? ??? ??? outputKeyStore.load(null, "123456".toCharArray());
??? ??? ??? Enumeration e = inputKeyStore.aliases();
??? ??? ??? while (e.hasMoreElements()) { // we are readin just one certificate.
??? ??? ??? ??? String keyAlias = (String) e.nextElement();
??? ??? ??? ??? System.out.println("alias=[" + keyAlias + "]");
??? ??? ??? ??? if (inputKeyStore.isKeyEntry(keyAlias)) {
??? ??? ??? ??? ??? Key key = inputKeyStore.getKey(keyAlias, nPassword);
??? ??? ??? ??? ??? Certificate[] certChain = inputKeyStore
??? ??? ??? ??? ??? ??? ??? .getCertificateChain(keyAlias);
??? ??? ??? ??? ??? outputKeyStore.setKeyEntry("outkey", key, "111111"
??? ??? ??? ??? ??? ??? ??? .toCharArray(), certChain);
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? FileOutputStream out = new FileOutputStream(OUTPUT_KEYSTORE_FILE);
??? ??? ??? outputKeyStore.store(out, nPassword);
??? ??? ??? out.close();
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? }
}

1 楼 joseph_he 2011-06-26 报错啊,知道是什么问题吗?

java.io.IOException: DerInputStream.getLength():
lengthTag=109, too big.

读书人网 >软件架构设计

热点推荐