Open rupesh1219 opened 6 years ago
I've run into this issue and found the workaround is:
with open('filename', 'rb') as fin:
b_data = fin.read()
str_data = b_data.decode('utf8')
gpg.decrypt(str_data, output='test.csv')
The issue seems to be that gpg.decrypt_file
only works with python2, but encoding the encrypted data first then passing it to decrypt
works in python3.
Hope that helps!
@tomleo It doesn't seem to work. Can you paste your entire code segment including import_keys
step? Thanks!
@zachliu My comment was from over 5 months ago, I can't even remember the project I was using gpg for. Could you share your non-working example?
@tomleo Python 3.6.2
import gnupg
gpg = gnupg.GPG(homedir='path_to_home_dir')
with open('private_key_file.asc', 'rb') as f_obj:
gpg.import_keys(f_obj.read())
with open('encryped_file.csv.pgp', 'rb') as f_obj:
gpg.decrypt(f_obj.read().decode('utf-8'), output='test.csv')
The same error:
gpg: no valid OpenPGP data found
@zachliu Did you get to a solution for this problem?
we end up switching to 2.7 and it works there fine.
Aslo got the same problem and I don't get what's wrong
gpg.decrypt_file(
open(os.path.join(encrypted), "rb"),
always_trust=False,
passphrase=None,
output=os.path.join(output),
)
def decrypt_file(self, filename, always_trust=False, passphrase=None,
output=None):
"""Decrypt the contents of a file-like object ``filename`` .
:param str filename: A file-like object to decrypt.
:param bool always_trust: Instruct GnuPG to ignore trust checks.
:param str passphrase: The passphrase for the secret key used for decryption.
:param str output: A filename to write the decrypted output to.
"""
args = ["--decrypt"]
if output: # write the output to a file with the specified name
if os.path.exists(output):
os.remove(output) # to avoid overwrite confirmation message
args.append('--output %s' % output)
if always_trust:
args.append("--always-trust")
result = self._result_map['crypt'](self)
self._handle_io(args, filename, result, passphrase, binary=True)
log.debug('decrypt result: %r', result.data)
return result
gpg: no valid OpenPGP data found.
[GNUPG:] NODATA 1
[GNUPG:] NODATA 2
gpg: decrypt message failed: Unknown system error
My situation is a follows - I receive different files ecnrypted/signed with gpg. Most of them are successfully verified/decrypted. Sometime, it happens that a single file will fail and throw the error I mentioned above. On the other hand, the same file may come later and will be successfuly decrypted/verified. That is, the same file may throw an error or succeed.
From my research I've found that the error means that either the file is incomplete (incomplete OpenPGP data) or the file is simply was not encrypted/signed.
I am working with tar.gz files which are encrypted/signed and have the extension tar.gz.gpg.
gpg (GnuPG) 2.0.22
libgcrypt 1.5.3
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: ~/.gnupg
Supported algorithms:
Pubkey: RSA, ?, ?, ELG, DSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
Python 2.7.5 (default, Apr 9 2019, 14:30:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
I was able to solve this issue by removing the gnupg package and installing the python-gnupg package. They are extremely similar, so I did not realize that I had installed the wrong one. python-gnupg supports the latest version of GnuPG, so a lot of issues, including this one were remedied. After I changed the package, something as simple the following started working:
stream = open(file_path, 'rb')
result = self.gpg.decrypt_file(stream, always_trust=True, passphrase=passphrase)
@asciiletters Did it work for python3 ?
Add a verbose=True
and try to confirm if gpg: no valid OpenPGP data found.
is actually coming from import_keys
, which is what I experienced.
In my case, I encoded the plaintext private key string and due to how I was handling the secret, base64 encoded it: base64.b64encode(PRIVATE_KEY_STRING.encode())
. Of course, I needed to decoded it upon import base64.b64decode(PRIVATE_KEY_SECRET)
. I was using the python-gnupg
package.
gnupg version 2.3.1 python3.6
import gnupg
gpg = gnupg.GPG(gnupghome='pathto_home_directory') gpg.import_keys('privatekey').read() gpg.import_keys('publickey').read()
with open('filename', 'rb') as f: status = gpg.decrypt_file(f, output='test.csv') print(status.status) print(status.stderr)
status: False status status: None status error: gpg: no valid OpenPGP data found. [GNUPG:] NODATA 1 [GNUPG:] NODATA 2 gpg: decrypt_message failed: eof