python gnupg未加密fi

2024-04-29 04:05:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用GPG密钥加密文件,但输出始终为空:

>>> import gnupg
>>> home_dir = '~/.gnupg'
>>> pgp = gnupg.GPG(gnupghome=home_dir)
>>> key = open('ff.asc', 'rb')
>>> fp = open('test.txt', 'rb')
>>> res = pgp.import_keys(key.read())
>>> res.results
[{'fingerprint': 'C3...', 'text': 'Not actually changed\n', 'ok': '0'}]
>>> enc = pgp.encrypt_file(fp, 'C3...')
>>> enc.data
b''

我错过了什么?在

另外,是否可以将公共GPG密钥直接从字符串传递给加密函数而不必导入它?在


Tags: keyimporthomedir密钥resopengpg
1条回答
网友
1楼 · 发布于 2024-04-29 04:05:35

问题可能是导入的密钥不受信任。从documentation of gnupg

Note:

Any public key provided for encryption should be trusted, otherwise encryption fails but without any warning. This is because gpg just prints a message to the console, but does not provide a specific error indication that the Python wrapper can use.

最简单的解决方案是使用加密函数的always_trust关键字参数:

always_trust (defaults to False) - Skip key validation and assume that used keys are always fully trusted.

因此,您的加密声明应为

enc = pgp.encrypt_file(fp, 'C3...', always_trust=True)

相关问题 更多 >