zoukankan      html  css  js  c++  java
  • 数字证书KeyTool使用(第二篇)

    http://my.oschina.net/frankies/blog/344914

     

    J2SDK提供了keytool命令行工具,可以根据指定的参数来创建数字证书。生成的证书或证书库默认保存在命令行当前目录下。 

    1. 创建数字证书 

    1
    keytool -genkey -v -alias scent -dname "CN=John,OU=MNG,O=Corp,L=Hangzhou,ST=Zhejiang,C=CN" -keyalg RSA -keysize 2048 -keypass 123456 -keystore prospectlib -storepass 123456 -storetype JCEKS -validity 900

    注:-genkey可以写成-genkeypair 

    dname的值详解: 
      CN(Common Name名字与姓氏) 
      OU(Organization Unit组织单位名称) 
      O(Organization组织名称) 
      L(Locality城市或区域名称) 
      ST(State州或省份名称) 
      C(Country国家名称) 

    2. 查看证书库中的所有数字证书 

    1
    keytool -list -rfc -keystore prospectlib -storepass 123456 -storetype JCEKS

    注:如果证书库是非默认storetype,需要明确指定。(JKS--默认,JCEKS, PKCS12 and PKCS11) 

    JDK 已有的加密算法

    JDK中不同的Keystore类型

    3. 查看证书详细 

    1
    keytool -list -v -alias scent -keystore prospectlib -storepass 123456 -storetype JCEKS

     

    注:如果证书是非默认storetype,需要明确指定。 

    4. 导入证书 

    1
    keytool -import -v -trustcacerts -alias scent -file scent.cer -keypass 123456 -keystore prospectlib -storepass 123456

    注: 
    -import可以写成-importcert 
    -trustcacerts和-v 可以不写,效果一样 

    5. 导出证书 

    1
    keytool -export -alias scent -file scent.cer -keystore prospectlib -storepass 123456

    注:-export可以写成-exportcert 

    6. 删除证书 

    1
    keytool -delete -alias scent -keystore prospectlib -storepass 123456 -storetype JCEKS

    注:如果证书是非默认storetype,需要明确指定。 

    7. 生成证书签名申请 

    1
    keytool -certreq -alias scent -sigalg "MD5withRSA" -file scent.csr -keypass 123456 -keystore cacerts.jks -storepass 123456

    注:将生成的scent.scr文件发给CA机构来申请签名。 

    8. 显示证书 

    1
    keytool -printcert -v -file scent.cer



    9. 更改证书别名 

    1
    keytool -changealias -v -alias scent -destalias perfume -keystore prospectlib -storepass 123456

     

    10. 导入证书库  

    1
    keytool -importkeystore -v -srckeystore prospectlib -srcstoretype JKS -srcstorepass 123456 -destkeystore intrinsic -deststoretype JKS -deststorepass 123456  -srcalias terrific prospect -destalias terrific prospect

    注:如果不提供-srcalias, -destalias,则会将源库的所有证书导入到目标库中。 

    11. 修改证书密码 

    1
    keytool -keypasswd -alias brilliant -keystore range -storepass 123456 -keypass 123456 -new 654321

    注:如果不提供-keypass,系统会提示你输入新密码。 

    12. 修改证书库密码 

    1
    keytool -storepasswd -v -new 654321 -keystore range -storepass 123456 -storetype JKS

    参数详解: 
    -dname "CN=xx,OU=xx,O=xx,L=xx,ST=xx,C=xx"  dn名为"CN=..." 
    -alias scent                别名为scent的一个证书 
    -keyalg 
         DSA RSA                    DSA或RSA算法(当使用-genkeypair参数) 
         DES DESede AES      DES或DESede或AES算法(当使用-genseckey参数) 
    -keysize 
         512 ~ 1024             密钥的长度为512至1024之间(64的倍数)(当使用-genkeypair和-keyalg DSA参数) 
         > 512                       密钥的长度大于512 (当使用-genkeypair和-keyalg RSA参数) 
         56                            密钥的长度为56 (当使用-genseckey和-keyalg DES 参数) 
         112 168                   密钥长度为112或168(当使用-genseckey和-keyalg DESede 参数) 
         128 192 256             密钥长度为128或192或256 (当使用-genseckey和-keyalg AES 参数) 
    -keypass  123456              这个证书的私钥密码为123456 
    -keystore prospectlib         证书库的名称为prospectlib 
    -storepass 123456             证书库的访问密码为123456 
    -validity  900            证书有效期为900天 
    -file  scent.cer           从scent.cer文件导入证书,或者导出证书到scent.cer文件 
    -v                               显示详细信息 
    -rfc                            以Base64的编码格式打印证书 
    -storetype JCEKS          密钥库的类型为JCEKS。常用的有JKS(默认),JCEKS(推荐),PKCS12,BKS,UBER。每个密钥库只可以是其中一种类型。

    13、导出私钥的方法(通过Java实现)

    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
    import java.io.FileInputStream;import java.security.Key;
    import java.security.KeyStore;//import sun.misc.BASE64Encoder;import org.apache.commons.codec.binary.Base64;
    public class DumpPrivateKey {
         /**
         * Provides the missing functionality of keytool
         * that Apache needs for SSLCertificateKeyFile.
         *
         * @param args  <ul>
         *              <li> [0] Keystore filename.
         *              <li> [1] Keystore password.
         *              <li> [2] alias
         *              <li> [3] Store type (optional)
         *              </ul>
         */
        static public void main(String[] args)
        throws Exception {
            if(args.length < 3) {
              throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, al                                   ias, [store type] <key password: default same than keystore");
            }
            final String keystoreName = args[0];
            final String keystorePassword = args[1];
            final String alias = args[2];
            final String storeType = (args.length>3) ? args[3] : "jks"//Default type is 'jks'
            final String keyPassword = getKeyPassword(args,keystorePassword);
            KeyStore ks = KeyStore.getInstance(storeType );
            ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray());
            Key key = ks.getKey(alias, keyPassword.toCharArray());
            //String b64 = new BASE64Encoder().encode(key.getEncoded());
            String b64 = new String(Base64.encodeBase64(key.getEncoded(),true));
            System.out.println("-----BEGIN PRIVATE KEY-----");
            System.out.println(b64);
            System.out.println("-----END PRIVATE KEY-----");
        }
        private static String getKeyPassword(final String[] args, final String keystorePassword)
        {
           String keyPassword = keystorePassword; // default case
           if(args.length == 4) {
             keyPassword = args[3];
           }
           return keyPassword;
        }}

    说明:

      (1) 命令运行:

    1
    java -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey $HOME/.keystore changeit tomcat

       (2)  参数说明:

            第一个参数:Key store 文件的存放目录

            第二个参数:Key store 的访问密码

             第三个参数: 导出的私钥别名。

             第四个参数(可选): 导出的私钥别名。   

  • 相关阅读:
    IBM AppScan 安全扫描:加密会话(SSL)Cookie 中缺少 Secure 属性 处理办法 分类: 数据安全 2014-06-28 11:35 2804人阅读 评论(0) 收藏
    IBM AppScan 安全扫描:加密会话(SSL)Cookie 中缺少 Secure 属性 处理办法 分类: 数据安全 2014-06-28 11:35 2805人阅读 评论(0) 收藏
    IBM AppScan 安全扫描:支持弱 SSL 密码套件 分类: 数据安全 2014-06-28 11:34 1843人阅读 评论(0) 收藏
    IBM AppScan 安全扫描:支持弱 SSL 密码套件 分类: 数据安全 2014-06-28 11:34 1844人阅读 评论(0) 收藏
    jQuery插件treeview点击节点名称不展开、收缩节点 分类: JavaScript 2014-06-16 20:28 540人阅读 评论(0) 收藏
    jQuery插件treeview点击节点名称不展开、收缩节点 分类: JavaScript 2014-06-16 20:28 539人阅读 评论(0) 收藏
    angular学习笔记(三十)-指令(7)-compile和link(3)
    angular学习笔记(三十)-指令(7)-compile和link(2)
    angular学习笔记(三十)-指令(7)-compile和link(1)
    angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令
  • 原文地址:https://www.cnblogs.com/a757956132/p/4618928.html
Copyright © 2011-2022 走看看