使用BC解析X.509证书

随笔 0 87
邮箱用户_8st46
邮箱用户_8st46 11月2日 23:09 发表
摘要:// 引入BC库 Security.addProvider(new BouncyCastleProvider()); // 使用BC解析X.509证书 CertificateFactory CF = CertificateFactory.getInstance("X.509", "BC"); //

try{
// 引入BC库
Security.addProvider(new BouncyCastleProvider());
// 使用BC解析X.509证书
CertificateFactory CF = CertificateFactory.getInstance("X.509", "BC"); // 从证书工厂中获取X.509的单例类
InputStream fileIn = new FileInputStream("F:\XH3.0\Base64X509.cer"); // 将本地证书读入文件流
Certificate C = CF.generateCertificate(fileIn); // 将文件流的证书转化为证书类
String certificateStr = C.toString();
System.out.println("使用[自带库函数]读入证书结果如下:");
System.out.print(certificateStr);
System.out.println("--------------------------------------\n证书主要字段:");
X509Certificate cer = (X509Certificate)C;
System.out.println("版本号:" + cer.getVersion());
System.out.println("序列号:" + cer.getSerialNumber().toString());
System.out.println("颁发者:" + cer.getSubjectDN()); // System.out.println("颁发者唯一标识符: " + cer.getSubjectUniqueID().toString());
System.out.println("使用者:" + cer.getIssuerDN());
// System.out.println("使用者唯一标识符: " + cer.getIssuerUniqueID().toString());
System.out.println("有效期:from:" + cer.getNotBefore() + " to: " + cer.getNotAfter());
System.out.println("签发算法" + cer.getSigAlgName());
System.out.println("签发算法ID:" + cer.getSigAlgOID());
System.out.println("证书签名:" + cer.getSignature().toString());
byte [] sig = cer.getSigAlgParams();
PublicKey publicKey = cer.getPublicKey();
byte [] pkenc = publicKey.getEncoded();
System.out.println("解析出的公钥:" + Base64.getEncoder().encodeToString(pkenc));
System.out.println("公钥:");
for(int i = 0; i < pkenc.length; i++){
System.out.print(pkenc[i]);
}
} catch(Exception e){
e.printStackTrace();
}

点赞 0 收藏(0)    分享
相关标签: 证书
0个评论
  • 消灭零评论