java网络编程_11.Keytool 产生公钥私钥 练习

7.3 用keytool生成公钥/私钥

下面演示用keytool分别为客户机和服务器生成公匙/密匙的方法步骤:

(1)首先以管理员身份进入命令窗口。

(2)在C:盘根目录下创建keystore文件夹,进入keystore文件夹

C:\Windows\System32>cd C:\keystore

(3)按照给出的keystore命令格式,创建客户机密匙库client.keystore。库中包含公匙/密匙对,密匙采用RSA算法,密匙长度为1024位,密匙库访问密码为123456,私匙访问密码为123456,密码有效期为365天,密匙库别名为client。命令执行过程中,需要回答密匙持有者的组织信息和区域信息,最后生成client.keystore 文件保存关于密匙的所有信息。输入命令行时注意所有命令参数要保持在一行,不能回车换行。

C:\keystore>keytool -genkeypair -keystore c:\keystore\client.keystore -storepass 123456 -alias client -keyalg RSA -keysize 1024 -keypass 123456 -validity 365
您的名字与姓氏是什么?
  [Unknown]:  DongXiangzhi
您的组织单位名称是什么?
  [Unknown]:  School of Information and Electrical Engineering
您的组织名称是什么?
  [Unknown]:  LuDong University
您所在的城市或区域名称是什么?
  [Unknown]:  Yantai
您所在的省/市/自治区名称是什么?
  [Unknown]:  Shangdong
该单位的双字母国家/地区代码是什么?
  [Unknown]:  CN
CN=DongXiangzhi, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN 是否正确?
[否]:  y

(4)按照给出的 keytop命令格式,创建服务器密钥库 server.keystore。库中包含公钥/私钥对,密钥采用RSA算法,密钥长度为1024位,密钥库访问密码为123456,私钥访问密码为123456,密钥有效期为365天,密钥库别名为 server。命令执行过程中,需要回答密钥持有者的组织信息和区域信息,最后生成 server. keystore文件保存关于密钥的所有信息。输入命令行时注意所有命令参数要保持在一行,不能回车换行。

C:\keystore>keytool -genkeypair -keystore c:\keystore\server.keystore -storepass 123456 -alias server -keyalg RSA -keysize 1024 -keypass 123456 -validity 365
您的名字与姓氏是什么?
  [Unknown]:  Server
您的组织单位名称是什么?
  [Unknown]:  School of Information and Electrical Engineering
您的组织名称是什么?
  [Unknown]:  LuDong University
您所在的城市或区域名称是什么?
  [Unknown]:  Yantai
您所在的省/市/自治区名称是什么?
  [Unknown]:  Shangdong
该单位的双字母国家/地区代码是什么?
  [Unknown]:  CN
CN=Server, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN是否正 确?
[否]:  y

(5)如图7.14所示,按照给出的 keytop命令格式,分别导出服务器公钥证书 server.cer和客户机公钥证书 dongxiangzhi.cer。

C:\keystore>keytool -exportcert -rfc -alias server -file c:\keystore\server.cer -keystore c:\keystore\server.keystore -storepass 123456
存储在文件 <c:\keystore\server.cer> 中的证书

C:\keystore>keytool -exportcert -rfc -alias client -file c:\keystore\dongxiangzhi.cer -keystore c:\keystore\client.keystore -storepass 123456
存储在文件 <c:\keystore\dongxiangzhi.cer> 中的证书

​ 第一行命令指定从密钥库 server. keystore(别名: server)导出公钥信息到文件 server.cer中,密钥库 server. keystore的访问密码为123456,命令行中的fc参数指定导出的证书符合RFC1421标准可打印编码格式。输入命令行时注意所有命令参数要保持在一行,不能回车换行。同样,第二行命令指定从密钥库 client. keystore(别名 client)导出公钥信息到证书文件 dongxiangzhi.cer。经过这一步的操作,客户机和服务器的公钥分别导出到各自的证书文件。

(6)这一步的目的是将服务器公钥证书分发给客户机。为了便于管,为客户机创建一个公钥库 client. keystore,集中存放客户机信任的所有公钥证书。下面的命令行将服务器公钥证书 server.cer导入到 client. keystore公钥库。

