java秘鑰、證書管理工具-keytool

[TOC]

1 簡介

keytool is a key and certificate management utility. It allows users to administer their own public/private key pairs and associated certificates for use in self-authentication (where the user authenticates himself/herself to other users/services) or data integrity and authentication services, using digital signatures. It also allows users to cache the public keys (in the form of certificates) of their communicating peers.
A certificate is a digitally signed statement from one entity (person, company, etc.), saying that the public key (and some other information) of some other entity has a particular value. (See Certificates.) When data is digitally signed, the signature can be verified to check the data integrity and authenticity. Integrity means that the data has not been modified or tampered with, and authenticity means the data indeed comes from whoever claims to have created and signed it.

keytool also enables users to administer secret keys used in symmetric encryption/decryption (e.g. DES).

keytool stores the keys and certificates in a keystore.

通過以上官方文檔中的一部分內(nèi)容,我們可以了解到:

  • keytool是一個秘鑰和證書的管理工具
  • keytool可以用于管理對稱加密和非對稱加密
  • keytool將秘鑰和證書存儲在keystore中

2 命令格式

2.1 幾點(diǎn)說明

  • 所有的命令和選項都以減號(-)打頭
  • 命令的選項可以以任意的位置出現(xiàn)
  • 選項的值中如果有空白字符,必須用引號引起來
  • -help命令是默認(rèn)的命令,所以keytoolkeytool -help是一樣的

2.2 命令列表

[root@hylexus ~]# keytool 
Key and Certificate Management Tool

Commands:

 -certreq            Generates a certificate request
 -changealias        Changes an entry's alias
 -delete             Deletes an entry
 -exportcert         Exports certificate
 -genkeypair         Generates a key pair
 -genseckey          Generates a secret key
 -gencert            Generates certificate from a certificate request
 -importcert         Imports a certificate or a certificate chain
 -importpass         Imports a password
 -importkeystore     Imports one or all entries from another keystore
 -keypasswd          Changes the key password of an entry
 -list               Lists entries in a keystore
 -printcert          Prints the content of a certificate
 -printcertreq       Prints the content of a certificate request
 -printcrl           Prints the content of a CRL file
 -storepasswd        Changes the store password of a keystore

Use "keytool -command_name -help" for usage of command_name

2.3 一些選項的默認(rèn)值

  • -alias "mykey"
  • -keyalg
    • "DSA" (使用 -genkeypair的時候)
    • "DES" (使用 -genseckey)的時候
  • -keysize
    • 1024(當(dāng)使用 -genkeypair的時候)
    • 56 (當(dāng)使用 -genseckey 并且 -keyalg 為 "DES"的時候)
    • 168 (當(dāng)使用 -genseckey 并且 -keyalg 為 "DESede"的時候)
  • -validity 90
  • -keystore 用戶家目錄下名為.keystore的文件
  • -file
    • 讀的時候為stdin
    • 寫的時候為stdout
  • -protected false

3 使用示例

3.1 生成秘鑰對兒/自簽署證書

創(chuàng)建密鑰對的時候,同時就創(chuàng)建了一個自簽署的證書

keytool -genkeypair -alias tomcat -keyalg rsa -keysize 2048 -validity 365 -keystore /soft/tomcat7-80/conf/keystore

解釋如下:

keytool \
    -genkeypair \ # 生成秘鑰對兒
    -alias tomcat \ # 名稱
    -keyalg rsa \ # 算法名稱
    -keysize 2048 \# 長度
    -validity 365 \# 有效期
    -keystore /soft/tomcat7-80/conf/keystore # 存儲位置

3.2 生成CSR

CSR即Certificate Signing Request,證書頒發(fā)請求。

keytool -certreq -keyalg rsa -alias tomcat -file /soft/tomcat7-80/conf/tomcat.csr -keystore /soft/tomcat7-80/conf/keystore

解釋如下:

keytool -certreq \ # 創(chuàng)建CSR命令
    -keyalg rsa \ # 加密算法
    -alias tomcat \ #在可以store中存儲的別名
    -file /soft/tomcat7-80/conf/tomcat.csr \ # 輸出文件位置
     #指定keystore的位置,默認(rèn)為用戶家目錄下的.keystore文件
    -keystore /soft/tomcat7-80/conf/keystore 

3.3 導(dǎo)出證書

keytool -exportcert -file /soft/tomcat7-80/conf/server.crt -alias tomcat -keystore /soft/tomcat7-80/conf/keystore

解釋如下:

keytool -exportcert \ 導(dǎo)出命令
    # 導(dǎo)出到哪里?
    -file /soft/tomcat7-80/conf/server.crt \
    # 導(dǎo)出哪個?
    -alias tomcat \
    # 從哪個keystore中導(dǎo)出?默認(rèn)為用戶家目錄~/.keystore文件
    -keystore /soft/tomcat7-80/conf/keystore

3.4 導(dǎo)入證書

keytool -importcert -file /soft/tomcat7-80/conf/server.crt -alias myCA -keystore /soft/tomcat7-80/conf/keystore -trustcacerts

解釋如下:

keytool -importcert \ # 導(dǎo)入命令
    # 從哪里導(dǎo)入?
    -file /soft/tomcat7-80/conf/server.crt \
    # 起別名
    -alias myCA \
    # 使用哪個keystore?
    -keystore /soft/tomcat7-80/conf/keystore \
    -trustcacerts

3.5 列出keystore中存儲的內(nèi)容

keytool -list

keytool -list -keystore /soft/tomcat7-80/conf/keystore 

keytool -list -keystore /soft/tomcat7-80/conf/keystore -alias tomcat

解釋如下:

keytool -list \ # list命令
    # 使用哪個keystore?
    -keystore /soft/tomcat7-80/conf/keystore 

3.6 導(dǎo)入現(xiàn)成的私鑰和證書到keystore

看看這個來自stackoverflow的截圖:

stackoverflow

貌似keytool并不提供或說是不直接提供方法導(dǎo)入私鑰。

不過可以繞繞,最終借助于openssl來解決:

http://stackoverflow.com/questions/906402/importing-an-existing-x509-certificate-and-private-key-in-java-keystore-to-use-i

http://stackoverflow.com/questions/17695297/importing-the-private-key-public-certificate-pair-in-the-java-keystore

http://cunning.sharp.fm/2008/06/importing_private_keys_into_a.html

來自stackoverflow的解決方案

# Create PKCS12 keystore from private key and public certificate.
openssl pkcs12 -export -name myservercert -in selfsigned.crt -inkey server.key -out keystore.p12

# Convert PKCS12 keystore into a JKS keystore
keytool -importkeystore -destkeystore mykeystore.jks -srckeystore keystore.p12 -srcstoretype pkcs12 -alias myservercert

以后有時間在補(bǔ)齊其他命令的使用吧……

參考文章

http://stackoverflow.com/questions/906402/importing-an-existing-x509-certificate-and-private-key-in-java-keystore-to-use-i
http://cunning.sharp.fm/2008/06/importing_private_keys_into_a.html
http://stackoverflow.com/questions/17695297/importing-the-private-key-public-certificate-pair-in-the-java-keystore

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,983評論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,772評論 3 422
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,947評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,201評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,960評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,350評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,406評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,549評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,104評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,914評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,089評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,647評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,340評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,753評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,007評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,834評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,106評論 2 375

推薦閱讀更多精彩內(nèi)容