When trying to encrypt files, I get the following error in KGpg editor window:


  The encryption failed with error code 2


On the command line I get:

$ gpg --list-keys
/home/user/.gnupg/pubring.gpg
 ---------------------------------
pub   2048D/5E04B919 2012-02-02 [expires: 2016-02-01]
uid                  Firstname Lastname <email.address@domain.com>
uid                  [jpeg image of size 4005]

$ 
$ gpg --encrypt file-to-encrypt
You did not specify a user ID. (you may use "-r")

Current recipients:

Enter the user ID.  End with an empty line: email.address@domain.com
No such user ID.


This used to work both with editor and on the command line with the same key. The Current recipients: is empty. Why is that?

UPDATE:

When trying to specify the user ID on the command line using the -r option, I get the following:

$ gpg -r email.address@domain.com --encrypt file-to-encrypt
gpg: email.address@domain.com: skipped: unusable public key
gpg: file-to-encrypt: encryption failed: unusable public key


Info: 

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.10
Release:        12.10
Codename:       quantal

$ dpkg -s gnupg
Package: gnupg
Status: install ok installed
Priority: important
Section: utils
Installed-Size: 1936
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 1.4.11-3ubuntu4.4


I figured out what the problem and solution was so I give an answer with details should anyone run into the same problem, it may be helpful.
The problem is somewhat ambiguous, no really informative error message is given.
It turned out that the encryption sub-key was expired. Strangely, gpg --list-keys did NOT show the expired sub-key!! Once the sub-key expiry was extended, it was included in the output of gpg --list-keys.
Also, KGpg does not show in any way that the sub-key is expired nor it allows to extend the expiry of the sub-key (only the main key's expiry can be changed).
The output of gpg --list-keys before the solution (I changed personal details):
$ gpg --list-keys
/home/user/.gnupg/pubring.gpg
---------------------------------
pub   2048D/5E04B919 2012-02-02 [expires: 2016-02-01]
uid                  Firstname Lastname <email.address@domain.com>
uid                  [jpeg image of size 4005]

Nothing more.
However, gpg --edit 5E04B919 showed that the sub-key is expired
$ gpg --edit 16AE78C5
gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

pub  2048D/5E04B919  created: 2012-02-02  expires: 2016-02-01  usage: SCA 
                     trust: ultimate      validity: ultimate
sub  1024g/16AE78C5  created: 2012-02-02  expired: 2014-02-01  usage: E   
[ultimate] (1). Firstname Lastname <email.address@domain.com>
[ultimate] (2)  [jpeg image of size 4005]

gpg>

After some Google search, I found this mailing list archive which pointed me to the right direction to extend the expiry of the sub-key using gpg command line:
http://lists.gnupg.org/pipermail/gnupg-users/2005-June/026063.html
For completeness, here's the relevant segment from the above linked mailing list archive:

gpg --edit-key [key ID]
then
Command> key N  where N is the subkey's index.
e.g. if the subkey whose validity you want to extend is the first listed
subkey, or if it is the only listed subkey, then the  command would be
Command> key 1
this will put a * after the word sub, indicating that this particular
subkey has been selected. then
Command> expire
and follow the prompts.
Hope this works for you, it works for me (Macintosh OS X 10.4.1)
Charly

I followed the instructions and extended the sub-key expiry. After this gpg --list-keys gave a different output:
$ gpg --list-keys
/home/user/.gnupg/pubring.gpg
---------------------------------
pub   2048D/5E04B919 2012-02-02 [expires: 2016-03-12]
uid                  Firstname Lastname <email.address@domain.com>
uid                  [jpeg image of size 4005]
sub   1024g/16AE78C5 2012-02-02 [expires: 2016-03-12]

After this, everything was back to normal, I could encrypt files, etc.
