Windows下使用java调用OpenSSL(无需安装OpenSSL)
来源:互联网 发布:东莞网站关键字优化 编辑:程序博客网 时间:2024/05/16 03:56
直接上代码:
代码所需要的配置文件下载地址 http://download.csdn.net/download/learning_lb/9951635
将OpenSSL整个目录复制到C盘根目录即可
import java.io.BufferedReader;import java.io.InputStreamReader;public class Demo {/** * @author LB win系统下 java 调用OpenSSL */public static String CertName = "CA";/** 这几个value为一些证书信息,不懂的自行百度 */public static String Cvalue = "CC";// 长度限制,两个字母public static String Svalue = "Svalue";public static String Lvalue = "Svalue";public static String Ovalue = "Ovalue";public static String OUvalue = "OUvalue";public static String CNvalue = "CNvalue";/** 这几个value为一些证书信息,不懂的自行百度 */public static String pw = "password";// 密码public static int yxq = 3650;// 有效期public static void main(String[] args) {try {Runtime runtime = Runtime.getRuntime();// 打开openSSLString cmd0 = "c:/OpenSSL/openssl.exe ";// 执行命令String cmd1 = "genrsa -out c:/cert/" + CertName + "-key.pem 1024 ";String cmd2 = "req -new -out c:/cert/" + CertName + "-req.csr -key c:/cert/" + CertName+ "-key.pem -passin pass:" + pw + " -subj /C=" + Cvalue + "/ST=" + Svalue + "/L=" + Lvalue + "/O="+ Ovalue + "/OU=" + OUvalue + "/CN=" + CNvalue;String cmd3 = "x509 -req -in c:/cert/" + CertName + "-req.csr -out c:/cert/" + CertName+ "-cert.pem -signkey c:/cert/" + CertName + "-key.pem -days " + yxq;String cmd4 = "pkcs12 -export -clcerts -in c:/cert/" + CertName + "-cert.pem -inkey c:/cert/" + CertName+ "-key.pem -out c:/cert/" + CertName + ".p12 -password pass:" + pw;Process process;int pcode = 0;process = runtime.exec(cmd0 + cmd1);pcode = process.waitFor();// pcode=0 ,无错误,等于1说明有错误。if (pcode == 1) {throw new Exception(getErrorMessage(process));}process = runtime.exec(cmd0 + cmd2);pcode = process.waitFor();if (pcode == 1) {throw new Exception(getErrorMessage(process));}process = runtime.exec(cmd0 + cmd3);pcode = process.waitFor();if (pcode == 1) {throw new Exception(getErrorMessage(process));}process = runtime.exec(cmd0 + cmd4);pcode = process.waitFor();if (pcode == 1) {throw new Exception(getErrorMessage(process));}} catch (Exception e) {e.printStackTrace();}}// 得到控制台输出的错误信息private static String getErrorMessage(Process process) {String errMeaage = null;try {BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));String line = null;StringBuilder sb = new StringBuilder();while ((line = br.readLine()) != null) {sb.append(line + "\n");}errMeaage = sb.toString();} catch (Exception e) {e.printStackTrace();}return errMeaage;}}
阅读全文
1 0
- Windows下使用java调用OpenSSL(无需安装OpenSSL)
- Windows下安装OpenSSL
- Windows 下OpenSSL 安装
- Windows 下OpenSSL 安装
- Windows下安装OpenSSL
- windows下openssl安装
- Windows安装使用Openssl
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- (转)Windows下安装OpenSSL及其使用
- Windows下安装OpenSSL及其使用
- Windows下安装、编译OpenSSL
- 矩阵的之字型遍历-LintCode
- 华硕飞行堡垒触摸板快捷键失效
- mybatis第一个问题
- 如何加固Linux系统
- 《Android源码设计模式》之Builder模式
- Windows下使用java调用OpenSSL(无需安装OpenSSL)
- poj 2533 Longest Ordered Subsequence (最长递增子序列)
- 求最大整数子序列和 以及C++读取数据
- soapui的测试内容体
- CSS样式定义、应用、框模型
- 智能小车二十《摄像头和路由器装上小车》
- 七夕-我有个恋爱想和你谈一下
- Spring 源码阅读 BeanFactory(三) 对象的初始化 singleton 草稿
- Android炫酷的UI界面