First generate a certificate with Keytool
1 Generating key
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 Export key, save in the form of certificate
D:\>keytool-export-alias wenger-file Mycert.cer-keystore Mykeystore-storepass abcdef
Certificates saved in a file <mycert.cer>
Read the certificate's information in 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 * *
}
}
}
Output:
Certficate Forcn=chen Sr, Ou=tanglab, O=lab, L=gz, ST=GD, C=CN
Generated with SHA1WITHDSA