设为首页 加入收藏

TOP

Java Keytool 介绍(一)
2023-07-25 21:42:02 】 浏览:60
Tags:Java Keytool 介绍

keytool 是 Java 自带的一个安全相关的工具,用于管理密钥和证书;本文主要介绍其基本使用;文中所使用到的软件版本:Java 1.8.0_321。

1、简介

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

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

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

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

2、用法

keytool [commands]

命令如下:

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

可以使用如下命令查看每个命令的用法:

keytool -command_name -help

命令的参数有些是有默认值的,使用命令时可以不指定:

-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>
 
-file
    stdin (if reading)
    stdout (if writing)
 
-protected false

-sigalg
    "SHA256withDSA" (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)

3、具体使用

3.1、创建密钥对

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

使用命令 keytool -genkeypair -help 查看用法:

keytool -genkeypair [OPTION]...

生成密钥对

选项:

 -alias <alias>                  要处理的条目的别名
 -keyalg <keyalg>                密钥算法名称
 -keysize <keysize>              密钥位大小
 -groupname <name>               Group name. For example, an Elliptic Curve name.
 -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                      通过受保护的机制的口令

下面创建一个别名为 test1 的条目:

keytool -genkeypair -alias test1 -keyalg RSA -keystore /home/mongo/test.keystore -storetype pkcs12

首先提示设置密钥库的密码,然后输入 CN(公用名)、OU(组织单位)、O(组织)、L(城市)、ST(省份)、 C(国家) ,最后回车。

注意:

1、如果指定的密钥库不存在,则会自动创建,需要设置密钥库口令;如果密钥库存在需要输入密钥口令。
2、如果创建默认类型(JKS)的密钥库,则可使用"-keypass"参数指定条目的密钥口令,如果没有指定则会在最后一步提示"输入该条目的密钥口令,(如果与密钥库口令相同按回车)",一般设为与密钥库口令相同。如果创建 PKCS12 类型的密钥库,则会忽略条目的密钥口令参数,因为 PKCS12 不支持设置密钥库条目密钥口令,默认它与密钥库密码一致。

3.2、创建密钥

keytool -genseckey -alias test1 -keystore /home/mongo/test2.keystore -sto
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java中的基本类型 下一篇通过Google Cloud Storage(GCS)管..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目