一、使用java代码

package com.sanglp.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyStore;

import java.security.cert.X509Certificate;
import java.util.Enumeration;

import org.apache.commons.io.FileUtils;

/**
 * 
 * @author yang6 从pfx文件里面读取信息
 * 
 */
public class ReadPFX {
	// 先得到keyStore
	private static KeyStore getKeyStore(byte[] pfxData, String password) throws Exception {
		KeyStore keystore = KeyStore.getInstance("PKCS12");
		keystore.load(new ByteArrayInputStream(pfxData), password.toCharArray());
		return keystore;
	}

	// x509证书
	public static X509Certificate getX509Certificate(byte[] pfxData, String password) throws Exception {
		X509Certificate x509Certificate = null;
		KeyStore keystore = getKeyStore(pfxData, password);
		Enumeration<String> enums = keystore.aliases();
		String keyAlias = "";
		while (enums.hasMoreElements()) {
			keyAlias = enums.nextElement();
			if (keystore.isKeyEntry(keyAlias)) {
				x509Certificate = (X509Certificate) keystore.getCertificate(keyAlias);
			}
		}
		return x509Certificate;
	}

	// 得到操作员的证书序列号
	public static String getOpertatorSn(String strPfx, String strPassword) {

		File pfxFile = new File(strPfx);
		String opertatorSn = null;
		try { // org.apach.commons.io
			X509Certificate cert = getX509Certificate(FileUtils.readFileToByteArray(pfxFile), strPassword);
			BigInteger serialNumbers = cert.getSerialNumber();

			opertatorSn = serialNumbers.toString(16); // 转十六进制

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return opertatorSn;
	}

	public static void main(String[] args) {
		System.out.println(getOpertatorSn("D://79813821473.pfx", "111111"));
	}
}

二、使用脚本

#!/bin/sh
PFX=$1

openssl pkcs12 -in $PFX.pfx -nodes -out $PFX.pem
openssl rsa -in $PFX.pem -out $PFX.key
openssl x509 -in $PFX.pem -out $PFX.crt

openssl x509 -noout -text -in $PFX.crt

方式二使用的时候需要执行脚本,并输入证书的名称(不需要.pfx后缀)