JAVA调用 keytool 生成keystore 和 cer 证书

来源:互联网 发布:淘宝网店站外推广 编辑:程序博客网 时间:2024/05/22 13:44
 

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

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

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

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

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

public class ExportCertFormKeystore {    public void execCommand(String[] arstringCommand) {        for (int i = 0; i < arstringCommand.length; i++) {            System.out.print(arstringCommand[i] + " ");        }        try {            Runtime.getRuntime().exec(arstringCommand);        } catch (Exception e) {            System.out.println(e.getMessage());        }    }    public void execCommand(String arstringCommand) {        try {            Runtime.getRuntime().exec(arstringCommand);                    } catch (Exception e) {            System.out.println(e.getMessage());        }    }    /**     * 生成密钥     */    public void genkey() {        String[] arstringCommand = new String[] {        "cmd ", "/k",                "start", // cmd Shell命令                "keytool",                "-genkey", // -genkey表示生成密钥                "-validity", // -validity指定证书有效期(单位:天),这里是36000天                "36500",                "-keysize",//     指定密钥长度                "1024",                "-alias", // -alias指定别名,这里是ss                "ss",                "-keyalg", // -keyalg 指定密钥的算法 (如 RSA DSA(如果不指定默认采用DSA))                "RSA",                "-keystore", // -keystore指定存储位置,这里是d:/demo.keystore                "d:/demo.keystore",                "-dname",// CN=(名字与姓氏), OU=(组织单位名称), O=(组织名称), L=(城市或区域名称),                            // ST=(州或省份名称), C=(单位的两字母国家代码)"                "CN=(SS), OU=(SS), O=(SS), L=(BJ), ST=(BJ), C=(CN)",                "-storepass", // 指定密钥库的密码(获取keystore信息所需的密码)                "123456",                 "-keypass",// 指定别名条目的密码(私钥的密码)                "123456",                 "-v"// -v 显示密钥库中的证书详细信息        };        execCommand(arstringCommand);    }    /**     * 导出证书文件     */    public void export() {        String[] arstringCommand = new String[] {        "cmd ", "/k",                "start", // cmd Shell命令                "keytool",                "-export", // - export指定为导出操作                 "-keystore", // -keystore指定keystore文件,这里是d:/demo.keystore                "d:/demo.keystore",                "-alias", // -alias指定别名,这里是ss                "ss",                "-file",//-file指向导出路径                "d:/demo.cer",                "-storepass",// 指定密钥库的密码                "123456"                        };        execCommand(arstringCommand);        }}
View Code

 JUnit测试用例:

import org.junit.Test;public class ExportCertFormKeystoreTest {    @Test    public void genkeyTest() {        //生成密钥测试        new ExportCertFormKeystore().genkey();    }    @Test    public void exportTest() {        //导出证书文件测试        new ExportCertFormKeystore().export();    }}
View Code

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

demo.keystore

demo.cer

0 0