「java读取证书」java获取证书信息
今天给各位分享java读取证书的知识,其中也会对java获取证书信息进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java如何解读证书里的内容(通过string 来生成X509Certificate对象)
- 2、java代码怎么获取数字的证书那一串20位指纹?
- 3、java怎么去获取证书文件的信息
- 4、如何用Java读取使用证书
java如何解读证书里的内容(通过string 来生成X509Certificate对象)
那个字符串是Base64编码后的
试着把String 转成inputStream,
InputStream inStream = new ByteArrayInputStream(caString.getBytes('UTF-8'));用这句替代 上面写的第二行代码。但是在执行第四行的时候报错。。
java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Unsupported encoding
就是把这个String 写到一个文件里,然后用上面的方法读这个文件。
java代码怎么获取数字的证书那一串20位指纹?
通过JAVA来读取数字证书的方法获取20位指纹:
CARead.java文件代码:
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;
}
}
java怎么去获取证书文件的信息
Java为安全应用提供了丰富的API,J2SDK1.4
的JSSE
(JavaTM
Secure
Socket
Extension)
包括javax.security.certificate包,并且提供对证书的操作方法,代码如下:
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();得到开始有效日期
Field=x509certificate.
getNotAfter().toString();得到截止日期
Field=x509certificate.getSerialNumber().toString(16);得到序列号
Field=x509certificate.getIssuerDN().getName();得到发行者名
Field=x509certificate.getSigAlgName();得到签名算法
Field=x509certificate.getPublicKey().getAlgorithm();得到公钥算法
*/
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;
}
如何用Java读取使用证书
证书(Certificate,也称public-key certificate)是用某种签名算法对某些内容(比如公钥)进行数字签名后得到的、可以用来当成信任关系中介的数字凭证。证书发行机构通过发行证书告知证书使用者或实体其公钥(public-key)以及其它一些辅助信息。证书在电子商务安全交易中有着广泛的应用,证书发行机构也称CA(Certificate Authority)。
应用证书
证书在公钥加密应用中的作用是保证公钥在某些可信的机构发布,其在协议SSL、电子交易协议SET等方面有重要的应用。图1显示了一个最简单的证书应用方法:
图1 证书应用方法
证书的应用步骤是:
(1) A把自己的公钥PKA送到CA(Certificate Authority);
(2) CA用自己的私钥和A的公钥生成A的证书,证书内包括CA的数字签名。签名对象包括需要在证书中说明的内容,比如A的公钥、时间戳、序列号等,为了简化这里不妨假设证书中只有三项内容:A的公钥PKA、时间戳TIME1、序列号IDA。那么CA发送给A的简单证书凭证可表达为:CertA=Eca[TIME1,IDA,PKA];
(3) B同样把自己的公钥PKB送到CA;
(4) B得到CA发布的证书CertB;
(5) A告知B证书CertA;
(6) B告知A证书CertB。
A、B各自得到对方证书后,利用从CA得到的公钥(在CA的自签证书中)验证彼此对方的证书是否有效,如果有效,那么就得到了彼此的公钥。利用对方的公钥,可以加密数据,也可以用来验证对方的数字签名。
本文为了方便说明,并没有使用从CA获得的证书,而是通信双方各自产生自签证书,也就是说图1的A和B并没有经过CA,不过前提是A和B之间是互相拥有对方的证书。
java读取证书的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取证书信息、java读取证书的信息别忘了在本站进行查找喔。
发布于:2022-12-03,除非注明,否则均为
原创文章,转载请注明出处。