isislovecruft / python-gnupg

A modified version of python-gnupg, including security patches, extensive documentation, and extra features.
Other
424 stars 172 forks source link

Fix for Python 3 compatibility #222

Closed instantname closed 6 years ago

instantname commented 6 years ago

Due to popular demand in #102, here is a hackish fix for that bug. I think it requires some testing and understanding before being merged.

isislovecruft commented 6 years ago

Hi @instantname! Thanks for the patches!

isislovecruft commented 6 years ago

This didn't appear to change anything for me on python-3.4. Do either of you, @instantname or @DustinHolden, have maybe an example script of what you were doing that failed?

david-drinn commented 5 years ago

@isislovecruft How about these unittests? test_decrypt_message__unarmored() fails before the patch, and passes after.

#!/usr/bin/env python3

import gnupg
import logging
import unittest

TEST_MESSAGE = 'test_message_ABCDEFabcdef0123456789'
TEST_PASSWORD = 'password'

class TestDecryptBug(unittest.TestCase):
    def setUp(self):
        self.message = TEST_MESSAGE
        self.password = TEST_PASSWORD

        self.gpg = gnupg.GPG()

        self.encrypted_message__armored = self.gpg.encrypt(
            self.message,
            None,
            symmetric=True,
            encrypt=False,
            passphrase=self.password,
            armor=True,
        ).data

        self.encrypted_message__unarmored = self.gpg.encrypt(
            self.message,
            None,
            symmetric=True,
            encrypt=False,
            passphrase=self.password,
            armor=False,
        ).data

    def test_decrypt_message__unarmored(self):
        decrypted_message = str(self.gpg.decrypt(
            self.encrypted_message__unarmored,
            passphrase=self.password))
        self.assertEqual(decrypted_message, self.message)

    def test_decrypt_message__armored(self):
        decrypted_message = str(self.gpg.decrypt(
            self.encrypted_message__armored,
            passphrase=self.password))
        self.assertEqual(decrypted_message, self.message)
Alekhya404 commented 4 years ago

Hello,

Am running into the same issue where the decrypt_file works in 2.7 but not in python 3.6.8. We do have latest version of python-gnupg which is 0.4.6

We tried in 2 ways and below are the code snippets and both did not work. 1.) with open(_inputfile, "rb") as f: status = gpg.decrypt_file(f.read().decode('utf-8'), output= out_filename) 2.) with open(_inputfile, "rb") as f: out_filename = os.path.splitext(filename)[0] status = gpg.decrypt_file(f, output= out_filename)

ERROR:

None gpg: no valid OpenPGP data found. [GNUPG:] NODATA 1 [GNUPG:] NODATA 2 gpg: decrypt_message failed: Unknown system error

If anyone knows the answer please help.

instantname commented 4 years ago

@Alekhya404 From the version number you gave, it looks like you are not using the python-gnupg hosted at https://github.com/isislovecruft/python-gnupg/ and that is now named pretty-bad-protocol, but another package from which this project is a fork. Do you get the same error when using pretty-bad-protocol?

Alekhya404 commented 4 years ago

Thanks for your update. I installed pretty-bad-protocol and that worked for me in python3