国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java如何基于command調用openssl生成私鑰證書

瀏覽:3日期:2022-08-27 14:22:04

在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:softOpenSSL-Win64binopenssl.exe genrsa -out ca.key 2048'}; //java生成證書請求 String[] cmdCertificationReq = {'cmd', '/C', 'C:softOpenSSL-Win64binopenssl.exe req -new -key ca.key -out ca.csr -subj /C=CN'}; //java生成證書 String[] cmdCertification = {'cmd', '/C', 'C:softOpenSSL-Win64binopenssl.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。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 含山县| 古蔺县| 全椒县| 汤原县| 鲁甸县| 成都市| 祥云县| 徐汇区| 蓝田县| 沾化县| 灌阳县| 桃江县| 赤城县| 萨迦县| 昌黎县| 衢州市| 泸定县| 泌阳县| 石家庄市| 玉树县| 佛学| 河西区| 梓潼县| 绥德县| 广宗县| 友谊县| 襄樊市| 阿克苏市| 宜宾市| 濮阳县| 静宁县| 石景山区| 肇庆市| 靖江市| 临桂县| 景泰县| 稻城县| 信阳市| 芦山县| 微博| 富锦市|