通过keytool生成证书,并提取公钥和私钥

1、生成证书

keytool -genkeypair -alias abc -keyalg RSA -keypass abc -keystore abc.jks -storepass abc -validity 3650

 

keytool -genkeypair -alias home -storetype PKCS12  -keyalg RSA -keystore home.pkcs12 -storepass 13987664391 -validity 3650 -keysize 2048 

 

 

keytool命令

keytool密钥和证书管理工具

命令:

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

使用 "keytool -command_name -help" 获取 command_name 的用法

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

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

2、提取公钥

2.1 用keytool只能提取cert格式的公钥

keytool -list -rfc -keystore abc.jks -storepass abc

显示如下:

密钥库类型: jks
密钥库提供方: SUN

您的密钥库包含 1 个条目

别名: abc
创建日期: 2019-5-18
条目类型: PrivateKeyEntry
证书链长度: 1
证书[1]:
-----BEGIN CERTIFICATE-----
MIIDXzCCAkegAwIBAgIEQ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vBQfE8
bqZrk/bvyt5lPnmpZJBthiEe/C3mMmsLwsUTN7jsIldtJaZ1lffmqar3bqxTxRdc
w60x
-----END CERTIFICATE-----

2.2 通过openssl可以提取PUBLIC KEY格式,windows下需要安装 openssl:http://slproweb.com/products/Win32OpenSSL.html

通过通道符号把cert格式转成public key格式
keytool -list -rfc --keystore wood.jks | openssl x509 -inform pem -pubkey

输出显示:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvFsEiaLvij9C1Mz+oyAm
t47whAaRkRu/8kePM+X8760UGU0RMwGti6Z9y3LQ0RvK6I0brXmbGB/RsN38PVnh
cP8ZfxGUH26kX0RK+tlrxcrG+HkPYOH4XPAL8Q1lu1n9x3tLcIPxq8ZZtuIyKYEm
oLKyMsvTviG5flTpDprT25unWgE4md1kthRWXOnfWHATVY7Y/r4obiOL1mS5bEa/
iNKotQNnvIAKtjBM4RlIDWMa6dmz+lHtLtqDD2LF1qwoiSIHI75LQZ/CNYaHCfZS
xtOydpNKq8eb1/PGiLNolD4La2zf0/1dlcr5mkesV570NxRmU1tFm8Zd3MZlZmyv
9QIDAQAB
-----END PUBLIC KEY-----
-----BEGIN CERTIFICATE-----
MIIDXzCCAkegAwIBAgIEQkT0ojANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJj
bjELMAkGA1UECBMCZ2QxCzAJBgNVBAcTAnN6MREwDwYDVQQKEwhjaGFuZ2dvdTER
MA8GA1UECxMIY2hhbmdnb3UxETAPBgNVBAMTCGNoYW5nZ291MB4XDTE5MDUxNzIw
MDMyNFoXDTE5MDgxNTIwMDMyNFowYDELMAkGA1UEBhMCY24xCzAJBgNVBAgTAmdk
MQswCQYDVQQHEwJzejERMA8GA1UEChMIY2hhbmdnb3UxETAPBgNVBAsTCGNoYW5n
Z291MREwDwYDVQQDEwhjaGFuZ2dvdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBALxbBImi74o/QtTM/qMgJreO8IQGkZEbv/JHjzPl/O+tFBlNETMBrYum
fcty0NEbyuiNG615mxgf0bDd/D1Z4XD/GX8RlB9upF9ESvrZa8XKxvh5D2Dh+Fzw
C/ENZbtZ/cd7S3CD8avGWbbiMimBJqCysjLL074huX5U6Q6a09ubp1oBOJndZLYU
Vlzp31hwE1WO2P6+KG4ji9ZkuWxGv4jSqLUDZ7yACrYwTOEZSA1jGunZs/pR7S7a
gw9ixdasKIkiByO+S0GfwjWGhwn2UsbTsnaTSqvHm9fzxoizaJQ+C2ts39P9XZXK
+ZpHrFee9DcUZlNbRZvGXdzGZWZsr/UCAwEAAaMhMB8wHQYDVR0OBBYEFDlS24hM
6d3CpJUW4QNA91iPVq+TMA0GCSqGSIb3DQEBCwUAA4IBAQByLqfGerXnoxT4roBo
8/PY7+Ir1lQfpQWSYWIDcY7+ek5c7AGqGOk5dVG4GxdgsCdCmNM9VlcS7r2jlmD1
tO4g2MABbYWKkaVunSdYm73KhF4ktz+QPrBnUKisZnE6wciPl+MBe6J61uJR47Sx
Gd/bbqwr7JjfpvepAux3NVNaL6YN8K4M4Zwapu+GTiwNyoBXk6bTPp4fdgWKpW6X
i+DX64vC0WVqiQSRMSIZ+1RzPBRjaOXQklJuFbYIbnez9eVd1I264ziUIUvBQfE8
bqZrk/bvyt5lPnmpZJBthiEe/C3mMmsLwsUTN7jsIldtJaZ1lffmqar3bqxTxRdc
w60x
-----END CERTIFICATE-----

3、提取私钥

3.1 需要把证书先转成pfx格式

keytool -v -importkeystore -srckeystore abc.jks -srcstoretype jks -srcstorepass abc -destkeystore abc.pfx -deststoretype pkcs12 -deststorepass abc -destkeypass abc

3.2 提取私钥

显示在屏幕上
openssl pkcs12 -in abc.pfx -nocerts -nodes

输出到文件
openssl pkcs12 -in abc.pfx -nocerts -nodes -out abc.key

 

显示:

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8WwSJou+KP0LU
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jYtM8DJRJL77NQVscyTU6eo1M
GMN8gapVH29nxt+bHJa3jIiJ/bf/C35cmGSSl90zJBWGmgAWD0kdlu4IWyW++QfJ
zPmppPwWJ3CvfZbp8j4zGHrvC4Xunuf0xaZdAmNGB+FRptsLGhWO2UmBAoGAO8zV
lgVM7NYm7SdfN1C1UvN21N3+YB8HXws9a5RxOtHlrkmRVOyM+HBD6sFs21KFKziX
hEfA+eP/YPGzTGYU4h7/Ydg4sftLvqRdPrl5sgAVjAeJ7GbfKh2E8xr9ce8VDsIH
svBo3SGXTL1MgXeLAKucj60Q/bNxC+fhAYeJq5kCgYEAlhguSbZeJ8jGVzZpl1FI
2B5OMSgs45IUoOKtXYSFKC4JuVZiZ+LCEqsGnnakSi2bWSLjx54g6umWx/BLI3U/
zqVuCPw2UkWWjs0jghOeRTxMqX+WJSuZqmaZrtPgrkSmCiGSWEhfTVINLNOgFBMH
wMSf0XsGgUlP9aA0yD6qPzU=
-----END PRIVATE KEY-----

 

posted @ 2020-07-21 14:01  我是属车的  阅读(11962)  评论(0编辑  收藏  举报