JAVA 读出证书的信息

先生成:

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>


importjava.io.*;

import java.security.cert. * ;

public class TestCert {
publicstaticvoidmain(String[]args){
try{
FileInputStreamfis
=newFileInputStream("mycert.cer");
CertificateFactorycf
=CertificateFactory.getInstance("X509");
X509Certificatec
=(X509Certificate)cf.generateCertificate(fis);
System.out.println(
"Certficatefor"+c.getSubjectDN());
System.out.println(
"Generatedwith"+c.getSigAlgName());
}

catch(FileNotFoundExceptionex){
}

catch(CertificateExceptionex){
/**@todoHandlethisexception*/
}


}

}



输出:
Certficate forCN=chen sr, OU=tanglab, O=lab, L=gz, ST=gd, C=cn

Generated with SHA1withDSA



你可能感兴趣的:(java)