Problem decrypting PGP in python with pyme without user interaction

倖福魔咒の 提交于 2019-12-12 09:27:21

问题


I am trying to decrypt messages using pyme (a python wrapper from gpgme). It works fine if I type in the password when it prompts but I cannot get the passphrase callback to work. Here is the code

import pyme.core

def Callback( x, y, z ):
    print 'in passphrase callback'
 return 'passphrase'

plain = pyme.core.Data()
cipher = pyme.core.Data(sys.stdin.read())
c = pyme.core.Context()
c.set_armor(1)
c.set_passphrase_cb(Callback)
c.op_decrypt( cipher, plain )
plain.seek(0,0)
print plain.read()

When I run this and don't provide the password interactively the program then tries the Callback printing 'in passphrase callback' but then fails with error:

pyme.errors.GPGMEError: Invocation of gpgme_op_decrypt: Unspecified source: General error (0,1)

First and foremost, why does the passphrase callback not work? And secondly, how can I prevent the program from prompting the user for a password before calling the passphrase callback?

This is running on Ubuntu 10.04


回答1:


apparently, you need to interpret the keyword hook:

   def Callback( x, y, z, hook=None):
   ...

works perfectly well.




回答2:


I'm able to reproduce the error you're reporting by returning None from the passphrase callback. Python functions return None by default if they reach the end of executing a function without reaching a return statement. Is it possible that you are accidentally returning None from your callback, perhaps due to misindentation of your code ending your function early? (The misindentation idea is just a guess based on the illegal indentation in your example.)



来源:https://stackoverflow.com/questions/3660150/problem-decrypting-pgp-in-python-with-pyme-without-user-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!