8

GPG is always returning 2 at the result. My code is as follows

$cmd = "/usr/bin/gpg -a --recipient $to -e -o $outfile $infile";

Where outfile is the file where the encrypted data will be written and infile is the unencrypted data.

I don't know why this is happening. Can anyone please tell me what's wrong. Thanks.

4 Answers 4

10

GPG is asking whether you want to continue on with the encryption using an unsigned key. Since no user can input Y it produces an error.

To fix this put the following switches

--yes and --always-trust

4

See this message: http://lists.gnupg.org/pipermail/gnupg-users/2008-January/032410.html

It appears to be a permission problem. gpg is trying to access a directory that it can't have access to, so it fails with a fatal error. (error code 2)

You can fix that by specifying a homedir directive with a directory writable by gpg. Like this:

$cmd = "/usr/bin/gpg -a --recipient $to -e -o $outfile $infile --homedir /path/to/dir";

Information from man gpg:

--homedir directory
Set the name of the home directory to directory

If this option is not used it defaults to "~/.gnupg". It does not make sense to use this in a options file. This also overrides the environment variable $GNUPGHOME.

4
  • so the path do the directory would be something like /something/something/.gnupg? I also set the GNUPGHOME to the path to the .gnupg folder
    – koralarts
    Jun 14, 2011 at 23:12
  • 2
    From the link: "Also, for some reason, setting GNUPGHOME directly does not work.". I think you should use --homedir. Yes, it would be something like it. The directory needs to be writable by your script, though. Jun 15, 2011 at 0:02
  • 1
    I figured it out... i needed --yes and --always-trust because it's asking for input but since it's in php it doesn't get that input hence it breaks.
    – koralarts
    Jun 16, 2011 at 16:49
  • @thiago I tried the yes and always-trust...no luck there. I'm trying to do the homedir method now, but I'm confused as to what I want to set it to...?
    – Shackrock
    Aug 21, 2011 at 13:24
3

You also might want to concider adding key to trusted keys list:

gpg.exe --edit-key KEY_NAME
trust
5 (level of trust)
Y
Save

I've had some problems of --always-trust parameter not functioning properly on XP windows, this helped me solve the problem.

1
  • Doesn't the trust level only apply to verifying message/file signatures? I thought a key had to be signed, either by you or others whose key you've signed, to be able to use it as an encryption recipient. Apr 10, 2014 at 2:42
1

I had the same problem, but for the decoding command

At first and general, you can get the error message by redirecting stderr to stdout.

$cmd = "/usr/bin/gpg -a --recipient $to -e -o $outfile $infile 2>&1";

Then you can modify gpg's parameters to suit your needs. Because I had a files encrypted with a key with pass phrase I had to add several parameters.

I started with

gpg  -o $out -d $path

But it complained, that it can not open tty, then with --no-tty it outputs some other errors and finally the command for decoding files with key with pass phrase is

gpg --batch --passphrase $pass_phrase --no-tty -o $outfile -d $path_to_encoded_file

I hope this helps someone.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.