Java Keytool 介绍

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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

注意:

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

3.2、创建密钥

keytool -genseckey -alias test1 -keystore /home/mongo/test2.keystore -storetype jceks

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

3.3、查看密钥库信息

keytool -list -v -keystore /home/mongo/test.keystore

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

3.4、导出证书

keytool -exportcert -keystore /home/mongo/test.keystore -alias test1 -file test1.cer

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

3.5、打印证书内容

keytool -printcert -file test1.cer

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

3.6、导入证书

keytool -importcert -keystore /home/mongo/test.keystore -file test1.cer -alias test2

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

3.7、删除条目

keytool -delete -keystore /home/mongo/test.keystore -alias test2

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

3.8、签发证书

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

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

4、Java API 操作密钥库

可以通过 Java 代码来访问密钥库,读取所需要的信息。

package com.abc.demo.general.keystore;

import java.io.FileInputStream;
import java.security.*;
import java.security.cert.X509Certificate;

public class KeystoreCase {
    public static void main(String[] args) throws Exception {
        KeyStore keyStore = loadKeyStore("pkcs12", "d:/test.keystore", "123456");
        //读取私钥
        PrivateKey privateKey = (PrivateKey) keyStore.getKey("test1", "123456".toCharArray());
        System.out.println("privateKey:" + privateKey);
        //读取证书
        X509Certificate certificate = (X509Certificate)keyStore.getCertificate("test1");
        System.out.println("certificate:" + certificate);
        //读取公钥
        PublicKey publicKey = certificate.getPublicKey();
        System.out.println("publicKey:" + publicKey);

        //获得公钥及私钥后,就可以通过公钥及私钥进行加解密操作、签名及验签操作。
    }

    private static KeyStore loadKeyStore(String type, String filePath, String password) {
        KeyStore keySotre = null;
        try (FileInputStream in = new FileInputStream(filePath)) {
            keySotre = KeyStore.getInstance(type);
            keySotre.load(in, password.toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keySotre;
    }
}

 

 

参考:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html#CHDFFCBG

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java