JKS证书转换成PFX证书


package com;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.util.Enumeration;

public class ConvertJKS {
	/**      
	* 从JKS格式转换为PKCS12格式      *    
	* @param jksFilePath      *     
	String JKS格式证书库路径    
	* @param jksPasswd      
	*    String JKS格式证书库密码   
	* @param pfxFilePath    
	*       String PKCS12格式证书库保存文件夹 
	* @param pfxPasswd     
	*            String PKCS12格式证书库密码    
	*/  
	public void covertJSKToPFX(String jksFilePath, String jksPasswd, String pfxFolderPath, String pfxPasswd) throws Throwable   
	{         
	FileInputStream fis = null;     
	try          {        
	KeyStore inputKeyStore = KeyStore.getInstance("JKS");      
	fis = new FileInputStream(jksFilePath);            
	char[] srcPwd = jksPasswd == null ? null : jksPasswd.toCharArray();       
	char[] destPwd = pfxPasswd == null ? null : pfxPasswd.toCharArray();     
	inputKeyStore.load(fis, srcPwd);             
	KeyStore outputKeyStore = KeyStore.getInstance("PKCS12");      
	Enumeration enums = inputKeyStore.aliases();          
	while (enums.hasMoreElements())            {     
	String keyAlias = (String) enums.nextElement();           
	System.out.println("alias=[" + keyAlias + "]");        
	outputKeyStore.load(null, destPwd);                 
	if (inputKeyStore.isKeyEntry(keyAlias))           
	{                     
	Key key = inputKeyStore.getKey(keyAlias, srcPwd);      
	java.security.cert.Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);    
	outputKeyStore.setKeyEntry(keyAlias, key, destPwd, certChain);         
	}                 
	String fName = pfxFolderPath + "_" + keyAlias + ".pfx";     
	FileOutputStream out = new FileOutputStream(fName);              
	outputKeyStore.store(out, destPwd);      
	out.close();                
	outputKeyStore.deleteEntry(keyAlias);      
	}         
	} finally   
	{          
	try         
	{            
	if (fis != null)     
	{                   
	fis.close();       
	}             
	} catch (Exception e)   
	{                 
	e.printStackTrace();          
	}       
	}      
	} 
	
	
	public static void main(String[] args) throws Throwable {
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值