GnuPG with Java on Windows?
Jean-Christian Imbeault
jc@mega-bucks.co.jp
Mon Mar 24 06:40:02 2003
I'm trying to write a small Java program that will decrypt content
encrypted with GnuPG. I've scoured the archives and have found links and
even some code examples.
The problem I am running in to is that I can't get the code examples to
run under Windows. GnuPg keeps giving an error.
For example the following will work fine on the command line in windows:
gpg --no-secmem-warning --passphrase-fd 0 --batch --decrypt
C:\DOCUME~1\Jc1\LOCALS~1\Temp\AAA9691.tmp
But if run from Java as:
String cmd, path;
path = " C:\DOCUME~1\Jc1\LOCALS~1\Temp\AAA9691.tmp";
cmd = "gpg --no-secmem-warning --passphrase-fd 0 --batch --decrypt";
Runtime.getRuntime().exec(cmd + path);
I get the following error:
gpg error >> code: 2 >> gpg: can't open
`C:\DOCUME~1\Jc1\LOCALS~1\Temp\AAA9691.tmp'
gpg: decrypt_message failed: file open error
Why can't gpg open the file when run from Java but can open when run
from the command line? What did I miss?
Can anyone giver me some pointers on why I get this error and how I can
fix my code to make it work? (code included at bottom)
Or if anyone can point me to a simple implementation of decryption using
gpg in Java (under windows?) that might prove useful also.
Thanks,
Jc
PS The code I am using was shamelessly lifted from
http://www.eurielec.etsit.upm.es/~jamc/work/jfreevote-1.0/ :)
package TAL.Encryption;
import java.io.*;
public class GnuPG {
private static String gpg_comm = "gpg --no-secmem-warning ";
private static String decrypt_param = "--passphrase-fd 0 --batch
--decrypt ";
public GnuPG() {}
public String decrypt(String str, String passphrase) throws Exception {
Process p = null;
File f = null;
try {
f = File.createTempFile("AAA", null);
FileWriter fw = new FileWriter(f);
fw.write(str);
fw.flush();
} catch (Exception e) {
throw new Exception("A- gpg error >> " + e.toString());
}
try {
p = Runtime.getRuntime().exec(gpg_comm + decrypt_param +
f.getAbsoluteFile());
System.out.println("New process with:\n" + gpg_comm +
decrypt_param + f.getAbsoluteFile());
} catch (Exception e) {
throw new Exception("gpg error >> " + e.toString());
}
ProcessStreamReader psr_stdout = new
ProcessStreamReader(p.getInputStream());
ProcessStreamReader psr_stderr = new
ProcessStreamReader(p.getErrorStream());
psr_stdout.start();
psr_stderr.start();
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(p.getOutputStream()));
try {
out.write(passphrase);
out.close();
} catch (Exception e) {
throw new Exception("1- gpg error >> " + e.toString());
}
try {
p.waitFor();
psr_stdout.join();
psr_stderr.join();
} catch (Exception e) {
throw new Exception("2- gpg error >> " + e.toString());
}
if (p.exitValue() != 0) {
throw new Exception("3- gpg error >> code: " + p.exitValue() +
" >> " + psr_stderr.getString());
}
try {
f.delete();
} catch (Exception e) {
throw new Exception("4- gpg error >> " + e.toString());
}
return psr_stdout.getString();
}
public static void main(String[] args) {
GnuPG gpg = new GnuPG();
String encrypted = "";
encrypted =
"-----BEGIN PGP MESSAGE-----\n"+
"Version: GnuPG v1.0.7 (GNU/Linux)\n"+
"\n"+
"hQIOA1O09Ob0n2sjEAgAod5W1wtN3LoRl3wJ1QEkGSu2UT3YzgQXrV8KRxGtaG5R\n"+
"c5JYRfAR1Z3t5SpEFIEMCU8oJxqnZQBSlRJ1UeFvE77Gqg6qFMWvSqJ6FiIOHuw3\n"+
"ZL3lBoom8GFl7z7InKBbAN3ZksJhDTd20IEakwpYB7Y669vTUbI4g91i7K2NT9ee\n"+
"l3OJtgahw9IEvGUN+mwhm6jtvUVq4UAhArf4ujiU9BJa8yH42jzjgYxpErrJdQ5S\n"+
"LGFgI9zipwKXeO4sP3VLnRfWgXMtw4E7gw9fDjq4DLsgOBWyi6GlaQpLZT3q88YA\n"+
"uGcJ4FF6faAGYZgVh1KiDQuZZ5HH0W3tRpvNKTKtTQgAm9uzYVX3iTv8YaN3CB8q\n"+
"Gxwy1tYF7FFiMxkdLDUBlETwZBkfitUgBMeZM/aCjhBv4jiomndJGmYP0TCjxDzv\n"+
"Df2B1y5LYNn1Hj8/IfUEkx1G9fEmDPxWhIK0iJetPOdicIEMXxw+YLMpmtgFwYcp\n"+
"KQx+RkXJbbyXtHVC0UoXk/QlWH/LV887H3lSPLcWXxC7hxBdRWiIql6LKTnMh6HK\n"+
"d3c73hsz4VUvb2gjCOQLEpWlNc6lTPcvJ/PhoTmd9o6+bfbotKL3sfu6ZHai2t3S\n"+
"yxrexotKfrOnFBR6zIBkSpCrEWZHOTjuZY0/lPMs6oyaVGCrDdwVAt4YMyx0ylad\n"+
"p9JSAYlUjojW5T+pxejtMSJVFj0Qgkqm2B+nucffOaLBNGfJ/85pLTt2Zps6K74L\n"+
"eMF0OgTjQ0s7NNxkVkhLZetAF51Rt+OWM/NXr+nWP70azQYJaw==\n"+
"=sAZg\n"+
"-----END PGP MESSAGE-----\n";
String decrypted = "";
try {
decrypted = gpg.decrypt(encrypted, "The passphrase 4 TAL gpg");
} catch (Exception e) {System.out.println("Error:\n" + e);}
System.out.println("Decrypted is:\n" + decrypted);
}
}
class ProcessStreamReader extends Thread {
StringBuffer stream;
InputStreamReader in;
final static int BUFFER_SIZE = 256;
public ProcessStreamReader(InputStream in) {
super();
this.in = new InputStreamReader(in);
this.stream = new StringBuffer();
}
public void run() {
try {
int read;
char[] c = new char[BUFFER_SIZE];
while ((read = in.read(c, 0, BUFFER_SIZE - 1)) > 0) {
stream.append(c, 0, read);
if (read < BUFFER_SIZE - 1) {
break;
}
}
} catch (IOException io) {
;
}
}
public String getString() {
return stream.toString();
}
}