Keytool 命令手冊
https://www.namecheap.com/support/knowledgebase/article.aspx/9441/0/tomcat-using-keytool
工具:利用java 安裝目錄 bin下的keytool.exe
生成證書的默認路徑 :
內容概覽:
keytool的幾個常用的命令。
1.創建證書
2.查看證書庫
3.導出證書文件
4.導入證書的信息
5.查看證書信息
6.刪除密鑰庫中的條目
7.修改證書條目的口令
預備知識:
keytool的概念
SUN公司提供了製作證書的工具keytool。
在JDK 1.4以後的版本中都包含了這一工具,它的位置爲\bin\keytool.exe。
主要內容:
1.創建證書
Cmd代碼
1. keytool -genkeypair -alias "test1" -keyalg "RSA" -keystore "test.keystore"
說明:
密鑰庫密碼爲testtest
證書條目密碼爲testtest1,若別名爲test2則密碼爲testtest2
這樣爲個不亂
功能:
創建一個別名爲test1的證書條目,該條目存放在名爲test.keystore的密鑰庫中,若test.keystore密鑰庫不存在則創建。
參數說明:
-genkeypair:生成一對非對稱密鑰;
-alias:指定密鑰對的別名,該別名是公開的;
-keyalg:指定加密算法,本例中的採用通用的RAS加密算法;
-keystore:密鑰庫的路徑及名稱,不指定的話,默認在操作系統的用戶目錄下生成一個”.keystore”的文件
注意:
1.“名字與姓氏”應該是域名,若輸成了姓名,和真正運行的時候域名不符,會出問題;
2.再次輸入密碼,第一次輸入的是密鑰庫(keystore)的密碼,第二次輸入的是證書條目的密碼
3.這裏所說的證書庫和密鑰庫是等同的(個人觀點)
爲了測試需要,這裏再創建兩個別名爲test2和test3的證書條目在test.keystore密鑰庫中,代碼如下:
Cmd代碼
1. keytool -genkeypair -alias "test2" -keyalg "RSA" -keystore "test.keystore"
2. keytool -genkeypair -alias "test3" -keyalg "RSA" -keystore "test.keystore"
2.查看證書庫
Cmd代碼
1. keytool -list -keystore test.keystore
功能:
查看名爲test.keystore的證書庫中的證書條目
3.導出到證書文件
Cmd代碼
1. keytool -export -alias test1 -file test.crt -keystore test.keystore
功能:
將名爲test.keystore的證書庫中別名爲test1的證書條目導出到證書文件test.crt中
4.導入證書的信息
Cmd代碼
1. keytool -import -keystore test_cacerts -file test.crt
功能:
將證書文件test.crt導入到名爲test_cacerts的證書庫中,
5.查看證書信息
Cmd代碼
1. keytool -printcert -file "test.crt"
功能:
查看證書文件test.crt的信息
6.刪除密鑰庫中的條目
刪除前查看密鑰庫test.keysote中的證書條目
Cmd代碼
1. keytool -list -keystore test.keystore
刪除密鑰庫test.keystore中別名爲test2的證書條目
Cmd代碼
1. keytool -delete -keystore test.keystore -alias test2
刪除後查看密鑰庫test.keystore中的證書條目
Cmd代碼
1. keytool -list -keystore test.keystore
7.修改證書條目的口令
交互的方式
Cmd代碼
1. keytool -keypasswd -alias test1 -keystore test.keystore
功能:
將密鑰庫test.keystore中別名爲test1的證書條目的密碼修改爲testtesttest1
非交互方式
Cmd代碼
1. keytool -keypasswd -alias test1 -keypass testtesttest1 -new testtest1 -storepass testtest -keystore test.keystore
Cmd代碼
1. keytool -keypasswd -alias test1 -keypass testtesttest1 -new testtest1 -storepass testtest -keystore test.keystore
功能:
將密鑰庫test.keystore中別名爲test1的證書條目的密碼修改爲testtest1
從其他祕鑰庫導入一個或所有條目(即在java中pkcs12 和jks的相互轉換):
JKS → P12
keytool -importkeystore -srckeystore keystore.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore keystore.p12
P12 → JKS
keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -deststoretype JKS -destkeystore keystore.jks