package Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
public class Test {
public static void main(String[] args)
throws CertificateException, IOException, javax.security.cert.CertificateException {
File pfx = new File(“lushunde_private.pfx”);
String privKeyPswdString = “lushunde”;
FileInputStream fileInputStream = new FileInputStream(pfx);
File cer = new File("lushunde_public.cer");
FileInputStream fileInputStreamc = new FileInputStream(cer);
getPrivateKeyInfo(fileInputStream, privKeyPswdString);
getPublicKeyInfo(fileInputStreamc);
}
/**
* 传入私钥数据流和密码 获取私钥有效时间
*
* @Title: getPrivateKeyInfo
* @Description:
* fileInputStream 私钥文件数据流
* privKeyPswdString 私钥密码
* Mr.lu(lushunde.com)
* @Date 2018年3月9日 上午12:24:37
*/
public static void getPrivateKeyInfo(FileInputStream fileInputStream, String privKeyPswdString) {
String keyAlias = null;
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
char[] nPassword = null;
if ((privKeyPswdString == null) || privKeyPswdString.trim().equals("")) {
nPassword = null;
} else {
nPassword = privKeyPswdString.toCharArray();
}
keyStore.load(fileInputStream, nPassword);
fileInputStream.close();
Enumeration<String> enumeration = keyStore.aliases();
if (enumeration.hasMoreElements()) {
keyAlias = enumeration.nextElement();
}
Certificate certificate = keyStore.getCertificate(keyAlias);
PublicKey publicKey = certificate.getPublicKey();
X509Certificate cert = X509Certificate.getInstance(certificate.getEncoded());
Date notBefore = cert.getNotBefore();
Date notAfter = cert.getNotAfter();
System.out.println("=============私钥日期================");
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(notBefore));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(notAfter));
System.out.println("=============私钥日期================");
} catch (Exception e) {
System.out.println(e);
}
}
public static void getPublicKeyInfo(FileInputStream fileInputStream) {
try {
X509Certificate cert = X509Certificate.getInstance(fileInputStream);
PublicKey publicKey = cert.getPublicKey();
Date notBefore = cert.getNotBefore();
Date notAfter = cert.getNotAfter();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
// BASE64Encoder base64Encoder = new BASE64Encoder();
// String publicKeyString = base64Encoder.encode(publicKey.getEncoded());
System.out.println(“—————–公钥——————–”);
System.out.println(new SimpleDateFormat(“yyyy-MM-dd”).format(notBefore));
System.out.println(new SimpleDateFormat(“yyyy-MM-dd”).format(notAfter));
System.out.println(“—————–公钥——————–”);
} catch (Exception e) {
System.out.println(e);
}
}
- 1
- 2
}