import java.security.*;
import java.security.cert.X509Certificate;
public class TestController {
public static void main(String args[]) throws Exception {
String mchId = "商户号";
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(getCertStream(""), mchId.toCharArray());
String alias = ks.aliases().nextElement();
X509Certificate certificate = (X509Certificate) ks.getCertificate(alias);
java.util.Date notBefore = certificate.getNotBefore();
java.util.Date notAfter = certificate.getNotAfter();
String serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, mchId.toCharArray());
System.out.println("证书开始有效日期:" + notBefore);
System.out.println("证书结束有效日期:" + notAfter);
System.out.println("---------serialNo------" + serialNo);
System.out.println("---------privateKey--------" + privateKey);
}
public static InputStream getCertStream(String certPath) {
byte[] certData = null;
try {
InputStream certStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("apiclient_cert.p12");
certData = IOUtils.toByteArray(certStream);
certStream.close();
} catch (Exception e) {
e.printStackTrace();
}
ByteArrayInputStream certBis = new ByteArrayInputStream(certData);
return certBis;
}
}