天天看点

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