PFX bulk turn into JKS tool code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.