I have an Ubuntu 20.04 server. On the server I am running a python script to decrypt some PGP encrypted files. I have added the PGP keys to a keyring and can decrypt a file using the command line:
gpg --output test.zip --decrypt myfile.pgp
The Python script is using python-gnupg. Please see below my extract.
import gnupg
gpg = gnupg.GPG(gpgbinary='/usr/bin/gpg')
stream = open('myfile.pgp', "rb")
data = gpg.decrypt_file(stream, output=f'output.zip')
print(data.status)
The issue is that when I run this Python code the private key cannot be found. The data.status returns 'no secret key'. However if I run the code shortly after running the command line decrypt - the decryption works - data.status returns 'decryption ok'
This must be because following the command line statement the key is briefly available to the script.
I have tried specifying the directory for the key in the Python script - but this didn't make a difference.
When I run with verbose=True the error seems to be related to the following:
gpg: public key decryption failed: Inappropriate ioctl for device
Any help would be much appreciated