1.生成一个名称为zhanghaiwen的证书
keytool -genkeypair -alias "zhanghaiwen" -keyalg "RSA" -keystore "zhanghaiwen.keystore"
输入口令
2.在bin文件下生成了 zhanghaiwen.keystore 文件
然后将文件拷贝到D:\zhengshu 目录下
3.使用export命令导出cer证书
将别名为zhanghaiwen的 位于 D:\zhengshu\的文件 用密码为 zhanghaiwen 的文件导出到 D:\zhengshu\zhanghaiwen.cer
keytool -export -alias "zhanghaiwen" -keystore D:\zhengshu\zhanghaiwen.keystore -storepass "zhanghaiwen" -rfc -file D:\zhengshu\zhanghaiwen.cer
因为keytool不提供命令导出私钥,所以需要编写java类导出key,测试类如下:
public class GetKey {
public static KeyStore getKeyStore(String keyStorePath, String password) throws Exception {
FileInputStream is = new FileInputStream(keyStorePath);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(is, password.toCharArray());
is.close();
return ks;
}
public static PrivateKey getPrivateKey() {
try {
BASE64Encoder encoder = new BASE64Encoder();
KeyStore ks = getKeyStore("D:\\zhengshu\\zhanghaiwen.keystore", "zhanghaiwen");
PrivateKey key = (PrivateKey) ks.getKey("zhanghaiwen", "zhanghaiwen".toCharArray());
String encoded = encoder.encode(key.getEncoded());
System.out.println("-----BEGIN RSA PRIVATE KEY-----");
System.out.println(encoded);
System.out.println("-----END RSA PRIVATE KEY-----");
return key;
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
getPrivateKey();
}
}
最后打印:
到这一步,已经可以将cer证书(若需要crt证书,可以直接把cer证书文件的后缀改为crt即可)配置到nginx中使用