public classPFXUtil {/*** 获取RSA算法的keyFactory
*
*@return
*/
private static KeyFactory getKeyFactory() throwsException {return getKeyFactory("RSA");
}/*** 获取指定算法的keyFactory
*
*@paramalgorithm
*@return
*/
private static KeyFactory getKeyFactory(String algorithm) throwsException {
KeyFactory keyFactory=KeyFactory.getInstance(algorithm);returnkeyFactory;
}/*** 根据pfx证书获取keyStore
*
*@parampfxData
*@parampassword
*@return*@throwsException*/
private static KeyStore getKeyStore(byte[] pfxData, String password) throwsException {
KeyStore keystore= KeyStore.getInstance("PKCS12");
keystore.load(newByteArrayInputStream(pfxData), password.toCharArray());returnkeystore;
}/*** 根据pfx证书得到私钥
*
*@parampfxData
*@parampassword
*@throwsException*/
public static PrivateKey getPrivateKeyByPfx(byte[] pfxData, String password) throwsException {
PrivateKey privateKey= null;
KeyStore keystore=getKeyStore(pfxData, password);
Enumeration enums =keystore.aliases();
String keyAlias= "";while(enums.hasMoreElements()) {
keyAlias=enums.nextElement();if(keystore.isKeyEntry(keyAlias)) {
privateKey=(PrivateKey) keystore.getKey(keyAlias, password.toCharArray());
}