java 调用 keytool 生成keystore 和 cer 证书

来源:互联网 发布:手机桌面天气预报软件 编辑:程序博客网 时间:2024/05/23 01:26

keytool是一个Java数据证书的管理工具,

keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,

包含两种数据:
密钥实体(Key entity)——密钥(secret key)又或者是私钥和配对公钥(采用非对称加密)
可信任的证书实体(trusted certificate entries)——只包含公钥


ailas(别名)每个keystore都关联这一个独一无二的alias,这个alias通常不区分大小写


下面给出一个java 调用 keytool 生成keystore 和 cer 证书的例子测试:


[java] view plaincopy
  1. public class ExportCertFormKeystore {  
  2.   
  3.     public void execCommand(String[] arstringCommand) {  
  4.         for (int i = 0; i < arstringCommand.length; i++) {  
  5.             System.out.print(arstringCommand[i] + " ");  
  6.         }  
  7.         try {  
  8.             Runtime.getRuntime().exec(arstringCommand);  
  9.   
  10.         } catch (Exception e) {  
  11.             System.out.println(e.getMessage());  
  12.         }  
  13.     }  
  14.     public void execCommand(String arstringCommand) {  
  15.         try {  
  16.             Runtime.getRuntime().exec(arstringCommand);  
  17.               
  18.         } catch (Exception e) {  
  19.             System.out.println(e.getMessage());  
  20.         }  
  21.     }  
  22.   
  23.     /** 
  24.      * 生成密钥 
  25.      */  
  26.     public void genkey() {  
  27.         String[] arstringCommand = new String[] {  
  28.   
  29.         "cmd ""/k",  
  30.                 "start"// cmd Shell命令  
  31.   
  32.                 "keytool",  
  33.                 "-genkey"// -genkey表示生成密钥  
  34.                 "-validity"// -validity指定证书有效期(单位:天),这里是36000天  
  35.                 "36500",  
  36.                 "-keysize",//     指定密钥长度  
  37.                 "1024",  
  38.                 "-alias"// -alias指定别名,这里是ss  
  39.                 "ss",  
  40.                 "-keyalg"// -keyalg 指定密钥的算法 (如 RSA DSA(如果不指定默认采用DSA))  
  41.                 "RSA",  
  42.                 "-keystore"// -keystore指定存储位置,这里是d:/demo.keystore  
  43.                 "d:/demo.keystore",  
  44.                 "-dname",// CN=(名字与姓氏), OU=(组织单位名称), O=(组织名称), L=(城市或区域名称),  
  45.                             // ST=(州或省份名称), C=(单位的两字母国家代码)"  
  46.                 "CN=(SS), OU=(SS), O=(SS), L=(BJ), ST=(BJ), C=(CN)",  
  47.                 "-storepass"// 指定密钥库的密码(获取keystore信息所需的密码)  
  48.                 "123456",   
  49.                 "-keypass",// 指定别名条目的密码(私钥的密码)  
  50.                 "123456",   
  51.                 "-v"// -v 显示密钥库中的证书详细信息  
  52.         };  
  53.         execCommand(arstringCommand);  
  54.     }  
  55.   
  56.     /** 
  57.      * 导出证书文件 
  58.      */  
  59.     public void export() {  
  60.   
  61.         String[] arstringCommand = new String[] {  
  62.   
  63.         "cmd ""/k",  
  64.                 "start"// cmd Shell命令  
  65.   
  66.                 "keytool",  
  67.                 "-export"// - export指定为导出操作   
  68.                 "-keystore"// -keystore指定keystore文件,这里是d:/demo.keystore  
  69.                 "d:/demo.keystore",  
  70.                 "-alias"// -alias指定别名,这里是ss  
  71.                 "ss",  
  72.                 "-file",//-file指向导出路径  
  73.                 "d:/demo.cer",  
  74.                 "-storepass",// 指定密钥库的密码  
  75.                 "123456"  
  76.                   
  77.         };  
  78.         execCommand(arstringCommand);  
  79.       
  80.     }  
  81. }  

JUnit测试用例:

[java] view plaincopy
  1. import org.junit.Test;  
  2.   
  3. public class ExportCertFormKeystoreTest {  
  4.   
  5.     @Test  
  6.     public void genkeyTest() {  
  7.         //生成密钥测试  
  8.         new ExportCertFormKeystore().genkey();  
  9.     }  
  10.     @Test  
  11.     public void exportTest() {  
  12.         //导出证书文件测试  
  13.         new ExportCertFormKeystore().export();  
  14.     }  
  15.   
  16. }  

运行测试用例之后,在D盘的根目录下面会生成两个文件:

demo.keystore

demo.cer

转载来自:http://blog.csdn.net/saindy5828/article/details/11987587

0 0