zoukankan      html  css  js  c++  java
  • java keytool证书工具使用小结【转】

     java keytool证书工具使用小结

     keytool导入导出多条目对比

    在Security编程中,有几种典型的密码交换信息文件格式:


        DER-encoded certificate: .cer, .crt
        PEM-encoded message: .pem
        PKCS#12 Personal Information Exchange: .pfx, .p12
        PKCS#10 Certification Request: .p10
        PKCS#7 cert request response: .p7r
        PKCS#7 binary message: .p7b

      .cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
        .pem跟crt/cer的区别是它以Ascii来表示。
        pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
        p10是证书请求
        p7r是CA对证书请求的回复,只用于导入
        p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。

    keytool简介

    Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中.

    在keystore里,包含两种数据:

    密钥实体(Key entity)-密钥(secret key) 或者是

    私钥和配对公钥(采用非对称加密)可信任的证书实体(trusted certificate entries)-只包含公钥.

    keytool常用参数说明

    JDK中keytool常用参数说明

    (不同版本有差异,详细可参见【附录】中的官方文档链接)

    • -genkey 在用户主目录中创建一个默认文件”.keystore”,还会产生一个mykey的别名,mykey中包含用户的公钥、私钥和证书(在没有指定生成位置的情况下,keystore会存在用户系统默认目录)
    • -alias 产生别名 每个keystore都关联这一个独一无二的alias,这个alias通常不区分大小写
    • -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 g:ssomichael.crt
    • -keypasswd 修改密钥库中指定条目口令 keytool -keypasswd -alias 需修改的别名 -keypass 旧密码 -new 新密码 -storepass keystore密码 -keystore sage
    • -storepasswd 修改keystore口令 keytool -storepasswd -keystore g:ssomichael.keystore(需修改口令的keystore) -storepass pwdold(原始密码) -new pwdnew(新密码)
    • -import 将已签名数字证书导入密钥库 keytool -import -alias 指定导入条目的别名 -keystore 指定keystore -file 需导入的证书

    目录说明:

    1. 生成证书
    2. 查看证书
    3. 证书导出
    4. 客户端导入证书
    5. 附录资料

    一、生成证书

     按win键+R,弹出运行窗口,输入 cmd 回车,打开命令行窗户,输入如下命令:
      keytool -genkey -alias michaelkey -keyalg RSA -keysize 1024 -keypass michaelpwd -validity 365 -keystore g:ssomichael.keystore -storepass michaelpwd2
    截图如下:
     

    二、查看证书

    缺省情况下,-list 命令打印证书的 MD5 指纹。而如果指定了 -v 选项,将以可读格式打印证书,如果指定了 -rfc 选项,将以可打印的编码格式输出证书。

    -v 命令如下:
      keytool -list  -v -keystore g:ssomichael.keystore -storepass michaelpwd2
    回车看到的信息如下:
     
    -rfc 命令如下:
     
    keytool -list -rfc -keystore g:ssomichael.keystore -storepass michaelpwd2
     
    回车看到的信息如下:
     

    三、证书的导出和查看:

    导出证书命令:
      keytool -export -alias michaelkey -keystore g:ssomichael.keystore -file g:ssomichael.crt -storepass michaelpwd2
    回车如下:
     
    查看导出的证书信息:
      keytool -printcert -file g:ssomichael.crt
    回车看到信息如下:

      

    四、客户端导入证书:

    keytool -import -keystore %JAVA_HOME%jrelibsecuritycacerts -fileg:ssossodemo.crt -alias ssodemo

     

    五、官方有关keytool命令的介绍文档:

    • jdk1.4.2 :http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/keytool.html
    • jdk1.6    :http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html
    • jdk1.7    :http://docs.oracle.com/javase/7/docs/technotes/tools/windows/keytool.html

     六.我自己的证书生成命令

    <!--   ok  -->
    keytool -genkey -alias king -keyalg RSA -keysize 1024 -keypass kingkp -storepass kingsp -validity 365 -keystore d:	empking.keystore 
    
    <!--   ok  -->
    keytool -list -v -keystore d:	empking.keystore -storepass kingsp
    
    <!--   ok  -->
    keytool -export -alias king -keystore d:	empking.keystore -file d:	empking.crt -storepass kingsp
    
    <!--   ok  -->
    keytool -printcert -file d:	empking.crt

    七 java代码使用storepass和keypass的地方

    KeyStore keyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(fis, storepass.toCharArray());
    
    PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, keypass.toCharArray());

     如果出现Data must not be longer than 117 bytes。异常,可分段加密和分段解密

    我的其它相关链接

    keytool导入导出多条目对比

    java keytool证书工具使用小结【转】

    本文完全引用自 : http://blog.163.com/pengfang@yeah/blog/static/623930532013416111012845/

    其它参考1: http://www.cnblogs.com/xdp-gacl/p/3750965.html

    其它参考2: http://blog.csdn.net/jy_he/article/details/51838566

  • 相关阅读:
    Erlang性能的八个误区
    Unity预览
    一步步实现cnblogs博客采集工具>实现辅助对话框
    Asp.Net MVC 必备插件MVC Route Visualizer(Visual Studio 2012 版)
    IBM SOA[ESB,BPM,Portal等]基础架构图解
    PowerShell收发TCP消息包
    Sonar安装使用篇
    在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke
    排序算法
    ASP.NET MVC Web API 学习增删改查
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/5913538.html
Copyright © 2011-2022 走看看