python - 如何在 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。
谢谢
解决方案
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
并解密。
推荐阅读
- mysql - 这两个 SQL 查询之间是否存在性能差异?
- androidx - androidx 中的自定义 SwitchPreferenceCompat
- python - 没有名为“tkinter”的模块 - 出现错误
- java - 如果 DTO 类需要新的查询,我们应该怎么做?
- kdb - IPC 上的 FK 字段
- sql - 有没有办法获取 IBM Netezza 表中的所有列名?
- excel - 将日期写入单元格并更改日期格式
- node.js - lambda bundle 打包期间出现许多 webpack 错误
- tensorflow - Tensorflow:将两个模型依次导出为一个
- python - 为什么我的python版本hoare分区快速排序比lomuto分区慢?