Posted on 2005-09-19 21:54
wenger的天空 閱讀(2654)
評論(0) 編輯 收藏 所屬分類:
Security
首先用keytool生成證書
1 產(chǎn)生密鑰
D:\>keytool -genkey -alias wenger -keysize 1024 -keypass abcdef -keystore myKeys
tore -storepass abcdef -dname "CN=chen sr, OU=tanglab, O=lab, L=gz, ST=gd, C=cn"
2 導(dǎo)出密鑰,用證書的形式保存
D:\>keytool -export -alias wenger -file mycert.cer -keystore myKeystore -storepass abcdef
保存在文件中的認(rèn)證 <mycert.cer>
用java讀出證書的信息

import java.io.*;
import java.security.cert.*;


public class TestCert
{

public static void main(String[] args)
{

try
{
FileInputStream fis = new FileInputStream("mycert.cer");
CertificateFactory cf=CertificateFactory.getInstance("X509");
X509Certificate c=(X509Certificate) cf.generateCertificate(fis);
System.out.println("Certficate for" + c.getSubjectDN());
System.out.println("Generated with "+c.getSigAlgName());
}

catch (FileNotFoundException ex)
{
}

catch (CertificateException ex)
{

/** *//** @todo Handle this exception */
}

}
}
輸出:
Certficate forCN=chen sr, OU=tanglab, O=lab, L=gz, ST=gd, C=cn
Generated with SHA1withDSA