python-gnupg always returning empty decryption result

246 Views Asked by At

I am using this piece of code to generate a key, encrypt some content and then decrypt it

with tempfile.TemporaryDirectory() as tmpdir:
    gpg = gnupg.GPG(gnupghome=str(tmpdir), verbose=True)
    gpg.encoding = 'utf-8'
    input_data = gpg.gen_key_input(
        key_type='RSA',
        key_length=2048,
        name_real='Atfinity',
        name_comment='Generated by Atfinity',
        name_email='[email protected]',
        passphrase='test',
    )
    key = gpg.gen_key(input_data)
    content = 'Encrypt this please'
    encrypted = gpg.encrypt_file(
        StringIO(content),
        recipients=key.fingerprint,
        always_trust=True,
        passphrase='test',
        sign=False
    )
    decrypted = gpg.decrypt(str(encrypted), always_trust=True, passphrase='test')
    self.assertEqual(content, str(decrypted))

However, decrypted is always an empty string. What am I doning wrong?

0

There are 0 best solutions below