大家好,又见面了,我是你们的朋友全栈君。
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
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134608.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...