Java如何基于command调用openssl生成私钥证书

作者:护花使者 时间:2023-08-09 11:55:39 

在windows环境下进行的测试,前提条件,windows上需要先安装openssl。

配置环境变量,查看版本:

Java如何基于command调用openssl生成私钥证书


import java.io.*;
import java.util.Properties;

public class OpensslCommand {
 private static void runCMD(String[] CMD) {
   java.lang.Process process = null;
   try {
     process = Runtime.getRuntime().exec(CMD);
     ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
     InputStream errorInStream = new BufferedInputStream(process.getErrorStream());
     InputStream processInStream = new BufferedInputStream(process.getInputStream());
     int num = 0;
     byte[] bs = new byte[1024];
     while ((num = errorInStream.read(bs)) != -1) {
       resultOutStream.write(bs, 0, num);
     }
     while ((num = processInStream.read(bs)) != -1) {
       resultOutStream.write(bs, 0, num);
     }
     String result = new String(resultOutStream.toByteArray(), "gbk");
     System.out.println(result);
     errorInStream.close();
     processInStream.close();
     resultOutStream.close();
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (process != null) process.destroy();
   }
 }
 public static void main(String[] args) throws Exception {
   //需要指定openssl.exe路径
   //java生成私钥
   String[] cmdPrivateKey = {"cmd", "/C", "C:\\soft\\OpenSSL-Win64\\bin\\openssl.exe genrsa -out ca.key 2048"};
   //java生成证书请求
   String[] cmdCertificationReq = {"cmd", "/C", "C:\\soft\\OpenSSL-Win64\\bin\\openssl.exe req -new -key ca.key -out ca.csr -subj /C=CN"};
   //java生成证书
   String[] cmdCertification = {"cmd", "/C", "C:\\soft\\OpenSSL-Win64\\bin\\openssl.exe x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt"};
   runCMD(cmdPrivateKey);
   runCMD(cmdCertificationReq);
   runCMD(cmdCertification);
   Properties props=System.getProperties(); //系统属性
   System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
 }
}

对应目录下可以生成:

Java如何基于command调用openssl生成私钥证书

其中,ca.crt是自签名证书文件。ca.key是私钥。ca.csr只是生成证书的中间请求,是用来指定一些信息,这边只指定国家为CN。

来源:https://www.cnblogs.com/chenmz1995/p/13401450.html

标签:Java,command,openssl,私钥,证书
0
投稿

猜你喜欢

  • C#实现批量Word转换Html的示例代码

    2023-02-07 18:28:27
  • MyBatis中动态sql的实现方法示例

    2022-08-26 06:02:31
  • Java集合类的组织结构和继承、实现关系详解

    2023-03-09 10:48:50
  • 浅谈spring security入门

    2023-02-20 04:48:58
  • java实现简易的学籍管理系统

    2023-07-21 07:47:26
  • Kryo框架使用方法代码示例

    2021-05-30 15:46:05
  • Android自定义processor实现bindView功能的实例

    2023-09-07 17:16:37
  • C#中文随机数实现方法

    2023-12-07 20:56:30
  • Mybatis对mapper的加载流程深入讲解

    2022-06-01 12:33:04
  • WPF实现页面的切换的示例代码

    2023-09-26 21:35:27
  • C#使用foreach遍历哈希表(hashtable)的方法

    2022-10-07 20:00:44
  • Android checkbox的listView具体操作方法

    2023-10-10 06:58:33
  • 详解Android中Service AIDL的使用

    2022-10-02 05:08:16
  • 详解Android WebView加载html片段

    2023-04-23 11:40:12
  • Servlet 过滤器详细介绍

    2021-10-21 13:51:57
  • String concat(String str)使用小结

    2023-02-28 05:52:13
  • C语言实现扫雷小游戏的示例代码

    2022-05-21 13:05:18
  • Win10系统下配置java环境变量的全过程

    2023-10-13 12:09:50
  • Spring实战之注入集合值操作示例

    2023-03-04 04:02:53
  • C++找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)

    2023-06-22 07:32:31
  • asp之家 软件编程 m.aspxhome.com