我正在尝试执行openssl命令进行加密。我试图通过java运行时启动openssl
。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExecuteShellCommand {
public static void main(String[] args) {
ExecuteShellCommand obj = new ExecuteShellCommand();
String command="echo \'StringtoEncrypt\' | \\openssl enc -a -e -aes-256-cbc -nosalt -pass pass:mySecretPass |\\openssl enc -a -d -aes-256-cbc -nosalt -pass pass:mySecretPass";
String output = obj.executeCommand(command);
System.out.println(output);
}
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}}
当这个类运行时,它只运行echo 'StringtoEncrypt' | \openssl enc -a -e -aes-256-cbc -nosalt -pass pass:mySecretPass |\openssl enc -a -d -aes-256-cbc -nosalt -pass pass:mySecretPass
。
它不会执行openssl
。看起来只有echo部分被执行但不是openssl
。当我在控制台中执行它时,该命令运行良好。