I am trying to execute the openssl command for encryption. I am trying to launch the openssl via java runtime.
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();
}}
When this class runs it just runs the echo 'StringtoEncrypt' | \openssl enc -a -e -aes-256-cbc -nosalt -pass pass:mySecretPass |\openssl enc -a -d -aes-256-cbc -nosalt -pass pass:mySecretPass.
It doesn't execute the openssl. Looks like only the echo part is getting executed but not the openssl. The command works well when I execute it in the console.