package org.liuy.security.cert;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Enumeration;
/**
*
* PFX格式证书和JAVA keyStore证书相互转换
* @author liuy
* @version 1.0
* @since 1.0
*/
public class ConventPFX {
/**
*
* pfx转keyStore。
* @param pfxFile pfx路径
* @param ketStoreFile ketStore路径
* @param pfxPass pfx密码
*
*/
public static void pfxTokeyStore(String pfxFile,String ketStoreFile,String pfxPass) {
try {
KeyStore inputKeyStore = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream(pfxFile);
char[] nPassword = null;
if ((pfxPass == null)
|| pfxPass.trim().equals("")) {
nPassword = null;
} else {
nPassword = pfxPass.toCharArray();
}
inputKeyStore.load(fis, nPassword);
fis.close();
KeyStore outputKeyStore = KeyStore.getInstance("JKS");
outputKeyStore.load(null, pfxPass.toCharArray());
Enumeration enums = inputKeyStore.aliases();
while (enums.hasMoreElements()) { // we are readin just one certificate.
String keyAlias = (String) enums.nextElement();
System.out.println("alias=[" + keyAlias + "]");
if (inputKeyStore.isKeyEntry(keyAlias)) {
Key key = inputKeyStore.getKey(keyAlias, nPassword);
Certificate[] certChain = inputKeyStore
.getCertificateChain(keyAlias);
outputKeyStore.setKeyEntry(keyAlias, key, pfxPass
.toCharArray(), certChain);
}
}
FileOutputStream out = new FileOutputStream(ketStoreFile);
outputKeyStore.store(out, nPassword);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* keyStore转pfx
* @param pfxFile pfx路径
* @param ketStoreFile ketStore路径
* @param ketStorePass ketStore密码
*/
public static void keyStoreToPfx(String ketStoreFile,String pfxFile,String ketStorePass)
{
try {
KeyStore inputKeyStore = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream(ketStoreFile);
char[] nPassword = null;
if ((ketStorePass == null)
|| ketStorePass.trim().equals("")) {
nPassword = null;
} else {
nPassword = ketStorePass.toCharArray();
}
inputKeyStore.load(fis, nPassword);
fis.close();
KeyStore outputKeyStore = KeyStore.getInstance("PKCS12");
outputKeyStore.load(null, ketStorePass.toCharArray());
Enumeration enums = inputKeyStore.aliases();
while (enums.hasMoreElements())
{ // we are readin just one certificate.
String keyAlias = (String) enums.nextElement();
System.out.println("alias=[" + keyAlias + "]");
if (inputKeyStore.isKeyEntry(keyAlias))
{
Key key = inputKeyStore.getKey(keyAlias, nPassword);
Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);
outputKeyStore.setKeyEntry(keyAlias, key, ketStorePass.toCharArray(), certChain);
}
}
FileOutputStream out = new FileOutputStream(pfxFile);
outputKeyStore.store(out, nPassword);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
String pfxFile = "D:/test.pfx";
String ketStorePass = "123456";
String ketStoreFile = "d:/127.0.0.1";
pfxTokeyStore(pfxFile, ketStoreFile, ketStorePass);
// keyStoreToPfx(ketStoreFile, pfxFile, ketStorePass);
}
}