java pfx 证书_Java将pfx证书转换为jks

packagecom.yangangus.util;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.security.Key;importjava.security.KeyStore;importjava.security.cert.Certificate;importjava.util.Enumeration;public classConventPFXToJKS {public static final String PKCS12 = "PKCS12";public static final String JKS = "JKS";public static final String PFX_KEYSTORE_FILE = "D:\\temp\\certs\\wildcard_test_com.pfx";//pfx文件位置

public static final String PFX_PASSWORD = "pfx_password";//导出为pfx文件的设的密码

public static final String JKS_KEYSTORE_FILE = "D:\\temp\\certs\\keystore.jks"; //jks文件位置

public static final String JKS_PASSWORD = "jks_password";//JKS的密码

public static voidcoverTokeyStore() {

FileInputStream fis= null;

FileOutputStream out= null;try{

KeyStore inputKeyStore= KeyStore.getInstance("PKCS12");

fis= newFileInputStream(PFX_KEYSTORE_FILE);char[] pfxPassword = null;if ((PFX_PASSWORD == null) || PFX_PASSWORD.trim().equals("")) {

pfxPassword= null;

}else{

pfxPassword=PFX_PASSWORD.toCharArray();

}char[] jksPassword = null;if ((JKS_PASSWORD == null) || JKS_PASSWORD.trim().equals("")) {

jksPassword= null;

}else{

jksPassword=JKS_PASSWORD.toCharArray();

}

inputKeyStore.load(fis, pfxPassword);

fis.close();

KeyStore outputKeyStore= KeyStore.getInstance("JKS");

outputKeyStore.load(null, jksPassword);

Enumeration enums=inputKeyStore.aliases();while (enums.hasMoreElements()) { //we are readin just one//certificate.

String keyAlias =(String) enums.nextElement();

System.out.println("alias=[" + keyAlias + "]");if(inputKeyStore.isKeyEntry(keyAlias)) {

Key key=inputKeyStore.getKey(keyAlias, pfxPassword);

Certificate[] certChain=inputKeyStore.getCertificateChain(keyAlias);

outputKeyStore.setKeyEntry(keyAlias, key, jksPassword, certChain);

}

}

out= newFileOutputStream(JKS_KEYSTORE_FILE);

outputKeyStore.store(out, jksPassword);

out.close();

}catch(Exception e) {

e.printStackTrace();

}finally{if (fis != null) {try{

fis.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (out != null) {try{

out.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}public static voidmain(String[] args) {//TODO Auto-generated method stub

coverTokeyStore(); //pfx to jks

}

}