标签:为什么 输入 ... ber == 了解 cer 服务器 www
随着信息安全的重要性的日益提高,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 ~]#
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
项目 | 详细 |
---|---|
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
执行参照
[root@liumiaocn ~]# 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
[root@liumiaocn ~]#
生成结果文件确认
[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 ~]#
因为生成的/tmp/kstore.keystore非文本类型文件,无法直接确认内容,使用list子命令可以确认keystore的详细信息。
执行命令:
keytool -list -v -keystore /tmp/kstore.keystore -storepass init234
执行参照
[root@liumiaocn ~]# 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
]
]
*******************************************
*******************************************
[root@liumiaocn ~]#
单向认证和双向认证时不可避免的用到证书,使用如下命令则可以生成证书。
keytool -export -alias kstore -keystore /tmp/kstore.keystore -file /tmp/kstore.crt -rfc -storepass init234
注意: 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 ~]#
生成的证书的格式是PEM certificate,确认其内容则可以通过如下命令:
keytool -printcert -file /tmp/kstore.crt
执行参照
[root@liumiaocn ~]# 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
]
]
[root@liumiaocn ~]#
将生成的证书倒入到keystore中,使用如下命令:
keytool -import -alias aliascrt -file /tmp/kstore.crt -keystore /tmp/kstore.keystore -storepass init234 -keypass init123
文件信息
[root@liumiaocn 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
[root@liumiaocn tmp]#
keystore详细
[root@liumiaocn 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
]
]
*******************************************
*******************************************
[root@liumiaocn tmp]#
[root@liumiaocn 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
[root@liumiaocn tmp]#
通过文件确认,keystore文件发生了变化
[root@liumiaocn 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
[root@liumiaocn tmp]#
keystore详细确认后发现,证书已经加入: Alias name: aliascrt
[root@liumiaocn 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
]
]
*******************************************
*******************************************
[root@liumiaocn tmp]#
从keystore中删除证书,使用如下命令即可
keytool -delete -alias aliascrt -keystore /tmp/kstore.keystore -storepass init234
执行参照
[root@liumiaocn tmp]# keytool -delete -alias aliascrt -keystore /tmp/kstore.keystore -storepass init234
[root@liumiaocn tmp]#
事后确认
[root@liumiaocn 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
]
]
*******************************************
*******************************************
[root@liumiaocn tmp]#
可以看到,刚刚加入的证书已经被删除。
keytool作为JDK提供的证书管理工具,使用它可以很方便的管理DSA/RSA等流行加密方式的证书,而这些在Web服务器认证以及容器私库管理方面都有着很多应用。
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
标签:为什么 输入 ... ber == 了解 cer 服务器 www
原文地址:https://www.cnblogs.com/firsttry/p/10294083.html