利用keytool 来产生公钥和私钥对以及相关的证书

来源:互联网 发布:win10图片预览软件 编辑:程序博客网 时间:2024/06/05 04:54
利用keytool 来产生公钥和私钥对以及相关的证书
2010-10-23 11:31

keytool的使用可以参考:

http://edocs.weblogicfans.net/wls/docs92/secmanage/identity_trust.html#wp1167001

http://java.sun.com/j2se/1.3/docs/tooldocs/win32/keytool.html

现在使用keytool工具来生成证书:

1)使用keytool来生成一个密钥库,其中-strorepass后面的密码是密钥库的密码,而-keypass是私钥的密码
C:\Program Files\Java\jre6\bin>keytool -genkey -dname "CN=hellking-Client,OU=tsi
nghua,O=tsinghua,L=BEIJING,S=BEIJING,C=CN" -storepass 666666 -keystore client.ke
ystore -keyalg RSA -keypass 888888

2)显示client.keystore中的信息,需要输入keystore的密码(666666),显示的为私钥

C:\Program Files\Java\jre6\bin>keytool -list -keystore client.keystore
输入keystore密码:

Keystore 类型: JKS
Keystore 提供者: SUN

您的 keystore 包含 1 输入

mykey, 2010-10-23, PrivateKeyEntry,
认证指纹 (MD5): 63:40:B7:7D:E4:1D:52:8B:C5:0B:26:BD:E8:9E:36:9B

3)从密钥库中导出证书为test_axis.cer,证书中包含了公钥

C:\Program Files\Java\jre6\bin>keytool -export -file test_axis.cer -storepass 66
6666 -keystore client.keystore
保存在文件中的认证 <test_axis.cer>

4)显示证书中的信息如下:

C:\Program Files\Java\jre6\bin>keytool -printcert -file test_axis.cer
所有者:CN=hellking-Client, OU=tsinghua, O=tsinghua, L=BEIJING, ST=BEIJING, C=CN
签发人:CN=hellking-Client, OU=tsinghua, O=tsinghua, L=BEIJING, ST=BEIJING, C=CN
序列号:4cc24cc9
有效期: Sat Oct 23 10:47:37 CST 2010 至Fri Jan 21 10:47:37 CST 2011
证书指纹:
         MD5:63:40:B7:7D:E4:1D:52:8B:C5:0B:26:BD:E8:9E:36:9B
         SHA1:B6:EE:A9:45:88:CE:61:23:9C:C0:7C:31:B2:AC:13:74:05:76:AD:A3
         签名算法名称:SHA1withRSA
         版本: 3

5)把私钥保存到server.truststore中

C:\Program Files\Java\jre6\bin>keytool -import -file test_axis.cer -storepass 66
6666 -keystore server.truststore -alias clientkey -noprompt
认证已添加至keystore中

7)显示server.truststore

C:\Program Files\Java\jre6\bin>keytool -list -keystore server.truststore
输入keystore密码:

Keystore 类型: JKS
Keystore 提供者: SUN

您的 keystore 包含 1 输入

clientkey, 2010-10-23, trustedCertEntry,
认证指纹 (MD5): 63:40:B7:7D:E4:1D:52:8B:C5:0B:26:BD:E8:9E:36:9B

keytool的参考示例:http://www.360doc.com/content/10/0121/10/633992_14065815.shtml

keytool密钥和证书管理工具-使用详解:http://www.51testing.com/?uid-16403-action-viewspace-itemid-87545

利用java程序导出,私钥,java代码可以参考:http://lukejin.javaeye.com/blog/586073