gpg --list-keys does not show any results, although keyfiles are in place

Viewed 1509

I did generate a privat/public keypair with gpg on a Debian linux system. Encryption and decryption in the command line worked well. Then I did try to do the decryption with a python program:

import gnupg
gpg = gnupg.GPG(homedir='/usr/home/username/.gnupg')
encrypted_string = '''-----BEGIN PGP MESSAGE-----
...encrypted data...
-----END PGP MESSAGE-----'''
decrypted_data = gpg.decrypt(encrypted_string, passphrase='mypassphrase')
print('ok: ', decrypted_data.ok)
print('status: ', decrypted_data.status)
print('decrypted string: ', decrypted_data.data)

After I did run the python code once (and decryption failed with message secret key missing), decryption in the command line (same command as before) fails as well with the message:

gpg: decryption failed: No secret key

Checking the gpg version

gpg --version

returns

gpg (GnuPG) 2.2.12
libgcrypt 1.8.4
...
Home: /usr/home/username/.gnupg
...

Looking in this home folder, all files are in place:

4 drwx------  2 username username 4096 Jun 28 09:26 openpgp-revocs.d
4 drwx------  2 username username 4096 Jun 28 09:26 private-keys-v1.d
4 -rw-------  1 username username   32 Oct 11 00:25 pubring.gpg
4 -rw-r--r--  1 username username 2480 Jun 28 09:26 pubring.kbx
4 -rw-------  1 username username   32 Jun 28 09:23 pubring.kbx~
0 srwx------  1 username username    0 Jun 29 16:19 S.gpg-agent
0 srwx------  1 username username    0 Jun 29 16:19 S.gpg-agent.browser
0 srwx------  1 username username    0 Jun 29 16:19 S.gpg-agent.extra
0 srwx------  1 username username    0 Jun 29 16:19 S.gpg-agent.ssh
4 -rw-------  1 username username 1280 Oct 11 01:39 trustdb.gpg

and the private key is in private-keys-v1.d folder. Anyhow both gpg --list-keys and gpg --list-secret-keys show no results at all. Using

gpg --list-keys --debug-level 9 

I got following output:

gpg: enabled debug flags: packet mpi crypto filter iobuf memory cache memstat trust ipc clock lookup extprog
gpg: DBG: [not enabled in the source] start
gpg: DBG: [not enabled in the source] keydb_new
gpg: DBG: [not enabled in the source] keydb_search_reset
gpg: DBG: keydb_search: reset  (hd=0x00008b9427390f20)
gpg: DBG: [not enabled in the source] keydb_search enter
gpg: DBG: keydb_search: 1 search descriptions:
gpg: DBG: keydb_search   0: FIRST
gpg: DBG: keydb_search: searching keybox (resource 0 of 1)
gpg: DBG: keydb_search: searched keybox (resource 0 of 1) => EOF
gpg: DBG: [not enabled in the source] keydb_search leave (not found)
gpg: DBG: [not enabled in the source] stop
gpg: keydb: handles=1 locks=0 parse=0 get=0
gpg:        build=0 update=0 insert=0 delete=0
gpg:        reset=1 found=0 not=1 cache=0 not=0
gpg: kid_not_found_cache: count=0 peak=0 flushes=0
gpg: sig_cache: total=0 cached=0 good=0 bad=0
gpg: random usage: poolsize=600 mixed=0 polls=0/0 added=0/0
              outmix=0 getlvl1=0/0 getlvl2=0/0
gpg: rndjent stat: collector=0x0000000000000000 calls=0 bytes=0
gpg: secmem usage: 0/65536 bytes in 0 blocks

The searched keybox (resource 0 of 1) => EOF is the problem here I guess, but I have no idea why.

I tried to import the key files again as binary or text file but go the message

gpg: no valid OpenPGP data found.

I also tried

gpg --update-trustdb

and

gpg --refresh-keys

without any change.

I have no idea what did happen with running the python code and I have no idea why gpg does not know the keys anymore.

  • How can I get my keys back to work so I can decrypt again?
  • How can I get my python code running?
1 Answers

In between I did figured out the root causes:

  1. How can I get my keys back to work so I can decrypt again? Or why did decryption in the command line work well until I did run the python code once and after running the python code, decryption in the command line does not work anymore?

The python command

decrypted_data = gpg.decrypt(encrypted_string, passphrase='mypassphrase')

did ignore the pubring.kbx and generate the file pubring.gpg. With the (more or less empty) pubring.gpg, decryption in the command line uses pubring.gpg instead of pubring.kbx. Due to that, decryption does not work anymore. With simply deleting pubring.gpg, decryption in the command line is working again.

  1. How can I get my python code running?

The answer is not only in the code. What I did before running the code is installing gnupg

pip install gnupg

and later python-gnupg

pip install python-gnupg

But both, gnupg and python-gnupg have the gnupg class that is included with

import gnupg

And at least on my maschine, it did always use gnupg from the first installation gnupg and not from python-gnupg. When this become clear to me, solution was easy. I uninstalled gnupg with

pip uninstall gnupg

As gnupg from python-gnupg uses little different parameters, I had to modify the home parameter:

gpg = gnupg.GPG(gnupghome='/usr/home/username/.gnupg')

And then descyption with the python program runs perfect.

Anyhow one question is left:

How can I get error or warning messages in python for the case, that two (or more) packages are installed with the same class name?

Related