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) {// define variable values keystore pfxkeystore = null; keystore jkskeystore = null; fileinputstream fis = null; fileoutputstream fos = null;try {// Initialize pfx ,jks keystorepfxkeystore = 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);d irfile.mkdirs (); string jks = filejks + "//" + jksNames + ". JKs";fos = New fileoutputstream (New file (JKS)); Logger.info ("Expected output converted file path:" + new file (JKS). GetAbsolutePath ());// load pfx format certificate pfxkeystore.load (fis, PropertiesInfo.pfxPassword.toCharArray ()); Fis.close (); Enumeration<string> enums = pfxkeystore.aliases ();// traversal of the alias in KeyStore while (Enums.hasmoreelements ()) {string aliase = enums.nextelement ();if ( Pfxkeystore.iskeyentry (aliase)) {// get Key Object Key key = pfxkeystore.getkey (Aliase, PropertiesInfo.pfxPassword.toCharArray ());// get the certificate chain certificate[] certificates = Pfxkeystore.getcertificatechain (aliase)// jkskeystore load key information Jkskeystore.setkeyentry (Propertiesinfo.jksaliasename, key, PropertiesInfo.jksPassword.toCharArray (), certificates);// output JKS certificate 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 certificate into JKS code rename alias and password
PFX bulk turn into JKS tool code