數字證書KeyTool使用(第二篇)


J2SDK提供了keytool命令行工具,能夠根據指定的參數來建立數字證書。生成的證書或證書庫默認保存在命令行當前目錄下。 

1. 建立數字證書 
html

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. 查看證書庫中的全部數字證書 
java

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

注:若是證書庫是非默認storetype,須要明確指定。(JKS--默認,JCEKS, PKCS12 and PKCS11) 算法

JDK 已有的加密算法shell

JDK中不一樣的Keystore類型

3. 查看證書詳細 
apache

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


注:若是證書是非默認storetype,須要明確指定。 

4. 導入證書 
tomcat

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

注: 
-import能夠寫成-importcert 
-trustcacerts和-v 能夠不寫,效果同樣 

5. 導出證書 
oracle

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

注:-export能夠寫成-exportcert 

6. 刪除證書 ide

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

注:若是證書是非默認storetype,須要明確指定。 

7. 生成證書籤名申請 
工具

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

注:將生成的scent.scr文件發給CA機構來申請簽名。 

8. 顯示證書 
ui

keytool -printcert -v -file scent.cer



9. 更改證書別名 

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

 

10. 導入證書庫  

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

注:若是不提供-srcalias, -destalias,則會將源庫的全部證書導入到目標庫中。 

11. 修改證書密碼 

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

注:若是不提供-keypass,系統會提示你輸入新密碼。 

12. 修改證書庫密碼 

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。每一個密鑰庫只能夠是其中一種類型。

1三、導出私鑰的方法(經過Java實現)

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) 命令運行:

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

   (2)  參數說明:

        第一個參數:Key store 文件的存放目錄

        第二個參數:Key store 的訪問密碼

         第三個參數: 導出的私鑰別名。

         第四個參數(可選): 導出的私鑰別名。              

相關文章
相關標籤/搜索