先生成:
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>
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