package com.gc.test.cert; import java.io.BufferedInputStream; import java.io.File; import java.io.InputStream; import java.security.SecureRandom; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; public class CertTest { public CertTest(){ //空构造 } //create ketstore and get cert file class initial{ String stringidentity =null; String stringpassword = null; String stringname = null; String stringhome = System.getProperty("user.home"+"/"+"keystore"); String stringkeystore = null; TrustManager[] arturstmanager; // 负责管理做出信任决定时使用的的信任材料,也负责决定是否接受同位体提供的凭据 KeyManager[] arkeymanager; //KeyManager 负责管理用于验证到同位体的本地 SSLSocket 的密钥内容 SSLContext sslcontext; //安全套接字协议的实现 public initial(String identity,String password,String name){ stringidentity = identity; stringpassword = password; stringname = name; } public initial(){ //空构造 } /** * Create Dir */ public void makeDir(){ File filehome = new File(stringhome); if(filehome.exists()==false){ filehome.mkdirs(); } } /** * Create the keystore save directory * @throws Exception */ public void makeStore()throws Exception{ stringkeystore =stringhome; File filekeystore = new File(stringkeystore); if(filekeystore.exists()==false){ System.out.println("creating keystroe.."); byte[] arb = new byte[16]; //伪随机数生成器 (PRNG) 形式 SecureRandom sr = SecureRandom.getInstance("sha1prng"); sr.nextBytes(arb); stringname ="ofcard"; //创建key tool命令 String[] stringCommand = new String[]{ "keytool ", "-genkey ", "-alias ",stringidentity, "-keyalg ","rsa", "-keysize","1024", "-dname","cn="+ stringname, "-keystore ",stringname, "-keypass ",stringpassword, "-storetype ","jks", "-storepass ",stringpassword }; for(int i=0;i<stringCommand.length;i++){ System.out.println(stringCommand[i]); } try { Process process = Runtime.getRuntime().exec(stringCommand); //处理keytool命令 process.waitFor();//进程等待 InputStream is = process.getErrorStream(); BufferedInputStream br = new BufferedInputStream(process.getErrorStream()); int len = br.available(); byte[] b = new byte[len]; br.read(b); String msgerr = new String(b); System.out.println("/n"+msgerr.trim()); if(process.exitValue()!=0){ new Exception().printStackTrace(); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } System.out.println("\n"+"make store successfully"); } } /** * 生成keystore文件 */ public void getCert(){ String[] stringCommand = new String[]{ "keytool ", "-export ", "-keystore ",stringhome, "-alias ",stringidentity, "-file ","c:\\serverx.cer", "-storetype ","jks", "-storepass",stringpassword }; for(int i=0;i<stringCommand.length;i++){ System.out.println(stringCommand[i]); } try { Process process = Runtime.getRuntime().exec(stringCommand); //处理keytool命令 process.waitFor();//进程等待 InputStream is = process.getErrorStream(); BufferedInputStream br = new BufferedInputStream(process.getErrorStream()); int len = br.available(); byte[] b = new byte[len]; br.read(b); String msgerr = new String(b); System.out.println("/n"+msgerr.trim()); if(process.exitValue()!=0){ new Exception().printStackTrace(); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } System.out.println("\n"+"make store successfully"); } } }