0

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.

5
  • is there a reason you don't want to just do that (the encryption) in java? Commented Mar 17, 2016 at 20:33
  • Artjom B. What do you mean by lot more required here? I just approved all the edits done by @User0123456789 Commented Mar 17, 2016 at 21:24
  • @jtahlborn Java just allows AES with 128 bytes. Whereas I need 256 bytes. in order to do that in java i need to update the JRE security directory with couple of policy jars. We dont want to update the JRE, so exploring other options. Commented Mar 17, 2016 at 21:27
  • what about bouncycastle? Commented Mar 18, 2016 at 1:17
  • @GAK I have to agree with you, there wasn't a lot required Commented Mar 18, 2016 at 18:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.