Execute an openssl command from a Java program

Hello,

I need to execute an openssl command from a java program to decrypt a password. i have tried the following:

Process p = Runtime.getRuntime().exec("echo " + encodedPassword + " | openssl aes-256-cbc -salt -a -d -pass pass:" + passphared);

BufferedReader reader;

// Command completed successfully

reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String s = null;

while ( (s = reader.readLine()) != null ) {

System.out.println(s);

}

I got a IO exception with the following message: CreateProcess: echo U2FsdGVkX19fYZhqxtOQXmkcJTwqCX8YrKEm0w7ISjo= | openssl aes-256-cbc -a -d -pass pass:coucou error=2

I do not know what's error=2. I replace the openssl command by the ping command and in that case it works well. Any idea will be more than welcome.

reader.close();

[872 byte] By [marlysaa] at [2007-10-2 17:19:49]
# 1
| is interpreted as piping by a command interpreter shell like cmd.exe or sh or ksh or csh or bash or zsh etc. Plain exec won't cope with it.
BIJ001a at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 2
> I do not know what's error=2.So look it up in the openssl documentation.
CeciNEstPasUnProgrammeura at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 3
> > I do not know what's error=2.> > So look it up in the openssl documentation.I did and did not find it ....
marlysaa at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 4
> | is interpreted as piping by a command interpreter> shell like cmd.exe or sh or ksh or csh or bash or zsh> etc. Plain exec won't cope with it.any solution to be able to pipe the echo into the openssl command?
marlysaa at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 5
I changed the command to:Process p = Runtime.getRuntime().exec("openssl aes-256-cbc -a -d -pass pass:" + passphrase + " <<EOF " + encodedPassword + " EOF");And it works better.>
marlysaa at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 6

> I changed the command to:

>

> Process p = Runtime.getRuntime().exec("openssl

> aes-256-cbc -a -d -pass pass:" + passphrase + " <<EOF

> " + encodedPassword + " EOF");

>

> And it works better.

except that I can not catch the command output ....

marlysaa at 2007-7-13 18:35:46 > top of Java-index,Java Essentials,Java Programming...
# 7
> except that I can not catch the command output ....Why not? You have the Process object.
CeciNEstPasUnProgrammeura at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 8
That's a good question. The bufferedReader does not seem to get back something from the openssl command. If tried to test the p.exitstatus() command and I got, the process has not exited.
marlysaa at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 9
Your reader is only reading from System.out, not System.err. Might be one reason. And might openssl (which I don't know) be waiting for input?
CeciNEstPasUnProgrammeura at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 10
On Unix there is the possibility to open /dev/tty, that is, the controlling terminal even if stdin was redirected. Especially passwords are taken this way. This is way stdin-magic does not forcibly work with telnet, ssh andf the like.
BIJ001a at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 11

> Your reader is only reading from System.out, not

> System.err. Might be one reason. And might openssl

> (which I don't know) be waiting for input?

reading the error stream, I discovered that the problem was <<. cmd.exe does not accept such a command. I have decided so to use an input file for my command. Now I am getting the error: error reading input file from the java program and also from the command line....

marlysaa at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 12
Probably because the input file is not where you think it is.
CeciNEstPasUnProgrammeura at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 13
> Probably because the input file is not where you> think it is.it is in the right place. I have created this file from the Java program, written the encoded password in it and then pass it to the openssl command.
marlysaa at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...
# 14
ok the problem is due to the fact that the openssl command only accept input file that has been created by the openssl -out command. Not very cool for me.Is there any "easy" solution to execute this openssl command from my java program?
marlysaa at 2007-7-13 18:35:47 > top of Java-index,Java Essentials,Java Programming...