通过JAVA来读取数字证书

CARead_Demo.java文件
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;


public class CARead_Demo {
     CARead_Demo()
     {
      JFrame frame = new JFrame("证书阅读器");
             frame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {System.exit(0);}
             });
             frame.getContentPane().add(new CARead("robin.crt"),BorderLayout.CENTER);
             frame.setSize(700, 425);
             frame.setVisible(true);
     }
}
CARead.java文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.io.*;

public class CARead extends JPanel {
private String  CA_Name;
private String  CA_ItemData[][] = new String[9][2];
private String[]  columnNames = { "证书字段标记", "内容" };

public CARead(String CertName) {
CA_Name = CertName;
/* 三个Panel用来显示证书内容 */
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panelNormal = new JPanel();
tabbedPane.addTab("普通信息", panelNormal);
JPanel panelAll = new JPanel();
panelAll.setLayout(new BorderLayout());
tabbedPane.addTab("所有信息", panelAll);
JPanel panelBase64 = new JPanel();
panelBase64.setLayout(new BorderLayout());
tabbedPane.addTab("Base64编码形式的信息", panelBase64);
/* 读取证书常规信息 */
Read_Normal(panelNormal);
/* 读取证书文件字符串表示内容 */
Read_Bin(panelAll);
/* 以Base64编码形式读取证书文件的信息 */
Read_Raw(panelBase64);
tabbedPane.setSelectedIndex(0);
setLayout(new GridLayout(1, 1));
add(tabbedPane);
}
private int Read_Normal(JPanel panel) {
String Field;
try {
CertificateFactory certificate_factory = CertificateFactory
.getInstance("X.509");
FileInputStream file_inputstream = new FileInputStream(CA_Name);
X509Certificate x509certificate = (X509Certificate) certificate_factory
.generateCertificate(file_inputstream);
Field = x509certificate.getType();
CA_ItemData[0][0] = "类型";
CA_ItemData[0][1] = Field;
Field = Integer.toString(x509certificate.getVersion());
CA_ItemData[1][0] = "版本";
CA_ItemData[1][1] = Field;
Field = x509certificate.getSubjectDN().getName();
CA_ItemData[2][0] = "标题";
CA_ItemData[2][1] = Field;
Field= x509certificate.getNotBefore().toString();//得到开始有效日期
CA_ItemData[3][0] = "开始有效日期";
CA_ItemData[3][1] = Field;
Field= x509certificate. getNotAfter().toString();//得到截止日期
CA_ItemData[4][0] = "截止日期";
CA_ItemData[4][1] = Field;
Field= x509certificate.getSerialNumber().toString(16);//得到序列号
CA_ItemData[5][0] = "序列号";
CA_ItemData[5][1] = Field;
Field=x509certificate.getIssuerDN().getName();//得到发行者名
CA_ItemData[6][0] = "发行者名";
CA_ItemData[6][1] = Field;
Field=x509certificate.getSigAlgName();//得到签名算法
CA_ItemData[7][0] = "签名算法";
CA_ItemData[7][1] = Field;
Field =x509certificate.getPublicKey().getAlgorithm();//得到公钥算法
CA_ItemData[8][0] = "公钥算法";
CA_ItemData[8][1] = Field;  
file_inputstream.close();
final JTable table = new JTable(CA_ItemData, columnNames);
TableColumn tc = null;
tc = table.getColumnModel().getColumn(1);
tc.setPreferredWidth(600);
panel.add(table);
catch (Exception exception) {
exception.printStackTrace();
return -1;
}
return 0;
}

private int Read_Bin(JPanel panel) {
try {
FileInputStream file_inputstream = new FileInputStream(CA_Name);
DataInputStream data_inputstream = new DataInputStream(
file_inputstream);
CertificateFactory certificatefactory = CertificateFactory
.getInstance("X.509");
byte[] bytes = new byte[data_inputstream.available()];
data_inputstream.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
JEditorPane Cert_EditorPane;
Cert_EditorPane =  new JEditorPane();
X509Certificate cert= null;
//遍历得到所有的证书属性
                       if (bais.available() > 0)
                       {
                         cert = (X509Certificate) certificatefactory .generateCertificate(bais); 
                        Cert_EditorPane.setText( cert.toString())
                       }
Cert_EditorPane.disable();
JScrollPane edit_scroll = new JScrollPane(Cert_EditorPane);
panel.add(edit_scroll);
file_inputstream.close();
data_inputstream.close();
catch (Exception exception) {
exception.printStackTrace();
return -1;
}
return 0;
}
private int Read_Raw(JPanel panel) {
try {
JEditorPane Cert_EditorPane = new JEditorPane();
StringBuffer strBuffer =new StringBuffer();
File inputFile = new File(CA_Name);
FileReader in = new FileReader(inputFile);
char[] buf = new char[2000];
int len = in.read(buf, 0, 2000);
for (int i = 1; i < len; i++) {
strBuffer.append(buf[i]);
}
in.close();
Cert_EditorPane.setText(strBuffer.toString());
Cert_EditorPane.disable();
JScrollPane edit_scroll = new JScrollPane(Cert_EditorPane);
panel.add(edit_scroll);
catch (Exception exception) {
exception.printStackTrace();
return -1;
}
return 0;
}
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值