KeyTool证书生成与OpenSSL转换

一、证书生成方式

  1. 使用Java JDK自带的KeyTool工具;
  2. 使用OpenSSL生成;

二、证书工具安装及使用

1. KeyTool安装

  1. JDK自带的KeyTool是随着JDK安装的。只要配置好JDK环境变量,KeyTool就可正常使用;

2. OpenSSL安装

  1. 鉴于最近一直在使用Windows 10,所以需要在Windows上安装OpenSSL;
  2. 参考文章的第二种方式,主要命令如下:
    1)安装最新版的ActivePerl
    2)到https://www.openssl.org 下载最新OpenSSL源码
    3)到http://www.nasm.us 下载NASM汇编工具
    4)下载并安装VS 2010;
    5)安装上面参考的文档配置好环境变量;
    6)进入解压的OpenSSL根目录,执行如下命令(忽略perl Configure VC-WIN64A报错):
perl Configure VC-WIN64A
nmake 
nmake test 
nmake install

可以忽略的报错如下:

c:\openssl-OpenSSL_1_1_1>perl Configure VC-WIN64A
Configuring OpenSSL version 1.1.1 (0x1010100fL) for VC-WIN64A
Using os-specific seed configuration

It looks like you don't have either nmake.exe or dmake.exe on your PATH,
so you will not be able to execute the commands from a Makefile.  You can
install dmake.exe with the Perl Package Manager by running:

    ppm install dmake

Creating configdata.pm
Creating makefile

**********************************************************************
***                                                                ***
***   If you want to report a building issue, please include the   ***
***   output from this command:                                    ***
***                                                                ***
***     perl configdata.pm --dump                                  ***
***                                                                ***
**********************************************************************

7)执行完成后,在C:\Program Files下会生成OpenSSL安装目录,然后把C:\Program Files\OpenSSL\bin配置至Path环境变量;
8)重新打开cmd工具,执行openssl命令及正确的效果如下:

c:\openssl-OpenSSL_1_1_1>openssl
OpenSSL>

9)在使用Visual Studio x64 Win64 命令提示(2010)工具时(注:其它文档中好多都叫x64本机工具),记得以管理员身份打开并执行如下的命令;

三、证书生成过程

1、生成秘钥文件,有效期3年

keytool -genkeypair -alias stlicense -keypass test123 -storepass test@123! -dname "CN=license.test.com, OU=dev, O=test, L=Shenzhen, ST=Guangdong, C=CN" -keyalg RSA -keysize 2048 -validity 1095 -keystore server2.keystore

2、查看秘钥详情:

keytool -list -keystore server2.keystore  -storepass test@123! -v

3、根据秘钥导出cer证书

keytool -exportcert -keystore server2.keystore -file server2.cer -alias stlicense -storepass test@123!

4、输出PEM编码格式的证书

keytool -exportcert -keystore server2.keystore -rfc -file pem_server2.cer -alias stlicense -storepass test@123!

5、导入信任证书(SSL客户端使用)

keytool -importcert -keystore client_trust.keystore -file server2.cer -alias stlicense -storepass test@123! -noprompt

6、查看证书详情:

keytool -printcert -file server2.cer -v

7、jks格式转pkcs12

keytool -importkeystore -srckeystore server2.keystore -destkeystore server2.p12 -srcalias stlicense -destalias stserver -srcstoretype jks -deststoretype pkcs12 -srcstorepass test@123! -deststorepass test@321! -destkeypass test123 -noprompt

8、p12 证书提取pem证书和私钥

openssl pkcs12 -in server2.p12 -clcerts -nokeys -password pass:test@321! -out server.crt
openssl pkcs12 -in server2.p12  -nocerts -password pass:test@321! -passout pass:test@321! -out server2.key

9、不带密码的配置pem证书和私钥

openssl pkcs12 -in server2.p12 -clcerts -nokeys -out server3.crt
openssl pkcs12 -in server2.p12  -nocerts -out server3.key

四、总结

  1. OpenSSL和KeyTool都能够生成证书,OpenSSL功能更强大,更复杂(本文使用较少),二者相互转化比较容易;
  2. 证书是Java Web开发绕不过去的坎,无论是https、nginx、webservice、jms等,至少需要了解其原理和生成过程;

五、参考资料

[1] https://www.cnblogs.com/Savcry/p/9600117.html
[2] https://www.cnblogs.com/isylar/p/10002117.html
[3] https://www.cnblogs.com/littleatp/p/5922362.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值