Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About

Chapter 15. 數字證書開發

Table of Contents

15.1. Java (java.security.*)
15.1.1. 訪問X.509證書
15.1.2. 創建證書
15.2. SSL Socket
15.2.1. Java Socket HTTPS
15.2.2. Java SSL Socket Client
15.2.3. Java SSL Socket Server

15.1. Java (java.security.*)

15.1.1. 訪問X.509證書

Java訪問X.509證書

		
/*
 * Created on 2005-7-1
 *
 * Author: neo chen <openunix@163.com>
 * Nickname: netkiller
 */
import java.io.*;
import java.security.cert.*;
import java.security.cert.CertificateFactory;

public class CertInfo {
	static String issue,after,before,subject;
	static String serialno,signalg;
	static int version;
	public void Init() throws Exception{
	CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
	FileInputStream fis=new FileInputStream("e:/Java/chen.cer");
	X509Certificate cert = (X509Certificate)certFactory.generateCertificate(fis);

	fis.close();
	issue=cert.getIssuerDN().toString();
	subject=cert.getSubjectDN().getName();
	after=cert.getNotAfter().toString();
	before=cert.getNotBefore().toString();
	version=cert.getVersion();
	serialno=cert.getSerialNumber().toString();
	signalg=cert.getSigAlgName();
	}
	public String getIssue(){
	return issue;
	}

	public String getAfter(){
	return after;
	}

	public String getBefore(){
	return before;
	}

	public String getSerial(){
	return serialno;
	}

	public String getsignalg(){
	return signalg;
	}

	public String getsubject(){
	return subject;
	}

	public String getversion(){
	return ("ver:"+version);
	}


	public static void main(String[] args) throws Exception
	{
	CertInfo c=new CertInfo();
	c.Init();
	System.out.println(c.getBefore());
	System.out.println(version);
	System.out.println(c.getversion());
	System.out.println(issue);
	System.out.println(c.getsubject());
	System.out.println(c.getsignalg());
	}
}
		
		

15.1.2. 創建證書