java读取keystore密钥证书信息
代码:
public static void main(String[] args) throws Exception {
// 创建 KeyStore 对象
KeyStore keyStore = KeyStore.getInstance("JKS");
// 加载 KeyStore 文件
keyStore.load(new FileInputStream("keystore.jks"), "password".toCharArray());
// 获取密钥
Key key = keyStore.getKey("my-key", "password".toCharArray());
// 打印密钥条目信息
System.out.println("Key:");
System.out.println(" Algorithm: " + key.getAlgorithm());
System.out.println(" Format: " + key.getFormat());
// 获取证书
X509Certificate certificate = (X509Certificate) keyStore.getCertificate("my-certificate");
// 打印证书条目信息
System.out.println("Certificate:");
System.out.println(" Subject: " + certificate.getSubjectDN());
System.out.println(" Issuer: " + certificate.getIssuerDN());
System.out.println(" Serial number: " + certificate.getSerialNumber());
System.out.println(" Validity: " + certificate.getNotBefore() + " to " + certificate.getNotAfter());
}
提示:KeyStore 文件是用来存储密钥和证书的文件。如果 KeyStore 文件不存在或损坏,则无法读取 KeyStore 中的密钥和证书。