Xinwei Liu
Xinwei Liu

Reputation: 353

Encrypting file using GnuPG on command line hangs forever

There is one file I want to encrypt with GnuPG by

    gpg2 --homedir=~/.gnupg --always-trust=true --recipient="BlahBlah" --encrypt=/path/to/file --output=/path/to/output_file

However this command seems to hang forever and never return. Interestingly, after I interrupt process, there is indeed /path/to/output_file created , however the bytes written there is much bigger than raw payload (for example my /path/to/file is only of 5 bytes but it turns out there are nearly 200 bytes written to /path/to/output_file).

There must be something wrong, but I really couldn't figure out what is it.

I have in advance imported the key for BlahBlah by gpg --import key.asc. It happens both for GnuPG 1 and GnuPG 2.

Upvotes: 3

Views: 5337

Answers (1)

Jens Erat
Jens Erat

Reputation: 38702

You're applying --encrypt in a wrong way. --encrypt does not expect any parameters, the file(s) to be worked on are passed as very last arguments. Additionally, following the documentation you should pass --output /path/to/output_file instead of --output=/path/to/output_file. Finally, GnuPG distinguishes between options and commands, and options should precede commands.

What you observe is that GnuPG starts writing header information, but then waits for input from STDIN (until interrupted).

The GnuPG command line you're looking for is

gpg2 --homedir=~/.gnupg --always-trust=true --recipient="BlahBlah" --output /path/to/output_file --encrypt /path/to/file

One last hint: the combination of --always-trust=true and resolving a recipient by user ID is a very bad idea, as any other key with the same user ID in the local keyring might be used. Pass the full key's fingerprint instead, which specifically selects a distinct key (using short key IDs is not secure, either).

Upvotes: 4

Related Questions