0

Using java Process, I'm trying to decrypt a file with gpg and save it as a .txt file to my local filesystem. If I execute this script from my terminal it acts correctly. When I execute it through the java process it creates the file on the root of my project but it is always empty. I don't get any errors so it's making it hard to diagnose.

The sh script:

gpg --batch --yes --passphrase-file pass --import private-key.asc
gpg --batch --yes --passphrase-file pass -d fileStillEncrypted.txt.pgp > decrypted_file.txt

The first line above is a little redundant as it only needs to be done once but left in there for now.

I changed the file permissions to 777 for testing just to see if it was a permissions issue:

-rwxrwxrwx   1 user  767211335   6037 Aug 10 11:24 fileStillEncrypted.txt.pgp
-rw-r--r--@  1 user  767211335   2667 Aug 10 11:36 private-key.asc
-rwxrwxrwx   1 user  767211335    141 Aug 21 08:33 decryptFile.bat
-rwxrwxrwx   1 user  767211335    178 Aug 21 09:08 decryptFile.sh

The "pass" file contains the pw and is in the same directory as the sh script.

The Java code:

public void decryptFile() {

    Process p;
    try {
        String[] cmd = {"sh", "/Users/user/path/to/decryptFile.sh"};
        p = Runtime.getRuntime().exec(cmd);
        p.waitFor();

    } catch (IOException e) {
        // TODO catch exception properly
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO catch exception properly
        e.printStackTrace();
    }
}

Any help would be appreciated even if it's just advice on how to debug Process as I haven't used it in the past.

Damisco
  • 85
  • 7
  • 2
    I'd modify the shell script to capture stderr from the gpg command, or else modify the Java code to capture error output; that might give you the needed information. The symptoms suggest the command failed; the output file has already been created by the shell before gpg is entered. – user13784117 Aug 21 '20 at 16:54
  • 1
    to debug you can also print the console output when running the command via java. The easiest way to do that is to use ProcesBuilder instead of Runtime.exec. See https://stackoverflow.com/a/12200361/5474918 – martinspielmann Aug 21 '20 at 16:57

1 Answers1

1

Awesome. Getting the standard error showed me that although Im hitting my shell script with the path it ISN'T looking for the files I reference in that path but in the root path of my Java application. I moved the files there and it picked them up. I did see in the file "inappropriate ioctl" error at first. I found that by updating the following configurations in gnu and restarting that the file would populate correctly.

in ./.gnupg: created gpg-agent.conf and gpg.conf

gpg.conf:

  use-agent
  pinentry-mode loopback

gpg-agent.conf:

  allow-loopback-pinentry

then needed to restart the gnu process with:

  ➜  .gnupg echo RELOADAGENT | gpg-connect-agent
  OK
Damisco
  • 85
  • 7