Java Keytool工具简介

Posted firsttry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Keytool工具简介相关的知识,希望对你有一定的参考价值。

随着信息安全的重要性的日益提高,HTTP/FTP等越来越多的被迁到了 HTTPS/SFTP,SSL/TLS已经是避无可避。了解一些简单的加密算法的基本理论或者常见工具,或者如何生成和使用证书,这些都在工作中应用的愈加广泛。Java自带的Keytool工具就是这样的一种工具,被广泛地用于管理密钥和证书。

前提

keytool工具是JDK自带的工具,所以前提就是安装JDK。具体可以参照Maven的安装脚本,安装Maven的前提是安装JDK,所以下面的脚本执行之后,JDK自然会被安装。已经有JDK的可以跳过此步。

安装确认

确认keytool可用

[root@liumiaocn ~]# which keytool
/usr/local/java/jdk1.8.0_121/bin/keytool
[root@liumiaocn ~]#
  • 1
  • 2
  • 3

命令说明

keytool命令以及常见的option如下所示:

项目详细
-certreq Generates a certificate request
-changealias Changes an entry’s alias
-exportcert Exports certificate
-genkeypair Generates a key pair
-genseckey Generates a secret key
-gencert Generates certificate from a certificate request
-importcert Imports a certificate or a certificate chain
-importpass Imports a password
-importkeystore Imports one or all entries from another keystore
-printcert Prints the content of a certificate
-printcertreq Prints the content of a certificate request
-printcrl Prints the content of a CRL file
-genkey 在用户主目录中创建一个默认文件”.keystore”,还会产生一个mykey的别名,mykey中包含用户的公钥、私钥和证书
-alias 产生别名 缺省值”mykey”
-keystore 指定密钥库的名称(产生的各类信息将不在.keystore文件中)
-keyalg 指定密钥的算法 (如 RSA DSA(如果不指定默认采用DSA))
-validity 指定创建的证书有效期多少天 缺省值90天
-keysize 指定密钥长度 缺省值1024
-storepass 指定密钥库的密码(获取keystore信息所需的密码)
-keypass 指定别名条目的密码(私钥的密码)
-dname 指定证书拥有者信息 例如: “CN=名字与姓氏,OU=组织单位名称,O=组织名称,L=城市或区域名称,ST=州或省份名称,C=单位的两字母国家代码”
-list 显示密钥库中的证书信息 keytool -list -v -keystore 指定keystore -storepass 密码
-v 显示密钥库中的证书详细信息
-export 将别名指定的证书导出到文件 keytool -export -alias 需要导出的别名 -keystore 指定keystore -file 指定导出的证书位置及证书名称 -storepass 密码
-file 参数指定导出到文件的文件名
-delete 删除密钥库中某条目 keytool -delete -alias 指定需删除的别 -keystore 指定keystore -storepass 密码
-printcert 查看导出的证书信息 keytool -printcert -file 证书名称
-keypasswd 修改密钥库中指定条目口令
-storepasswd 修改keystore口令 keytool -storepasswd -keystore keystore的FULLPATH -storepass 原始密码 -new 新密码
-import 将已签名数字证书导入密钥库 keytool -import -alias 指定导入条目的别名 -keystore 指定keystore -file 需导入的证书

证书管理

证书的发行有专门的CA机构,但是基本上都是要付费的,CA机构又不是NGO,无利不起早,不然为什么人家要做这个呢。一般来说除非是非常正式的项目,一般的项目很多情况下使用自发行的证书即可。

基础知识

项目详细
证书种类 服务器端/客户端证书
认证方式:单向认证 客户端对服务器端的证书进行认证
认证方式:单向认证 客户端对服务器端的证书进行认证,同时服务器端对客户端的证书也进行认证

keystore生成

按照如下信息生成keystore

项目详细
alias名称 kstore
keypass init123
算法 RSA
秘钥长度 2048
有效期限(天) 30
保存路径 /tmp/kstore.keystore
storepass init234

执行命令:

keytool -genkey -alias kstore -keypass init123 -keyalg RSA -keysize 2048 -validity 30 -keystore /tmp/kstore.keystore -storepass init234
  • 1

执行参照

[[email protected] ~]# keytool -genkey -alias kstore -keypass init123 -keyalg RSA -keysize 2048 -validity 30 -keystore /tmp/kstore.keystore -storepass init234
What is your first and last name?
  [Unknown]:  michael
What is the name of your organizational unit?
  [Unknown]:  liumiaocn
What is the name of your organization?
  [Unknown]:  ngo
What is the name of your City or Locality?
  [Unknown]:  dalian
What is the name of your State or Province?
  [Unknown]:  liaoning
