Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPG Error Code 2

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.

like image 428
koralarts Avatar asked Jun 14 '11 21:06

koralarts


People also ask

What is a GPG code?

GPG, or GNU Privacy Guard, is a public key cryptography implementation. This allows for the secure transmission of information between parties and can be used to verify that the origin of a message is genuine.

How do I decrypt a file using GPG key?

To decrypt a message the option --decrypt is used. You need the private key to which the message was encrypted. Similar to the encryption process, the document to decrypt is input, and the decrypted result is output. blake% gpg --output doc --decrypt doc.

Can you decrypt GPG with PGP?

You can use the PGP Decrypt File activity to decrypt files that were encrypted as part of a backup operation. To use this activity, you must install the gpg executable.


4 Answers

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

like image 113
koralarts Avatar answered Oct 19 '22 05:10

koralarts


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.

like image 20
Thiago Silveira Avatar answered Oct 19 '22 05:10

Thiago Silveira


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.

like image 41
ertx Avatar answered Oct 19 '22 03:10

ertx


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.

like image 41
snoblucha Avatar answered Oct 19 '22 05:10

snoblucha