Python GnuPG解密在Windows上成功但在Linux上失败

发布于 2025-01-13 20:55:39 字数 1355 浏览 2 评论 0原文

我正在使用 python-gnupg 包使用我生成的密钥来解密文件。由于我无法解释的原因,此代码在 Windows 上运行并成功解密文件:

import gnupg
import os
key_data = '''-----BEGIN PGP PRIVATE KEY BLOCK-----

<key data here>
-----END PGP PRIVATE KEY BLOCK-----
'''
gpg_path = "C:/Program Files (x86)/GnuPG/bin/gpg.exe":
gpg = gnupg.GPG(gpg_path)
gpg.encoding = 'utf-8'
import_result = gpg.import_keys(key_data)
with open(r"C:\Users\test\Downloads\my.csv.gpg", "rb") as f:
  status = gpg.decrypt_file(f, passphrase=None, output=r"C:\Users\test\Downloads\TEST.CSV")
  print("STATUS OK ? " + str(status.ok))
  print("STDERR: " + str(status.stderr))

我看到打印了“STATUS OK ? True”。

但是,此代码无法在同一台 PC 上的 dockerized Linux 环境中解密:

import gnupg
import os
key_data = '''-----BEGIN PGP PRIVATE KEY BLOCK-----

<key data here>
-----END PGP PRIVATE KEY BLOCK-----
'''
gpg_path = "/usr/bin/gpg":
gpg = gnupg.GPG(gpg_path)
gpg.encoding = 'utf-8'
import_result = gpg.import_keys(key_data)
with open(r"/home/test/my.csv.gpg", "rb") as f:
  status = gpg.decrypt_file(f, passphrase=None, output=r"/home/test/TEST.CSV")
  print("STATUS OK ? " + str(status.ok))
  print("STDERR: " + str(status.stderr))

我看到打印了“STATUS OK ? False”,并且没有其他错误。未创建输出文件。两个环境都运行 Python 3.7.9,并且运行 pip show python-gnupg 在两个环境中具有相同的输出。我已确保复制加密文件并尝试使用各种编码保存它。 Linux 环境是通过 WSL 的 Debian。

I'm using the python-gnupg package to decrypt a file using a key I generated. For reasons I can't explain, this code runs on Windows and successfully decrypts the file:

import gnupg
import os
key_data = '''-----BEGIN PGP PRIVATE KEY BLOCK-----

<key data here>
-----END PGP PRIVATE KEY BLOCK-----
'''
gpg_path = "C:/Program Files (x86)/GnuPG/bin/gpg.exe":
gpg = gnupg.GPG(gpg_path)
gpg.encoding = 'utf-8'
import_result = gpg.import_keys(key_data)
with open(r"C:\Users\test\Downloads\my.csv.gpg", "rb") as f:
  status = gpg.decrypt_file(f, passphrase=None, output=r"C:\Users\test\Downloads\TEST.CSV")
  print("STATUS OK ? " + str(status.ok))
  print("STDERR: " + str(status.stderr))

I see "STATUS OK ? True" printed.

However this code fails to decrypt on a dockerized Linux environment on the same PC:

import gnupg
import os
key_data = '''-----BEGIN PGP PRIVATE KEY BLOCK-----

<key data here>
-----END PGP PRIVATE KEY BLOCK-----
'''
gpg_path = "/usr/bin/gpg":
gpg = gnupg.GPG(gpg_path)
gpg.encoding = 'utf-8'
import_result = gpg.import_keys(key_data)
with open(r"/home/test/my.csv.gpg", "rb") as f:
  status = gpg.decrypt_file(f, passphrase=None, output=r"/home/test/TEST.CSV")
  print("STATUS OK ? " + str(status.ok))
  print("STDERR: " + str(status.stderr))

I see "STATUS OK ? False" printed, and no other errors. The output file is not created. Both environments are running Python 3.7.9, and running pip show python-gnupg has the same output in both environments. I've made sure to copy over the encrypted file and have tried saving it with various encodings. The Linux environment is Debian via WSL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2025-01-20 20:55:39

事实证明,Debian 发行版中包含的 gpg 构建无法按预期工作。我从源代码安装了版本 2.2.27,并通过 gpgbinary 参数在 Python 中的 GPG 对象的构造函数中指定了可执行路径,然后我能够使用该脚本成功解密。我从 https://pythonhosted.org/python- 得到提示,Linux 发行版中的构建可能无法工作gnupg/ 在注释部分。

As it turns out, the build of gpg included in the Debian distro does not work as expected. I installed version 2.2.27 from source and specified the executable path in the constructor of the GPG object in Python via the gpgbinary argument, and I was then able to decrypt successfully using the script. I got the tip that builds that come in linux distros may not work from https://pythonhosted.org/python-gnupg/ in the Note section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文