What is the two-letter country code for this unit?
  [Unknown]:  CN
Is CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN correct?
  [no]:  yes

[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

生成结果文件确认

[root@liumiaocn ~]# ls -l /tmp/kstore.keystore
-rw-r--r--. 1 root root 2230 Mar 10 17:46 /tmp/kstore.keystore
[root@liumiaocn ~]# file /tmp/kstore.keystore
/tmp/kstore.keystore: Java KeyStore
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

keystore确认

因为生成的/tmp/kstore.keystore非文本类型文件,无法直接确认内容,使用list子命令可以确认keystore的详细信息。
执行命令:

keytool -list  -v -keystore /tmp/kstore.keystore -storepass init234
  • 1

执行参照

[[email protected] ~]# keytool -list  -v -keystore /tmp/kstore.keystore -storepass init234

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: kstore
Creation date: Mar 10, 2017
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]



*******************************************
*******************************************


[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

证书导出

单向认证和双向认证时不可避免的用到证书,使用如下命令则可以生成证书。

keytool -export -alias kstore -keystore /tmp/kstore.keystore -file /tmp/kstore.crt -rfc -storepass init234
  • 1

注意: storepass的密码是/tmp/kstore.keystore生成时创建的密码,此处是作确认用,输入错误会提示:Keystore was tampered with, or password was incorrect

执行参照

[root@liumiaocn ~]# keytool -export -alias kstore -keystore /tmp/kstore.keystore -file /tmp/kstore.crt -rfc -storepass init234
Certificate stored in file </tmp/kstore.crt>
[root@liumiaocn ~]# file /tmp/kstore.crt
/tmp/kstore.crt: PEM certificate
[root@liumiaocn ~]#
[root@liumiaocn ~]#
[root@liumiaocn ~]# cat /tmp/kstore.crt
-----BEGIN CERTIFICATE-----
MIIDaTCCAlGgAwIBAgIEBYcAoTANBgkqhkiG9w0BAQsFADBlMQswCQYDVQQGEwJD
TjERMA8GA1UECBMIbGlhb25pbmcxDzANBgNVBAcTBmRhbGlhbjEMMAoGA1UEChMD
bmdvMRIwEAYDVQQLEwlsaXVtaWFvY24xEDAOBgNVBAMTB21pY2hhZWwwHhcNMTcw
MzEwMjI0NjAwWhcNMTcwNDA5MjI0NjAwWjBlMQswCQYDVQQGEwJDTjERMA8GA1UE
CBMIbGlhb25pbmcxDzANBgNVBAcTBmRhbGlhbjEMMAoGA1UEChMDbmdvMRIwEAYD
VQQLEwlsaXVtaWFvY24xEDAOBgNVBAMTB21pY2hhZWwwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQDL/LZP2FH8jybQD7KvKaqo0TS17xr8isWIkYcjVkvP
T8ZoijWxSLckLI8r83g1Az+obfBuqDlbP/C2qGqf64MGw4mh6/CXnYNgvTOzgLOq
xFgCSQebBhGx0u6gDCpgkoOdTzljDs6YR7Jv3rhIUodsPxc+Bzf8NXbDi1i6rvi5
UW7ijLYFH1jAozTOlLtVWK1cw97BlXXcYhUEBsO682UzYvOeqCuPGcXKX7v1CsND
j+OWf+kfAZ1RF7e3/8IC/FBuAVKHbMD9jWknwAMI7NqGx4Ej6K0LFL6C4XmZMHla
sznT7Eyri0YS5jdWYI+duOEmcbh7MR1FzsZzcw8mmSKPAgMBAAGjITAfMB0GA1Ud
DgQWBBQGkUSTk0bQ7qmznKZsGr3U4+p0dDANBgkqhkiG9w0BAQsFAAOCAQEAdRpS
qAtxZ5JEzN/upzlT6Cp2kK6k7ZQ8ezKYDdUgBtqvMC/TaHwJCMHnvr0aSs24o8SI
v0vmX01RXf5qzMmelMiJCA2EyRFIsKwKE6fWvyRaK1L5NiKbbivqiOHlvcw5pZfK
iC/Cy6W0Y7KY4AtfNreAX7lmxQ611sc6F/Dz5rFv8s5gSaBg6oxc3HnXWYkR9iGu
lYJUhV4tanXsgyhS1/N6PiLbwZ+8ryactI9XgCmao+WQ1M0uhEITZFr8TuRkrG9K
fmvZUD+STRGDh4Us447vezeCw3s/r2rNsvxSpPhRd9PlPJShLe8lVM+bpkdilGwk
yOAewyPCWGtaaWXwIQ==
-----END CERTIFICATE-----
[root@liumiaocn ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

证书确认

生成的证书的格式是PEM certificate,确认其内容则可以通过如下命令:

keytool -printcert -file /tmp/kstore.crt
  • 1

执行参照

[[email protected] ~]# keytool -printcert -file /tmp/kstore.crt
Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]

[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

证书导入

将生成的证书倒入到keystore中,使用如下命令:

keytool -import -alias aliascrt -file /tmp/kstore.crt -keystore /tmp/kstore.keystore -storepass init234 -keypass init123
  • 1

事前确认

文件信息

[[email protected] tmp]# ll kstore.keystore kstore.crt
-rw-r--r--. 1 root root 1263 Mar 10 17:57 kstore.crt
-rw-r--r--. 1 root root 2230 Mar 10 17:46 kstore.keystore
[[email protected] tmp]#
  • 1
  • 2
  • 3
  • 4

keystore详细

[[email protected] tmp]# keytool -list  -v -keystore /tmp/kstore.keystore -storepass init234

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: kstore
Creation date: Mar 10, 2017
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]



*******************************************
*******************************************


[[email protected] tmp]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

导入

[[email protected] tmp]# keytool -import -alias aliascrt -file /tmp/kstore.crt -keystore /tmp/kstore.keystore -storepass init234 -keypass init123
Certificate already exists in keystore under alias <kstore>
Do you still want to add it? [no]:  yes
Certificate was added to keystore
[[email protected] tmp]#
  • 1
  • 2
  • 3
  • 4
  • 5

事后确认

通过文件确认,keystore文件发生了变化

[[email protected] tmp]# ll kstore.keystore kstore.crt
-rw-r--r--. 1 root root 1263 Mar 10 17:57 kstore.crt
-rw-r--r--. 1 root root 3140 Mar 10 18:11 kstore.keystore
[[email protected] tmp]#
  • 1
  • 2
  • 3
  • 4

keystore详细确认后发现,证书已经加入: Alias name: aliascrt

[[email protected] tmp]# keytool -list  -v -keystore /tmp/kstore.keystore -storepass init234

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 2 entries

Alias name: kstore
Creation date: Mar 10, 2017
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]



