keytool 工具介绍

使用JDK自带的 keytool 工具:

简介

keytool 命令是一个密钥和证书管理的工具。它允许用户使用数字签名管理自己的公钥/私钥对和相关证书,用于自我身份验证(向其他用户和服务验证自己)或数据完整性和身份验证服务。keytool 命令还允许用户缓存通信对等体的公钥(以证书的形式)。

证书是来自一个实体(个人、公司等)的数字签名声明,它表示实体的公钥(和一些其他信息)具有特定的值。对数据进行数字签名时,可以对签名进行验证,以验证数据的完整性和真实性。完整性意味着数据没有被修改或篡改,真实性意味着数据来自声称创建并签名它的人。

keytool 命令还允许用户管理对称加密和解密中使用的秘密密钥和口令。

keytool 命令将密钥和证书存储在keystore中。

Keytool密钥存储形式

Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中。也就是说,在keystore里只包含两种数据:

  1. 密钥实体:如果采用非对称加密形式,则包含私钥和配对公钥,否则只包括密钥。
  2. 可信任的证书实体:只包含公钥
keytool 位置

在这里插入图片描述

keytool 常用命令
C:\Users\Administrator>keytool
密钥和证书管理工具
 
命令:
 
 -certreq            生成证书请求
 -changealias        更改条目的别名
 -delete             删除条目
 -exportcert         导出证书
 -genkeypair         生成密钥对
 -genseckey          生成密钥
 -gencert            根据证书请求生成证书
 -importcert         导入证书或证书链
 -importpass         导入口令
 -importkeystore     从其他密钥库导入一个或所有条目
 -keypasswd          更改条目的密钥口令
 -list               列出密钥库中的条目
 -printcert          打印证书内容
 -printcertreq       打印证书请求的内容
 -printcrl           打印 CRL 文件的内容
 -storepasswd        更改密钥库的存储口令

查看" -genkeypair"命令的参数,该命令的可用参数如下:

C:\Users\Administrator>keytool -genkeypair -help
keytool -genkeypair [OPTION]...
 
生成密钥对
 
选项:
 
 -alias <alias>                  要处理的条目的别名
 -keyalg <keyalg>                密钥算法名称
 -keysize <keysize>              密钥位大小
 -sigalg <sigalg>                签名算法名称
 -destalias <destalias>          目标别名
 -dname <dname>                  唯一判别名
 -startdate <startdate>          证书有效期开始日期/时间
 -ext <value>                    X.509 扩展
 -validity <valDays>             有效天数
 -keypass <arg>                  密钥口令
 -keystore <keystore>            密钥库名称
 -storepass <arg>                密钥库口令
 -storetype <storetype>          密钥库类型
 -providername <providername>    提供方名称
 -providerclass <providerclass>  提供方类名
 -providerarg <arg>              提供方参数
 -providerpath <pathlist>        提供方类路径
 -v                              详细输出
 -protected                      通过受保护的机制的口令
 

参数默认值:

The following examples show the defaults for various option values.
 
-alias "mykey"
 
-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")
 
-validity 90
 
-keystore <the file named .keystore in the user's home directory>
 
-storetype <the value of the "keystore.type" property in the
    security properties file, which is returned by the static
    getDefaultType method in java.security.KeyStore, that is JKS>
 
-file
    stdin (if reading)
    stdout (if writing)
 
-protected false
 
-sigalg
    "SHA1withDSA" (when the underlying private key is of type DSA)
    "SHA256withRSA" (when the underlying private key is of type RSA)
    "SHA256withECDSA" (when the underlying private key is of type EC)

常用命令

创建密钥对

使用“keytool -genkeypair"命令会生成一个密钥对(公钥和私钥)并将公钥包装到X.509 v3自签名证书中,密钥对存储在以别名标识的条目中,条目类型为 PrivateKeyEntry。

例子:生成一个名称为example的证书

 keytool -genkeypair -alias "example" -keyalg "RSA" -keystore "example.keystore"  
  • 功能:
    创建一个别名为example的证书,该证书存放在名为example.keystore的密钥库中,若example.keystore密钥库不存在则创建。
  • 参数说明:
    -genkeypair:生成一对非对称密钥;
    -alias:指定密钥对的别名,该别名是公开的;
    -keyalg:指定加密算法,本例中的采用通用的RAS加密算法;
    -keystore:密钥库的路径及名称,不指定的话,默认在操作系统的用户目录下生成一个".keystore"的文件
    -keypass :密钥的密码
    -storepass :秘钥库的密码
创建密钥
keytool -genseckey -alias test1 -keystore /home/mongo/test2.keystore -storetype jceks

创建密钥时,-storetype 需指定为 jceks,否则会报错。

查看密钥库信息
keytool -list -v -keystore /home/mongo/test.keystore

指定密钥库的位置,查看该密钥库的条目信息。

导出证书
keytool -exportcert -keystore /home/mongo/test.keystore -alias test1 -file test1.cer

指定密钥库及需导出证书的条目,导出证书到指定的文件;执行命令后当前目录下生成了 test1.cer 的证书文件。

打印证书内容
keytool -printcert -file test1.cer

也可以使用 -sslserver <server[:port]> 参数,直接从网络上打印某个 ssl server 的证书内容 。

导入证书
keytool -importcert -keystore /home/mongo/test.keystore -file test1.cer -alias test2

执行命令后会在密钥库中增加一个条目类型为 trustedCertEntry,别名为 test2 的条目。

删除条目
keytool -delete -keystore /home/mongo/test.keystore -alias test2

执行命令后别名为 test2 的条目将被删除。

签发证书

先使用一个条目生成证书请求,再使用另外一个条目根据证书请求生成证书。

A、生成证书请求
keytool -certreq -keystore /home/mongo/test.keystore -alias test1 -file test1.csr

使用别名为 test1 的条目生成证书请求文件 test1.csr。

B、创建另一个密钥对用于签发证书
keytool -genkeypair -alias ca -keyalg RSA -keystore /home/mongo/test.keystore -storetype pkcs12
C、使用别名为 ca 的条目签发证书
keytool -gencert -alias ca -keystore /home/mongo/test.keystore -infile test1.csr -outfile test2.cer

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值