using python-gnupg v0.3.5 on windows 7 w/Python 2.7 and GPG4Win v2.2.0
test_gnupg.py results in 2 failures:
Test that searching for keys works ... FAIL
Doctest: gnupg.GPG.recv_keys ... FAIL
2 keyrings exist in each of these locations(secring & pubring in each):
under the GPGHome directory (C:\Program Files (x86)\GNU\GnuPG)
under the user profile(C:\Users\\AppData\Roaming\gnupg)
If I create GPG instance and set the keyring file path to the user profile pubring.pgp I get a result from GPG.list_keys(). If I let it use the gpghome directory pubring.pgp I get no results from list_keys() because that keyring is empty.
So given I specify the user profile keyring and I have a key to use this is what happens:
>>>data = '1234 abcd 56678'
>>>fingerprint = u'<fingerprint>'
>>>enc = gpg.encrypt(data,fingerprint)
>>>enc.data
''
encrypt_file() gives the same results, nothing happens, no errors. I'm not particularly savvy in any of this but it seems like if I have data and public key this should be dead simple. I'm having a horrendous time trying to determine what is wrong given I see no log files anywhere and I have no errors when attempting this.
How can I determine what is going wrong here? I've read pretty much everything I can find here on StackOverflow, http://pythonhosted.org/python-gnupg/#getting-started and the google group for python-gnupg.
Also why do I have 2 separate sets of keyrings in the first place?
edit: clarified there are 2 separate sets of pubring and secring
edit 2: answer below was instrumental in leading to the actual problem. the gnupg.GPG() constructor is setting gpg command line options that include 'no-tty', calling gnupg.GPG(options='') resolves the issue and successfully encrypts both data and files.
Okay, I finally got around to looking at this and got basic encryption to work from the command line. Here's an example that will work to encrypt data entered from the command line:
import gnupg
gpg_home = "/path/to/gnupg/home"
gpg = gnupg.GPG(gnupghome=gpg_home)
data = raw_input("Enter data to encrypt: ")
rkey = raw_input("Enter recipient's key ID: ")
encrypted_ascii_data = gpg.encrypt(data, rkey)
print(encrypted_ascii_data)
Change the gpg_home to whichever of those two GnuPG paths you want to use. The first one looks like the default installation location and the second one appears to be specific to your user account. The script will prompt for some text to encrypt and a key ID to encrypt to, then print the ASCII armoured encrypted data to stdout.
EDIT: I'm not certain, but I suspect the reason your code failed was either due to using the whole fingerprint for the recipient key ID, which is unnecessary (I used the 0xLONG format, an example of which is on my profile), or you called the wrong GPG home directory.
EDIT 2: This works to encrypt files and writes the output to a file in the same directory, it will work as is on *nix systems. You will need to change the gpg_home as with the above example:
import gnupg
gpg_home = "~/.gnupg"
gpg = gnupg.GPG(gnupghome=gpg_home)
data = raw_input("Enter full path of file to encrypt: ")
rkeys = raw_input("Enter key IDs separated by spaces: ")
savefile = data+".asc"
afile = open(data, "rb")
encrypted_ascii_data = gpg.encrypt_file(afile, rkeys.split(), always_trust=True, output=savefile)
afile.close()
My work here is done! :)
BTW, both these examples use Python 2.7, for Python 3 you'll need to modify the raw_input() lines to use input() instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With