Posted on 2005-09-19 21:54
wenger的天空 阅读(2640)
评论(0) 编辑 收藏 所属分类:
Security
首先用keytool生成证书
1 产生密钥
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:\>keytool -export -alias wenger -file mycert.cer -keystore myKeystore -storepass abcdef
保存在文件中的认证 <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