Java对证书的生成、导入和删除操作

	/**
	 * 生成自签名证书
	 * @param alias
	 * @param pass
	 * @param dn
	 * @param keysize
	 * @param validity
	 * @param jksfile
	 * @throws Exception
	 */
	public void generateJKS(String alias,String pass,String dn,String keysize,String validity,String jksfile)throws Exception{
		CommandRunner runner = null;
		Vector<String> v = new Vector<String>();
		v.add(KEY_TOOL);
		v.add("-genkey");
		v.add("-alias");
		v.add(alias);
		v.add("-keyalg");
		v.add(KEYALG_RSA);
		v.add("-keysize");
		v.add(keysize);
		v.add("-keystore");
		v.add(jksfile);
		v.add("-dname");
		v.add(dn);
		v.add("-storetype");
		v.add("jks");
		v.add("-storepass");
		v.add(pass);
		v.add("-keypass");
		v.add(pass);
		v.add("-validity");
		v.add(validity);
		try{
			runner = new CommandRunner(v);
			runner.runCommand();
		}catch(Exception e){
			throw new CertificateException("generateJKS", e);
		}
	}
	
	/**
	 * 产生证书请求
	 * @param alias
	 * @param pass
	 * @param dn
	 * @param keysize
	 * @param validity
	 * @param csrfile
	 * @param csrkeystore
	 * @return String
	 * @throws Exception
	 */
	public String generateCSR(String alias,String pass,String dn,String keysize,String validity,String csrfile,String csrkeystore)throws Exception{
		generateJKS(alias, pass, dn, keysize, validity, csrkeystore);
		InputStream in = null;
		CommandRunner runner = null;
		Vector<String> v = new Vector<String>();
		v.add(KEY_TOOL);
		v.add("-certreq");
		v.add("-alias");
		v.add(alias);
		v.add("-keyalg");
		v.add(KEYALG_RSA);
		v.add("-dname");
		v.add(dn);
		v.add("-keystore");
		v.add(csrkeystore);
		v.add("-file");
		v.add(csrfile);
		v.add("-storepass");
		v.add(pass);
		v.add("-keypass");
		v.add(pass);
		v.add("-validity");
		v.add(validity);
		try{
			runner = new CommandRunner(v);
			runner.runCommand();
			in = new FileInputStream(csrfile);
            return Util.loadStreamToString(in, null);
		}catch(Exception e){
			throw new CertificateException("generateCSR", e);
		}finally{
			Util.closeStream(in);
		}
	}
	
	/**
	 * 导入第三方签发者CA证书到临时密钥库中
	 * @param alias
	 * @param pass
	 * @param cafile
	 * @param csrkeystore
	 * @throws Exception
	 */
	public void importSignedCACert(String alias,String pass,String cafile,String csrkeystore)throws Exception{
		CommandRunner runner = null;
		Vector<String> v = new Vector<String>();
		v.add(KEY_TOOL);
		v.add("-import");
		v.add("-alias");
		v.add(alias);
		v.add("-keystore");
		v.add(csrkeystore);
		v.add("-file");
		v.add(cafile);
		v.add("-storepass");
		v.add(pass);
		v.add("-keypass");
		v.add(pass);
		v.add("-noprompt");
		try{
			runner = new CommandRunner(v);
			runner.runCommand();
		}catch(Exception e){
			throw new CertificateException("importSignedCert cafile", e);
		}
	}
	
	/**
	 * 导入第三方签发证书到临时密钥库中
	 * @param alias
	 * @param pass
	 * @param certfile
	 * @param csrkeystore
	 * @throws Exception
	 */
	public void importSignedCert(String alias,String pass,String certfile,String csrkeystore)throws Exception{
		CommandRunner runner = null;
		Vector<String> v = new Vector<String>();
		v.add(KEY_TOOL);
		v.add("-import");
		v.add("-trustcacerts");
		v.add("-alias");
		v.add(alias);
		v.add("-keystore");
		v.add(csrkeystore);
		v.add("-file");
		v.add(certfile);
		v.add("-storepass");
		v.add(pass);
		v.add("-keypass");
		v.add(pass);
		try{
			runner = new CommandRunner(v);
			runner.runCommand();
		}catch(Exception e){
			throw new CertificateException("importSignedCert certfile", e);
		}
	}
	
	/**
	 * 删除签发者证书
	 * @param alias
	 * @param pass
	 * @param csrkeystore
	 * @throws Exception
	 */
	public void deleteSignedCACert(String alias, String pass, String csrkeystore)throws Exception{
		CommandRunner runner = null;
		Vector<String> v = new Vector<String>();
		v.add(KEY_TOOL);
		v.add("-delete");
		v.add("-alias");
		v.add("root");
		v.add("-keystore");
		v.add(csrkeystore);
		v.add("-storepass");
		v.add(pass);
		try{
			runner = new CommandRunner(v);
			runner.runCommand();
		}catch(Exception e){
			throw new CertificateException("delete cafile", e);
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值