我尝试使用名为
的gnupg.py模块加密解密函数
“def test_encryption_and_decryption( self ):”
我可以通过传递从公钥服务器检索到的 key 或指纹来使用此功能吗?
我通过以下方式获取 key :
retk = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup
op=get&search=hex format of key')
pub_key = retk.read()
我还尝试通过加密方法传递指纹:
data = "Hello, world!"
edata = str(self.gpg.encrypt(data, fingerprint))
print edata
不知道该怎么做。是否有人通过提供有值(value)且有效的解决方案/建议来帮助我。
谢谢!
最佳答案
在使用您拥有 keyid 的 key 进行加密之前,您需要将该 key 导入到 key 环中。为此,请使用 import_keys
函数。
编辑:导入 key 后仍无法加密,因为GPG不信任它。当您打开详细消息时,这一点就会变得明显;你会得到
gpg: <keyid>: There is no assurance this key belongs to the named user
要解决此问题(无需为 key 设置信任路径),您可以更改 gpg 的信任模型。以下程序适合我(以我的 key 为例)
import gnupg, urllib
retk = urllib.urlopen("http://keyserver.pramberger.at/pks/"
"lookup?op=get&search=0x6AF053F07D9DC8D2")
pub_key = retk.read()
gpg = gnupg.GPG(gnupghome="/tmp/foo", verbose=True)
print "Import:", gpg.import_keys(pub_key).summary()
print "Encrypt:", gpg.encrypt("Hello, world!", "6AF053F07D9DC8D2",
always_trust=True)
关于python - 相关查询什么是最好的 python 加密方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558287/