*******************************************
*******************************************


Alias name: aliascrt
Creation date: Mar 10, 2017
Entry type: trustedCertEntry

Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]



*******************************************
*******************************************
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

[[email protected] tmp]#

证书删除

从keystore中删除证书,使用如下命令即可

keytool -delete -alias aliascrt -keystore /tmp/kstore.keystore -storepass init234
  • 1

执行参照

[root@liumiaocn tmp]# keytool -delete -alias aliascrt -keystore /tmp/kstore.keystore -storepass init234
[root@liumiaocn tmp]#
  • 1
  • 2

事后确认

[[email protected] tmp]# keytool -list  -v -keystore /tmp/kstore.keystore -storepass init234

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: kstore
Creation date: Mar 10, 2017
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Issuer: CN=michael, OU=liumiaocn, O=ngo, L=dalian, ST=liaoning, C=CN
Serial number: 58700a1
Valid from: Fri Mar 10 17:46:00 EST 2017 until: Sun Apr 09 18:46:00 EDT 2017
Certificate fingerprints:
         MD5:  C9:88:B5:3E:62:F1:31:4D:8B:81:9C:45:90:F1:0F:CF
         SHA1: 59:C9:D3:3F:07:80:73:7C:7E:43:94:3B:E5:43:61:FF:14:F1:1A:CC
         SHA256: 32:71:6C:1E:1F:F6:23:01:66:81:92:36:C8:6F:E3:8D:5B:32:C4:F2:10:94:D0:3D:8C:07:5B:91:7A:59:B2:56
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 06 91 44 93 93 46 D0 EE   A9 B3 9C A6 6C 1A BD D4  ..D..F......l...
0010: E3 EA 74 74                                        ..tt
]
]



*******************************************
*******************************************


[[email protected] tmp]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

可以看到,刚刚加入的证书已经被删除。

总结

keytool作为JDK提供的证书管理工具,使用它可以很方便的管理DSA/RSA等流行加密方式的证书,而这些在Web服务器认证以及容器私库管理方面都有着很多应用。

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow


以上是关于Java Keytool工具简介的主要内容,如果未能解决你的问题,请参考以下文章

使用JDK自带的keytool工具生成证书

java keytool证书工具使用小结(转)

用java keytool工具生成keystore文件的问题

如何将java/Android签名工具keytool单独提取出来使用

如何将java/Android签名工具keytool单独提取出来使用

如何将java/Android签名工具keytool单独提取出来使用