C:\keystore>keytool -importcert -alias server -file c:\keystore\server.cer -keypass 123456 -keystore c:\keystore\tclient.keystore -storepass 123456
所有者: CN=Server, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
发布者: CN=Server, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
序列号: 4635f4ad
有效期开始日期: Mon May 17 15:27:50 CST 2021, 截止日期: Tue May 17 15:27:50 CST 2022
证书指纹:
         MD5: 00:43:E8:96:3E:AD:72:80:87:84:9C:24:AB:07:E9:3B
         SHA1: 47:23:0D:ED:3B:6B:EA:B9:6D:81:6A:A3:0A:A3:CC:94:45:51:01:F7
         SHA256: D0:B1:1A:0C:E0:7D:73:36:05:0B:9E:F8:9D:CF:65:16:C9:10:57:10:64:91:12:2C:E8:1B:7A:03:67:99:B9:1F
         签名算法名称: SHA256withRSA
         版本: 3

扩展:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 39 36 37 E4 F4 57 3B 72   A6 8C 19 3D D5 8B 2C CA  967..W;r...=..,.
0010: 12 80 75 0E                                        ..u.
]
]

是否信任此证书? [否]:  y
证书已添加到密钥库中

(7)将客户机公钥证书分发给服务器。为了便于管理,为服务器创建个公钥库 server. keystore,集中存放服务器信任的所有公钥证书。下面命令行将客户机公钥证书 dongxiangzhi.cer导入到 server. keystore公钥库。

C:\Windows\System32>keytool -importcert -alias client -file c:\keystore\dongxiangzhi.cer -keypass 123456 -keystore c:\keystore\tserver.keystore -storepass 123456
所有者: CN=DongXiangzhi, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
发布者: CN=DongXiangzhi, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
序列号: 6ed32b4a
有效期开始日期: Mon May 17 15:25:15 CST 2021, 截止日期: Tue May 17 15:25:15 CST 2022
证书指纹:
         MD5: 83:7E:4A:46:1D:A0:96:56:AC:82:7F:9D:D6:63:D4:2B
         SHA1: 51:D6:10:DD:F4:64:1F:DA:BA:2A:1F:13:31:E2:5C:A0:A0:A2:07:6E
         SHA256: D2:61:19:BD:D9:F9:E3:69:64:CF:58:FA:FE:E2:88:C9:A2:70:7A:27:95:38:33:47:8C:EB:06:6C:C6:39:04:82
         签名算法名称: SHA256withRSA
         版本: 3

扩展:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: F4 D5 28 DE 47 65 28 31   3C 5F 93 7B FC 11 75 A3  ..(.Ge(1<_....u.
0010: 5C B3 D0 E7                                        \...
]
]

是否信任此证书? [否]:  y
证书已添加到密钥库中

(7)将客户机公钥证书分发给服务器。为了便于管理,为服务器创建个公钥库 server. keystore,集中存放服务器信任的所有公钥证书。下面命令行将客户机公钥证书 dongxiangzhi.cer导入到 server. keystore公钥库。

C:\Windows\System32>keytool -importcert -alias client -file c:\keystore\dongxiangzhi.cer -keypass 123456 -keystore c:\keystore\tserver.keystore -storepass 123456
所有者: CN=DongXiangzhi, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
发布者: CN=DongXiangzhi, OU=School of Information and Electrical Engineering, O=LuDong University, L=Yantai, ST=Shangdong, C=CN
序列号: 6ed32b4a
有效期开始日期: Mon May 17 15:25:15 CST 2021, 截止日期: Tue May 17 15:25:15 CST 2022
证书指纹:
         MD5: 83:7E:4A:46:1D:A0:96:56:AC:82:7F:9D:D6:63:D4:2B
         SHA1: 51:D6:10:DD:F4:64:1F:DA:BA:2A:1F:13:31:E2:5C:A0:A0:A2:07:6E
         SHA256: D2:61:19:BD:D9:F9:E3:69:64:CF:58:FA:FE:E2:88:C9:A2:70:7A:27:95:38:33:47:8C:EB:06:6C:C6:39:04:82
         签名算法名称: SHA256withRSA
         版本: 3

扩展:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: F4 D5 28 DE 47 65 28 31   3C 5F 93 7B FC 11 75 A3  ..(.Ge(1<_....u.
0010: 5C B3 D0 E7                                        \...
]
]

是否信任此证书? [否]:  y
证书已添加到密钥库中

(8)列出了经过上述步骤(1)~(7)生成的相关文件, client. keystore、 server.keystore两个库文件分别包含客户机与服务器的私钥与公钥。 client. keystore包含服务器的公钥, server. keystore包含客户机的公钥。 Dongxiangzhi.cer、 server.cer分别表示客户机和服务器的公钥证书。(生成的所在目录C:\keystore)

参考原文:java网络编程案例教程(微课版,董相志等编著)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LQ小林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值