Keytool JKS RSA

JKS 文件是一个 Java 中的密钥管理库。

JKS 文件就像一个仓库,可以放很多的东西(密钥)。

  • 仓库当然会有一把锁(JKS 文件的密码),防范别人随便乱拿。
  • 仓库里面存放的密钥也各有不同,每个密钥都有一个名字(别名)。
  • 密钥对包含公钥和私钥。公钥只要能进入仓库就可以查看,私钥则是有密码的,只允许有权限的人查看。

 

Keytool 是一个 JAVA 环境下的安全钥匙与证书的管理工具。

Keytool 将密钥(key)和证书(certificates)存在一个称为 keystore 的文件(受密码保护)中。

在 keystore 里,包含两种数据: 

  • 密钥实体(Key entity)——密钥(secretkey)又或者是私钥和配对公钥(采用非对称加密)
  • 可信任的证书实体(trustedcertificate entries)——只包含公钥

 

生成 JKS 文件

keytool -genkeypair -alias jhxxb -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -validity 365 -keystore jhxxb.jks -keypass keypass -storepass storepass -dname "CN=jhxxb.com, OU=jhxxb, O=jhxxb, L=wuhan, ST=hubei, C=CN"
keytool -genkeypair -alias jhxxb -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -validity 365 -keystore jhxxb.p12 -storetype PKCS12 -storepass storepass -dname "CN=jhxxb.com, OU=jhxxb, O=jhxxb, L=wuhan, ST=hubei, C=CN"

keytool -genkey -alias jhxxb -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -validity 365 -keystore jhxxb.jks -keypass keypass -storepass storepass -dname "CN=jhxxb.com, OU=jhxxb, O=jhxxb, L=wuhan, ST=hubei, C=CN"
keytool -genkey -alias jhxxb -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -validity 365 -keystore jhxxb.p12 -storetype PKCS12 -storepass storepass -dname "CN=jhxxb.com, OU=jhxxb, O=jhxxb, L=wuhan, ST=hubei, C=CN"

会在当前目录下生成 jks 或 p12 文件。
CN=(名字与姓氏), OU=(组织单位名称), O=(组织名称), L=(城市或区域名称),ST=(州或省份名称), C=(国家名称)

keytool -genkeypair -help

生成密钥对

选项:

 -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                      通过受保护的机制的口令

使用 "keytool -help" 获取所有可用命令

 

加载 JKS 文件

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;

@Slf4j
public class RSAUtil {
    public static void main(String[] args) {
        // 从 classpath 下获取 RSA 秘钥对
        KeyPair keyPair = loadKeyPairFromKeystore("jhxxb.jks", "storepass", "keypass", "jhxxb", "jks");
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 获取 RSA 公钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 获取 RSA 私钥
    }

    public static KeyPair loadKeyPairFromKeystore(String keystoreFile, String storePassword, String keyPassword, String keyAlias, String type) {
        try {
            if (type == null) {
                String ext = StringUtils.getFilenameExtension(keystoreFile);
                type = ext == null ? KeyStore.getDefaultType() : ext;
            }
            KeyStore keyStore = KeyStore.getInstance(type);
            @Cleanup InputStream stream = new ClassPathResource(keystoreFile).getInputStream();
            keyStore.load(stream, storePassword.toCharArray());
            // PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, keyPassword.toCharArray());
            RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyStore.getKey(keyAlias, keyPassword.toCharArray());
            if (privateKey == null) {
                throw new RuntimeException("Couldn't load key with alias '" + keyAlias + "' from keystore");
            }
            PublicKey publicKey;
            Certificate certificate = keyStore.getCertificate(keyAlias);
            if (certificate != null) {
                publicKey = certificate.getPublicKey();
            } else {
                RSAPublicKeySpec spec = new RSAPublicKeySpec(privateKey.getModulus(), privateKey.getPublicExponent());
                publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
            }
            return new KeyPair(publicKey, privateKey);
        } catch (Exception e) {
            throw new RuntimeException("Failed to load private key: " + e.getMessage(), e);
        }
    }
}

 


https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator

https://www.hutool.cn/docs/#/crypto/非对称加密-AsymmetricCrypto

https://www.liaoxuefeng.com/wiki/1252599548343744/1304227968188450

https://keystore-explorer.org

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.webserver.configure-ssl

posted @ 2020-06-23 09:31  江湖小小白  阅读(2512)  评论(0编辑  收藏  举报