santos ozoemena
santos ozoemena

Reputation: 27

Gnupg not out-puting encrypted file

Im trying to import a public key, read a csv file, encrypt that file and store that encrypted file in a folder/ directory. The program runs but nothing seems to be generated, created or outputed after I run the script. Any suggestions.

import gnupg
gpg = gnupg.GPG(gnupghome='./gnupghome')
key_data = open('./datafiles/public_key.txt').read()
import_result = gpg.import_keys(key_data)
encrypted_ascii_data = gpg.encrypt('./datafiles/myFile.csv', key_data, output="./datafiles/myFile.csv.gpg") 

Upvotes: 3

Views: 1163

Answers (1)

larsks
larsks

Reputation: 311606

The second parameter is a list of recipients. You're passing it the key_data. If you check the the result of your call to gpg.encrypt(...), you'll see that you have:

>>> encrypted_ascii_data.status
'invalid recipient'

You need to either specify an explicit recipient (by fingerprint, email address, etc), or extract a recipient from your imported key, like this:

>>> encrypted_ascii_data = gpg.encrypt('./datafiles/myFile.csv',
... import_result.fingerprints[0],
... output="./datafiles/myFile.csv.gpg") 

But this will still probably fail with:

>>> encrypted_ascii_data.stderr
'[GNUPG:] KEY_CONSIDERED ... 0\ngpg: 426D9382DFD6A7A9: There is no assurance this key belongs to the named user\n[GNUPG:] INV_RECP 10 ...\n[GNUPG:] FAILURE encrypt 53\ngpg: [stdin]: encryption failed: Unusable public key\n'

It looks like you need to set up a trust for that key. Before attempting to use the key:

gpg.trust_keys(import_result.fingerprints, 'TRUST_ULTIMATE')

Once you've done this:

>>> encrypted_ascii_data = gpg.encrypt('./datafiles/myFile.csv',
... import_result.fingerprints[0],
... output="./datafiles/myFile.csv.gpg")
>>> encrypted_ascii_data.status
'encryption ok'

Upvotes: 3

Related Questions