使用java 解析 pfx 和 cer、crt 的 到期时间

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值