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)
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 的訪問密碼
第三個引數: 匯出的私鑰別名。
第四個引數(可選): 匯出的私鑰別名。