How to verify gnupg signature with Python gnupg module?

1,240 views
Skip to first unread message

michal niklas

unread,
Apr 15, 2011, 5:39:49 AM4/15/11
to python-gnupg
Hello,

This is copy of my question on StackOverflow:
http://stackoverflow.com/questions/5673255/how-to-verify-gnupg-signature-with-python-gnupg-module

I have problem with verification signature with Python `gnupg` module.
With this module I can encrypt and sign file:

gpg.encrypt_file(stream, encrypt_for, sign=sign_by,
passphrase=key_passwd, output=file_out)

Such encrypted file can be decrypted by command line `gpg`, output:

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"

It can be decrypted by Python `gnupg` module too, output file has
decrypted content,
but I cannot verify signature. Code that decrypts and verifies:

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!")

Exception I got:

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!

I use `gnupg-0.2.7` from `python-gnupg-0.2.7.win32.exe` with
ActiveStatus Python 2.6.

I also tried `gpg.verify_file()` but I got the same error. File is
ASCII armoured and looks like:

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.9 (MingW32)

hQIOA0EAndRRjNGtEAf/YxMQaFMnBwT3Per6ypoMYaO1AKQikRgJJMJ90a/EoZ44
...
=G6Ai
-----END PGP MESSAGE-----

How to verify signature like command line `gpg`?

Regards,
Michal Niklas
Message has been deleted

Vinay Sajip

unread,
Apr 15, 2011, 3:11:24 PM4/15/11
to python-gnupg
Hi Michal,

On Apr 15, 10:39 am, michal niklas <michal.nik...@wp.pl> wrote:

> How to verify signature like command line `gpg`?

I posted an eariler response saying that I thought this might be a
bug, but I was a bit hasty (so I have now deleted it). When you
decrypt a signed messages, the object retrurned by the decrypt call
will have information about any valid signature. Here is a sample
script you can run:

https://gist.github.com/922267

which shows two cases - encryption with and without signing. Note that
the script deletes a subdirectory 'keys' relative to the directory it
is run from, so be careful where you run it.

Regards,

Vinay Sajip
Reply all
Reply to author
Forward
0 new messages