keytool 是个密钥和证书管理工具
keytool 是个密钥和证书管理工具。它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务。它还允许用户储存他们的通信对等者的公钥(以证书形式)。百度百科
名词解释
- 密钥仓库 用于存储证书和密钥(非对称密钥以及对称密钥)的文件。可以存放多组密钥和证书。
- 证书 也即是数字证书。是一个经证书授权中心数字签名的包含公开密钥拥有者信息以及公开密钥的文件。
- 密钥 即非对称密钥对(公钥以及私钥)与对称密钥的统称
- 别名 即密钥仓库项(证书或密钥)的别名,必须是唯一的,其他操作时必须使用别名来区分。
keytool 的基本用法
keytool参数不区分大小写;在不进行设置的时候有些信息都会使用缺省值。
-alias mykey
-keystore 你的用户文目录下的.keystore文件
(windows:C:\Users\xxx\.keystore,linux:/home/xxx/.keystore)
-keyalg
"DSA" (when using -genkeypair)
"DES" (when using -genseckey)
-keysize
2048 (when using -genkeypair and -keyalg is "RSA")
1024 (when using -genkeypair and -keyalg is "DSA")
256 (when using -genkeypair and -keyalg is "EC")
56 (when using -genseckey and -keyalg is "DES")
168 (when using -genseckey and -keyalg is "DESede")
-file
stdin (if reading)
stdout (if writing)
-validity 90
-protected false
-storetype java.security文件中的keystore.type属性
java.security文件位于jdk或jre的安装目录下\lib\security\
1)产生一个密钥对(公钥和私钥)
keytool -genkeypair
在这种情况下,会使用交互的方式来询问你一些信息(包括密码等),对于有些值,如果不指定会使用缺省值,如果不指定 -keystore ,会默认存储在你的用户文件夹下的.keystore文件
keytool -genkeypair -alias shaozuoKey -keystore shaozuo.keystore -keypass shaozuo
也可以一次性把所有信息写入,不需要进行交互输入。比如
keytool -genkey -keystore "laozhai.keystore" -alias laozhai -keyalg RSA -validity 365 -dname "CN=laozhai, OU=laozhai, O=laozhai, L=Beijing, ST=Beijing, C=CN" -keypass laozhai -storepass laozhai
2)生成一个秘密密钥(对称密钥)
keytool -genseckey -alias shaoSec -storetype jceks -keystore shaozuoSec.keystore -storepass shaozuoSec
需要注意到的是,秘密密钥不能存储在jks格式中,只能存储在jceks格式中,所以我们必须指定密钥存储类型(在我的系统中,密钥存储类型默认文jks),否则会提示错误:
keytool 错误: java.security.KeyStoreException: Cannot store non-PrivateKeys
3)keystore信息的查看
对于上述生成的keystore,我们可以使用以下命令查看其信息
keytool -list -keystore shaozuo.keystore -storepass shaozuo
显示的信息如下:
4)PKCS12存储格式
我们看到警告建议我们使用PKCS12格式。构建PKCS12存储格式的密钥命令如下:
keytool -list -keystore shaozuoP.keystore -storepass shaozuoP -storetype pkcs12
显示仓库信息:
keytool -list -keystore shaozuoP.keystore -storepass shaozuoP -storetype pkcs12
7)导出证书
之前我们生成了公钥和私钥,现在我们可以用命令将公钥导出来。
keytool -alias shaozuoKey -exportcert -keystore shaozuo.keystore -file shaozuo_pub.cer -storepass shaozuo
在windows中,导出来的证书可以直接打开,我们也可以将证书转换为其他格式,比如pem、p7b等。
使用windows打开证书
Base64编码的文件
还有p7p格式等
8)导入证书
在上一节中,我们将证书导出到了shaozuo_pub.cer文件,现在我们来讲shaozuo_pub.cer导入到test_cacerts中
keytool -import -keystore test_cacerts -file shaozuo_pub.cer -storepass test_cacerts
输出信息为:
9)删除证书
我们可以使如下命令将证书删除
keytool -delete -alias shaozuoKey -keystore shaozuo.keystore -storepass shaozuo
删除之后查看证书文件:
keytool -list -keystore shaozuo.keystore -storepass shaozuo
10)修改keystore密码
使用如下命令修改keystore密码,其中 -storepass 表示原密码 -new 后接 新密码
keytool -storepasswd -keystore shaozuo.keystore -storepass shaozuo -new zuoshao
11)修改证书的密码
使用如下命令修改证书的密码,其中 -keypass 表示原密码 -new 后接 新密码
keytool -keypasswd -alias shaozuoKey -keystore shaozuo.keystore -keypass shaozuo -new zuoshao
12)修改证书的别名
使用-changealias命令修改别名:
keytool -changealias -keystore shaozuo.keystore -alias shaozuoKey -destalias zuoshaoKey
shaozuoKey 是证书中当前的alias,-destalias指定的是要修改为的alias 比如 zuoshaoKey
参考资料
- Oracle keytool 介绍 官网地址
- Keytool 百度百科