4
我有验证签名与Python gnupg
模块的问题。 有了这个模块我可以加密和签名文件:如何用Python gnupg模块验证gnupg签名?
gpg: encrypted with 2048-bit ELG-E key, ID 518CD1AD, created 2011-04-14
"client"
gpg: Signature made 04/14/11 13:36:14 using DSA key ID C7C006DD
gpg: Good signature from "server"
它可以被Python解密gnupg
模块太,输出文件:
gpg.encrypt_file(stream, encrypt_for, sign=sign_by, passphrase=key_passwd, output=file_out)
这样加密的文件可以通过命令行gpg
,输出被解密已解密内容, 但我无法验证签名。该解密并验证码:
def decrypt_file(file_in, file_out, key_passwd):
gpg = gnupg.GPG()
f = open(file_in, "rb")
data = f.read()
f.close()
gpg.decrypt(data, passphrase=key_passwd, output=file_out)
verified = gpg.verify(data)
if not verified:
raise ValueError("Signature could not be verified!")
例外,我得到了:
decrypting file...
Exception in thread Thread-12:
Traceback (most recent call last):
File "c:\Python26\lib\threading.py", line 534, in __bootstrap_inner
self.run()
File "c:\Python26\lib\threading.py", line 486, in run
self.__target(*self.__args, **self.__kwargs)
File "c:\Python26\lib\site-packages\gnupg.py", line 202, in _read_response
result.handle_status(keyword, value)
File "c:\Python26\lib\site-packages\gnupg.py", line 731, in handle_status
raise ValueError("Unknown status message: %r" % key)
ValueError: Unknown status message: u'UNEXPECTED'
Traceback (most recent call last):
File "ht_gnupg.py", line 32, in <module>
test()
File "ht_gnupg.py", line 27, in test
decrypt_file('test_p.enc', 'test_p.txt', 'client')
File "ht_gnupg.py", line 18, in decrypt_file
raise ValueError("Signature could not be verified!")
ValueError: Signature could not be verified!
我用gnupg-0.2.7
从python-gnupg-0.2.7.win32.exe
与ActiveStatus的Python 2.6。
我也试过gpg.verify_file()
但我得到了同样的错误。文件是ASCII装甲,看起来像:
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.9 (MingW32)
hQIOA0EAndRRjNGtEAf/YxMQaFMnBwT3Per6ypoMYaO1AKQikRgJJMJ90a/EoZ44
...
=G6Ai
-----END PGP MESSAGE-----
如何验证签名,如命令行gpg
?
谢谢它工作得很好。我认为在'gnupg'主文档中显示它是值得的,''decrypt()'返回的对象信息很少。 – 2011-04-18 04:48:58
完成后,请参阅http://packages.python.org/python-gnupg/#using-signing-and-encryption-together – 2011-04-19 13:23:26