package com.yjm.pfx2jks; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.security.Key; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.util.Enumeration; import java.util.Properties; import org.apache.log4j.Logger; import com.yjm.pojo.PropertiesInfo; public class Pfx2JksTools { public static Logger logger = Logger.getLogger(Pfx2JksTools.class); static { Properties ps = new Properties(); try { ps .load(PropertiesInfo.class .getResourceAsStream("info.properties")); PropertiesInfo.pfxPassword = ps.getProperty("pfxpassword"); PropertiesInfo.jksPassword = ps.getProperty("jkspassword"); PropertiesInfo.jksAliaseName = ps.getProperty("jksaliasename"); logger.info("pfxPassword:" + PropertiesInfo.pfxPassword); logger.info("jksAliaseName:" + PropertiesInfo.jksAliaseName); logger.info("jksPassword:" + PropertiesInfo.jksPassword); } catch (IOException e) { e.printStackTrace(); } } public static void pfx2jks(File file) { // 定义变量值 KeyStore pfxKeyStore = null; KeyStore jksKeyStore = null; FileInputStream fis = null; FileOutputStream fos = null; try { // 初始化 pfx ,jks keyStore pfxKeyStore = KeyStore.getInstance("PKCS12"); jksKeyStore = KeyStore.getInstance("JKS"); jksKeyStore.load(null, PropertiesInfo.jksPassword.toCharArray()); fis = new FileInputStream(file); String jksNames = file.getName(); jksNames = jksNames.substring(0, jksNames.indexOf(".")); String filejks = file.getParentFile().getAbsolutePath() + "//jks"; File dirfile = new File(filejks); dirfile.mkdirs(); String jks = filejks + "//" + jksNames + ".jks"; fos = new FileOutputStream(new File(jks)); logger.info("预计输出转换后的文件路径:" + new File(jks).getAbsolutePath()); // 装载 pfx 格式证书 pfxKeyStore.load(fis, PropertiesInfo.pfxPassword.toCharArray()); fis.close(); Enumeration<String> enums = pfxKeyStore.aliases(); // 遍历keyStore里面的 别名 while (enums.hasMoreElements()) { String aliase = enums.nextElement(); if (pfxKeyStore.isKeyEntry(aliase)) { // 获取key对象 Key key = pfxKeyStore.getKey(aliase, PropertiesInfo.pfxPassword.toCharArray()); // 获取证书链 Certificate[] certificates = pfxKeyStore .getCertificateChain(aliase); // jksKeyStore里面载入key信息 jksKeyStore.setKeyEntry(PropertiesInfo.jksAliaseName, key, PropertiesInfo.jksPassword.toCharArray(), certificates); // 输出jks证书 jksKeyStore.store(fos, PropertiesInfo.jksPassword .toCharArray()); } } fos.flush(); fos.close(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } } }
pfx证书转成jks代码 重命名别名和密码
时间: 2024-10-30 09:48:58