如何使用 gnupg python 模块解密 tar.gz 文件

How to decrypt a tar.gz file using the gnupg python module

我有一个加密的 tar.gz 存档,需要创建函数来解密和解压缩。当我在终端中使用 gpg 命令时,它工作得很好。

gpg --output path/my_archive.tar.gz --decrypt path/my_archive.tar.gz.gpg

但是当我创建一个解密文件的函数时,我只得到 Unknown system error.

def _unpack(self, my_path: Path) -> Path:
    if not str(my_path).startswith("/tmp/"):
        # import to tmp
        my_path = edge.copy2tmp(my_path)
    if str(my_path).endswith('.gpg'):
        output_path = my_path.parent / my_path.stem
        logger.info(my_path)  # i'm sure it is gpg
        assert my_path.exists()  # i'm sure it exists
        with open(str(my_path), 'rb') as f:
            gpg = gnupg.GPG()
            status = gpg.decrypt_file(
                f, passphrase=MY_SECRET_KEY, output=str(output_path)
            )
        logger.info(status.ok)
        logger.info(status.status)
        logger.info(status.stderr)
        my_path = output_path
    if my_path.is_dir():
        # case when delivery is directory
        return my_path
    elif tarfile.is_tarfile(str(my_path)):
        ....

记录器结果:

my_logger - /tmp/nlgdb-p2iyxhh7/archive_name.tar.gz.gpg
gnupg - Setting homedir to '/home/o.solop/.config/python-gnupg'
gnupg - 
Initialised settings:
binary: /usr/bin/gpg
binary version: 2.2.19\ncfg:pubkey:1;16;17;18;19;22\ncfg:pubkeyname:RSA;ELG;DSA;ECDH;ECDSA;EDDSA\ncfg:cipher:1;2;3;4;7;8;9;10;11;12;13\ncfg:ciphername:IDEA;3DES;CAST5;BLOWFISH;AES;AES192;AES256;TWOFISH;CAMELLIA128;CAMELLIA192;CAMELLIA256\ncfg:digest:2;3;8;9;10;11\ncfg:digestname:SHA1;RIPEMD160;SHA256;SHA384;SHA512;SHA224\ncfg:compress:0;1;2;3\ncfg:compressname:Uncompressed;ZIP;ZLIB;BZIP2\ncfg:curve:cv25519;ed25519;nistp256;nistp384;nistp521;brainpoolP256r1;brainpoolP384r1;brainpoolP512r1;secp256k1\n'
homedir: /home/o.solop/.config/python-gnupg
ignore_homedir_permissions: False
keyring: /home/o.solop/.config/python-gnupg/pubring.gpg
secring: /home/o.solop/.config/python-gnupg/secring.gpg
default_preference_list: SHA512 SHA384 SHA256 AES256 CAMELLIA256 TWOFISH AES192 ZLIB ZIP Uncompressed
keyserver: hkp://wwwkeys.pgp.net
options: None
verbose: False
use_agent: False

gnupg - FAILURE status emitted from gpg process: decrypt 4294967295
my_logger - False
my_logger - decrypt 4294967295
my_logger - gpg: no valid OpenPGP data found.
[GNUPG:] NODATA 1
[GNUPG:] NODATA 2
[GNUPG:] FAILURE decrypt 4294967295
gpg: decrypt_message failed: Unknown system error

你知道我做错了什么吗?也许还有其他模块可以解密我的数据?可能,我只能使用 subprocess,但为什么该模块对我不起作用?

我今天遇到了完全同样的问题。

就我而言,我安装了软件包 gnupg(首先)和 python-gnupghttps://github.com/isislovecruft/python-gnupg/issues/208#issuecomment-786857806中的答案让我兴奋。

删除 gnupg 并重新安装 python-gnupg 后,错误消失了,我终于可以解密我的文件了。