首页 > 解决方案 > 如何在 PGPy 中使用公钥解密 PGP 文件

问题描述

我有一个公开共享给我,我正在加载它,如下所示:

key_path = os.environ.get('ESB_FILES_PUBLIC_KEY')
key, _ = pgpy.PGPKey.from_file(key_path)

我尝试使用此密钥解密文件

message_from_file = pgpy.PGPMessage.from_file(filepath)
raw_message = key.decrypt(message_from_file).message
print(raw_message)

它不起作用,我得到了这个错误pgpy.errors.PGPError: Expected: is_public == False. Got: True

关于如何仅使用公钥在 Python 中解密 PGP 文件的任何想法。供参考:我目前正在使用这个库https://pgpy.readthedocs.io/en/latest/examples.html

谢谢

标签: pythonencryptiongpgpupgp

解决方案


PGP 使用非对称加密。有一个公钥是公开的,只能加密,而私钥只能解密。我发现有帮助的一个很好的解释是https://www.freecodecamp.org/news/how-does-pretty-good-privacy-work-3f5f75ecea97/在“PGP 实际如何工作”下。

在您所指的文档中:https ://pgpy.readthedocs.io/en/latest/examples.html#encrypting-decrypting-messages-with-a-public-key它说“使用密钥加密需要公共密钥,而解密需要私钥。”

如果您生成了密钥,您的密钥环上应该有私钥,您可以将其导出为 a.asc并解密。


推荐阅读