天天看點

java 調用 keytool 生成keystore 和 cer 證書

keytool是一個Java資料證書的管理工具,

keytool将密鑰(key)和證書(certificates)存在一個稱為keystore的檔案中在keystore裡,

包含兩種資料:

密鑰實體(Key entity)——密鑰(secret key)又或者是私鑰和配對公鑰(采用非對稱加密)

可信任的證書實體(trusted certificate entries)——隻包含公鑰

ailas(别名)每個keystore都關聯這一個獨一無二的alias,這個alias通常不區分大小寫

下面給出一個java 調用 keytool 生成keystore 和 cer 證書的例子測試:

[java]  view plain copy

  1. public class ExportCertFormKeystore {  
  2.     public void execCommand(String[] arstringCommand) {  
  3.         for (int i = 0; i < arstringCommand.length; i++) {  
  4.             System.out.print(arstringCommand[i] + " ");  
  5.         }  
  6.         try {  
  7.             Runtime.getRuntime().exec(arstringCommand);  
  8.         } catch (Exception e) {  
  9.             System.out.println(e.getMessage());  
  10.         }  
  11.     }  
  12.     public void execCommand(String arstringCommand) {  
  13.         try {  
  14.             Runtime.getRuntime().exec(arstringCommand);  
  15.         } catch (Exception e) {  
  16.             System.out.println(e.getMessage());  
  17.         }  
  18.     }  
  19.     public void genkey() {  
  20.         String[] arstringCommand = new String[] {  
  21.         "cmd ", "/k",  
  22.                 "start", // cmd Shell指令  
  23.                 "keytool",  
  24.                 "-genkey", // -genkey表示生成密鑰  
  25.                 "-validity", // -validity指定證書有效期(機關:天),這裡是36000天  
  26.                 "36500",  
  27.                 "-keysize",//     指定密鑰長度  
  28.                 "1024",  
  29.                 "-alias", // -alias指定别名,這裡是ss  
  30.                 "ss",  
  31.                 "-keyalg", // -keyalg 指定密鑰的算法 (如 RSA DSA(如果不指定預設采用DSA))  
  32.                 "RSA",  
  33.                 "-keystore", // -keystore指定存儲位置,這裡是d:/demo.keystore  
  34.                 "d:/demo.keystore",  
  35.                 "-dname",// CN=(名字與姓氏), OU=(組織機關名稱), O=(組織名稱), L=(城市或區域名稱),  
  36.                             // ST=(州或省份名稱), C=(機關的兩字母國家代碼)"  
  37.                 "CN=(SS), OU=(SS), O=(SS), L=(BJ), ST=(BJ), C=(CN)",  
  38.                 "-storepass", // 指定密鑰庫的密碼(擷取keystore資訊所需的密碼)  
  39.                 "123456",   
  40.                 "-keypass",// 指定别名條目的密碼(私鑰的密碼)  
  41.                 "123456",   
  42.                 "-v"// -v 顯示密鑰庫中的證書詳細資訊  
  43.         };  
  44.         execCommand(arstringCommand);  
  45.     }  
  46.     public void export() {  
  47.         String[] arstringCommand = new String[] {  
  48.         "cmd ", "/k",  
  49.                 "start", // cmd Shell指令  
  50.                 "keytool",  
  51.                 "-export", // - export指定為導出操作   
  52.                 "-keystore", // -keystore指定keystore檔案,這裡是d:/demo.keystore  
  53.                 "d:/demo.keystore",  
  54.                 "-alias", // -alias指定别名,這裡是ss  
  55.                 "ss",  
  56.                 "-file",//-file指向導出路徑  
  57.                 "d:/demo.cer",  
  58.                 "-storepass",// 指定密鑰庫的密碼  
  59.                 "123456"  
  60.         };  
  61.         execCommand(arstringCommand);  
  62.     }  
  63. }  

JUnit測試用例:

[java]  view plain copy

  1. import org.junit.Test;  
  2. public class ExportCertFormKeystoreTest {  
  3.     @Test  
  4.     public void genkeyTest() {  
  5.         //生成密鑰測試  
  6.         new ExportCertFormKeystore().genkey();  
  7.     }  
  8.     @Test  
  9.     public void exportTest() {  
  10.         //導出證書檔案測試  
  11.         new ExportCertFormKeystore().export();  
  12.     }  
  13. }  

運作測試用例之後,在D盤的根目錄下面會生成兩個檔案:

demo.keystore

demo.cer