欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

支付宝RSA 公私钥 程序生成,openssl rsa 密钥程序生成,想要多少有多少

程序员文章站 2022-07-04 16:43:54
...

 

1.先配置好openssl 环境变量

保证cmd 输入openssl能够找到命令

 

 

2.java 代码

package com.openssl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class StreamDrainer implements Runnable {
	private InputStream ins;

	public StreamDrainer(InputStream ins) {
		this.ins = ins;
	}

	public void run() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					ins));
			String line = null;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

public class TestRunCmd {

	/** rsa 公钥 */
	private static String RSA_PUBLIC_KEY_FILE = "rsa_public_key.pem";

	public static void main(String[] args) {
		String rsaPrivate = "";
		String rsaPublic = "";
		String command = "openssl genrsa -out rsa_private_key.pem 1024";
		cmdExec(command, false);
		command = "openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem";
		cmdExec(command, false);
		command = "openssl pkcs8 -topk8 -nocrypt -inform PEM -in rsa_private_key.pem -outform PEM outform";
		String returnStr = cmdExec(command, true);
		if (returnStr != null && returnStr.length() > 0) {
			//过滤空值、头、尾
			rsaPrivate = returnStr.replace(" ", "")
					.replace("-----BEGINPRIVATEKEY-----", "")
					.replace("-----ENDPRIVATEKEY-----", "");
		}

		rsaPublic = readFile(new File(RSA_PUBLIC_KEY_FILE));
		if (rsaPublic != null && rsaPublic.length() > 0) {
			rsaPublic = rsaPublic.replace(" ", "")
					.replace("-----BEGINPUBLICKEY-----", "")
					.replace("-----ENDPUBLICKEY-----", "");
		}
		System.out.println("私钥: " + rsaPrivate);
		System.out.println("公钥: " + rsaPublic);

	}

	public static String cmdExec(String command, boolean isReturn) {
		String returnStr = "";
		String[] cmd = new String[] { "cmd.exe", "/C", command };
		try {
			Process process = Runtime.getRuntime().exec(cmd);
			if (isReturn) {
				returnStr = result(process.getInputStream());
			} else {
				new Thread(new StreamDrainer(process.getInputStream())).start();
			}
			new Thread(new StreamDrainer(process.getErrorStream())).start();
			process.getOutputStream().close();
			int exitValue = process.waitFor();
			 System.out.println("返回值:" + exitValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}

	public static String result(InputStream ins) {
		StringBuilder sb = new StringBuilder(1000);
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					ins));
			String line = null;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static String readFile(File file) {
		StringBuilder sb = new StringBuilder(1000);
		FileInputStream fis = null; 
		BufferedReader reader = null;
		try {
			fis = new FileInputStream(file);
			reader = new BufferedReader(new InputStreamReader(
					fis));
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return sb.toString();
	}

}

 

 

结果:

私钥: MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL1Vv8RsPrZw46j3Vc02VHOrJtwpH3l9mHnsGGTz8GJrw3R6RZ/k6vtbaZYDEZnQ5EuXunTT2c3rw9/zjE+n/26mGWKfpyS/T2Zg5OyUtR5lwRohWXfKOuXY3FSM97JAV4iS0XElIIoK7S0i6ON0gxFNGXzD06FTz1Nqd8YDJgNvAgMBAAECgYAKzItFjs785O93QbQ5IjIq7sYmpInRCiLaQWcayvcZBgB98kNqXUox0ALztXUZitqanSrbYwH3Rs1DRsbn/DMLaVWb5QGl3e3mL6ZfqcdqaN7aITi+WZ8XKDj4FUDJQz7mH3AjnVkqKjyBfWhWMNqgGlvtMXHo6+j7GyXIgujfwQJBAPXFLtLuUmPbA5NohCgvnmz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