Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PY3: Cannot encrypt to a file #93

Closed
doktorstick opened this issue Jan 28, 2015 · 3 comments
Closed

PY3: Cannot encrypt to a file #93

doktorstick opened this issue Jan 28, 2015 · 3 comments
Assignees
Milestone

Comments

@doktorstick
Copy link

import gnupg
import os

print ('gnupg version:', gnupg.__version__)

infile = os.path.expanduser ('~/usr/purernd.bin')
with open (infile, 'rb') as f:
    data = f.read()

kwargs = dict (passphrase='speedtest',
               symmetric=True,
               cipher_algo='AES256',
               armor=False,
               encrypt=False,
               output='/tmp/purernd.enc.gnupg')

gpg = gnupg.GPG (binary='/usr/bin/gpg')
r = gpg.encrypt (data, None, **kwargs)
print (r.ok)
gnupg version: 1.4.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-c5d667873aa8> in <module>()
     17 gpg = gnupg.GPG (binary='/usr/bin/gpg')
     18 t1 = time.perf_counter()
---> 19 r = gpg.encrypt (data, None, **kwargs)
     20 t2 = time.perf_counter()
     21 

/mnt/hgfs/work/branches/jayne/virts/jayne/lib/python3.4/site-packages/gnupg/gnupg.py in encrypt(self, data, *recipients, **kwargs)
    954         """
    955         stream = _make_binary_stream(data, self._encoding)
--> 956         result = self._encrypt(stream, recipients, **kwargs)
    957         stream.close()
    958         return result

/mnt/hgfs/work/branches/jayne/virts/jayne/lib/python3.4/site-packages/gnupg/_meta.py in _encrypt(self, data, recipients, default_key, passphrase, armor, encrypt, symmetric, always_trust, output, throw_keyids, hidden_recipients, cipher_algo, digest_algo, compress_algo)
    991             log.info("Writing encrypted output to file: %s" % output_filename)
    992             with open(output_filename, 'w+') as fh:
--> 993                 fh.write(result.data)
    994                 fh.flush()
    995                 log.info("Encrypted output written successfully.")

TypeError: must be str, not bytes
@isislovecruft
Copy link
Owner

This should be fixed in 2.0.1 (because #90 and #92 were merged).

@isislovecruft isislovecruft added this to the 2.0.1 milestone Mar 9, 2015
@isislovecruft isislovecruft self-assigned this Mar 9, 2015
@isislovecruft
Copy link
Owner

Okay… this wasn't fixed with the changes from #92; it got more broken for Python3.

isislovecruft added a commit that referenced this issue Mar 13, 2015
These issues were introduced in f8ccdc5.  Because we no longer convert
everything to an io.BytesIO in _encrypt() with _make_binary_stream(),
all io.StringIO()s which are passed through must take encoded strings
and io.BytesIO()s must take bytes (and there is actually a difference
with Python3).

Additionally, there appears to be an issue where the `outstream` passed
to _copy_data() is sometimes a _io.BufferedWriter and other times an
encodings.utf_8.StreamWriter.  I am not sure yet where this problem was
introduced.  For now, the workaround for dealing with the Python3
bytes/str io.BytesIO/io.StringIO problem also provides a workaround for
this issue.

 * FIXES #88.
 * FIXES #89 for Python3.
 * FIXES #93.
@isislovecruft
Copy link
Owner

@doktorstick Thanks for reporting this issue! I just re-ran your example with Python3.4 after merging the above commit 43164fa into develop and it works just fine now, so it'll be fixed in python-gnupg>=2.0.1.

isislovecruft added a commit that referenced this issue Mar 13, 2015
isislovecruft added a commit that referenced this issue Mar 13, 2015
This provides more accurate testing for issues like #93.
isislovecruft added a commit that referenced this issue Mar 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants