Decrypting Data

476 views
Skip to first unread message

carlskii

unread,
Mar 29, 2011, 9:57:08 AM3/29/11
to python-gnupg
Hi,

I'm following the examples in the documentation and can successfully
encrypt some data using GPG. e.g.

>>> print encrypted_ascii_data
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.11 (Darwin)

hQEMA/0F7Y6HYtBZAQf/cOp5EEJSyasQohG09gtYu7DpGudoDw2QXGk6o9h0rTXo
YoEM6beONGT2oJMZPco7Ch4TDRahpoHlKJ/N8SZcTOf7dqXE37LODqfSNlRKePZy
TOLasM6Jd/2mcTknN6vNUqPe8aY15B6nE5qJZl/t53z94GxDS7l2Hax60GsVJ1xL
aQ95bjNWnjtATB/+KhNI0ZWC7POXq1R9U4LpzDyo7bHPWrd7LEgS9s7qHwHK4lHy
hyeGMDzCw4FoUTPE5iKcUoIIG+i7uK1ED6WLUIjWK04ynMUqlkvEpIb7WFb/KW6e
iuNzG7tBKZSl/nnM597i6SfdQGpp/rGMKn07uEHbFNI/Abtw6Abh9M8gvmzOdrY2
0CvPOLtbaAjtKo3n9GLs4WqT5QGHbQdbDFyAbZZb247P0M0KV0sYI5igPVrFcSgH
=NKjs
-----END PGP MESSAGE-----

However, when I try to decrypt this I get the following error:

decrypted_data = gpg.decrypt(encrypted_ascii_data,
passphrase="password")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/gnupg.py", line 652, in decrypt
data = _make_binary_stream(message, self.encoding)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/gnupg.py", line 148, in _make_binary_stream
s = s.encode(encoding)
AttributeError: 'Crypt' object has no attribute 'encode'

Any pointers would be appreciated.

Carl

Vinay Sajip

unread,
Mar 31, 2011, 2:12:11 PM3/31/11
to python-gnupg


On Mar 29, 2:57 pm, carlskii <carl.bou...@intellect.co.uk> wrote:
> Hi,
>
> I'm following the examples in the documentation and can successfully
> encrypt some data using GPG. e.g.
>
> >>> print encrypted_ascii_data
>
> -----BEGIN PGP MESSAGE-----
> Version: GnuPG v1.4.11 (Darwin)
>
> hQEMA/0F7Y6HYtBZAQf/cOp5EEJSyasQohG09gtYu7DpGudoDw2QXGk6o9h0rTXo
> YoEM6beONGT2oJMZPco7Ch4TDRahpoHlKJ/N8SZcTOf7dqXE37LODqfSNlRKePZy
> TOLasM6Jd/2mcTknN6vNUqPe8aY15B6nE5qJZl/t53z94GxDS7l2Hax60GsVJ1xL
> aQ95bjNWnjtATB/+KhNI0ZWC7POXq1R9U4LpzDyo7bHPWrd7LEgS9s7qHwHK4lHy
> hyeGMDzCw4FoUTPE5iKcUoIIG+i7uK1ED6WLUIjWK04ynMUqlkvEpIb7WFb/KW6e
> iuNzG7tBKZSl/nnM597i6SfdQGpp/rGMKn07uEHbFNI/Abtw6Abh9M8gvmzOdrY2
> 0CvPOLtbaAjtKo3n9GLs4WqT5QGHbQdbDFyAbZZb247P0M0KV0sYI5igPVrFcSgH
> =NKjs
> -----END PGP MESSAGE-----
>
> However, when I try to decrypt this I get the following error:
>
> decrypted_data = gpg.decrypt(encrypted_ascii_data,
> passphrase="password")
> [snip]
> python2.7/site-packages/gnupg.py", line 148, in _make_binary_stream
>     s = s.encode(encoding)
> AttributeError: 'Crypt' object has no attribute 'encode'
>
> Any pointers would be appreciated.

You need to do a str() on the object returned from an encrypt call to
actually get it as a string, so try

decrypted_data = gpg.decrypt(str(encrypted_ascii_data),
passphrase="password")

Regards,

Vinay Sajip

carlskii

unread,
Mar 31, 2011, 2:52:18 PM3/31/11
to python-gnupg
Thanks Vinay,

That did the trick!

I assume this will also work with files?

Regards,

Carl

Vinay Sajip

unread,
Mar 31, 2011, 5:18:42 PM3/31/11
to python-gnupg


On Mar 31, 7:52 pm, carlskii <carl.bou...@intellect.co.uk> wrote:

> That did the trick!
>
> I assume this will also work with files?

When you encrypt something, the result is a Crypt object, and doing
str() on it gives you the actual data. I don't quite know what you
mean by "with files" (e.g. you could be referring to the output
parameter or the encrypt_file method).

I'll clarify this in the documentation - in the meantime, take a look
at test_gnupg.py and the docstrings in gnupg.py for more examples of
how to use the software.

Regards,

Vinay Sajip
Reply all
Reply to author
Forward
0 new messages