Skip to content

Encryption of binary file produces wrong output #295

@jploski

Description

@jploski

When encrypting binary data the library passes incorrectly encoded data via stdin to the gpg process. Specifically, the conversion encoded = binary(data) in gnupg/_util.,py within the function _copy_data garbles the output (I think by encoding it to utf-8) before it is sent down to gpg.

Environment:

Python version 3.10.9
python-gnupg version 2.3.1
GPG version 2.2.40
Linux

This can be demonstrated by trying to symmetrically encrypt any ZIP file:

import gnupg

# With this workaround it works:
# gnupg._util.binary = lambda x: x

gpg = gnupg.GPG()

in_path = "file.zip"
out_path = "file.zip.gpg"
passphrase = "test"

with open(in_path, "rb") as f:
    gpg.encrypt(
        f,
        symmetric=True,
        armor=False,
        encrypt=False,
        passphrase=passphrase,
        output=out_path,
    )

If you decrypt the file afterwards (either with this library or just gpg), it does not match the input file. As a workaround the function "binary" from gnupg/_util.py can be replaced with a no-op (see above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions