gpt4 book ai didi

python gnupg不加密文件

转载 作者:行者123 更新时间:2023-11-30 22:20:32 25 4
gpt4 key购买 nike

我尝试使用 GPG key 加密文件,但输出始终为空:

>>> 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''

我在这里缺少什么?

此外,是否可以将公共(public) GPG key 从字符串直接传递给加密函数,而无需导入它?

最佳答案

问题可能是导入的 key 不受信任。来自 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)

关于python gnupg不加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48827934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com