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 {
}
}
JKS证书转换成PFX证书
最新推荐文章于 2023-11-14 16:07:49 发布