Search code examples
pythonpgp

Decrypting PGP using gnupg in Python


I am trying to decrypt a PGP file using this module: http://packages.python.org/python-gnupg/

Here is my code snippet:

#!/usr/bin/python

import gnupg

gpg = gnupg.GPG(gnupghome='C:\\Users\\GSquire\\Desktop\\GnuPG',
                                gpgbinary='C:\\Users\\GSquire\\Desktop\\GnuPG\\pub\\gpg.exe',
                                keyring='C:\\Users\\GSquire\\Desktop\\GnuPG\\secring.skr')

with open('.\\tranx08022012.txt.pgp', 'rb') as f:
    status = gpg.decrypt_file(f, passphrase='passphrase', output='out.txt')

I am using the latest version of the module, and Python 2.6.6. I thought I could just use the secure ring file to decrypt it because that is obviously needed by the file. It outputs this when I run the script:

ok: False
status:
stderr:
gpg: expected public key but found secret key - must stop

Isn't it true that the secure key is what decrypts the file? Thanks for the help!


Solution

  • The error you're getting is because you're passing the secret keyring's filename in the keyring parameter. That parameter is only for the public keyring. Unfortunately, there doesn't seem to be an alternative parameter to specify a secret keyring file.

    By default, GnuPG will look for secret keys in secring.gpg in the gnupghome folder you specify, so you can probably rename your secret key file and get it to work.