赞
踩
当前遇到java解析SM2,der编码格式的证书,需要解析里面字段:
- package com.common;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.security.NoSuchProviderException;
- import java.security.Security;
- import java.security.cert.CertificateException;
- import java.security.cert.CertificateFactory;
- import java.security.cert.X509Certificate;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
-
- public class test {
- public static void main(String[] args) {
- Security.addProvider(new BouncyCastleProvider());
- String path="../src/main/resources/certs/oca.cer";
- try {
- FileInputStream fis= new FileInputStream(path);
- CertificateFactory cf =CertificateFactory.getInstance("X.509","BC");
- X509Certificate certificate=(X509Certificate) cf.generateCertificate(fis);
-
- String subjectDN= certificate.getSubjectDN().toString();
- String issuerDN =certificate.getIssuerDN().toString();
- String serialNumber= certificate.getSerialNumber().toString();
-
- System.out.println("subjectDN : "+subjectDN);
- System.out.println("issuerDN : "+issuerDN);
- System.out.println("serialNumber : "+serialNumber);
- } catch (FileNotFoundException |CertificateException |NoSuchProviderException e) {
- e.printStackTrace();
- }
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。