- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试通过 POpen
的 python 程序远程控制 gpg。
我有一个包含加密数据的文件,我想对其进行解密、修改并重新加密后写回磁盘。
目前,我将解密后的信息存储在一个临时文件中(程序结束时我将其切碎
)。然后我对该文件进行修改,然后使用函数重新加密它,该函数通过 stdin
传输密码。
其代码如下:
def encrypt(source, dest, passphrase, cipher=None):
"""Encrypts the source file.
@param source Source file, that should be encrypted.
@param dest Destination file.
@param passphrase Passphrase to be used.
@param cipher Cipher to use. If None or empty string gpg's default cipher is
used.
"""
phraseecho = Popen(("echo", passphrase), stdout=subprocess.PIPE)
gpgargs = [
"gpg",
"-c",
"--passphrase-fd", "0", # read passphrase from stdin
"--output", dest,
"--batch",
"--force-mdc"]
if not cipher is None and len(cipher) > 0:
gpgargs.extend(("--cipher-algo", cipher))
gpgargs.append(source)
encrypter = Popen(
gpgargs,
stdin=phraseecho.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = encrypter.communicate()
rc = encrypter.returncode
if not rc == 0:
raise RuntimeError(
"Calling gpg failed with return code %d: %s" % (rc, stderr))
这非常有效,但我相当确定将潜在敏感的解密数据存储在临时文件中是一个相当大的安全漏洞。
所以我想以某种方式重写我的加密/解密函数,使它们能够完全在内存中工作,而不会将敏感数据存储在磁盘上。
解密通过 stdin
管道传输密码并捕获解密数据的 stdout
直接进行。
另一方面,加密让我发疯,因为我不能将密码和消息通过管道传输到“stdin”...至少
encrypter.stdin.write("%s\n%s" % (passphrase, message))
没用。
我的下一个最佳猜测是提供某种内存文件/管道/套接字或任何东西的文件描述符作为 --passphrase-fd
参数。问题是:我不知道是否存在诸如内存文件之类的东西,或者套接字是否适用,因为我从未使用过它们。
任何人都可以帮助我或指出我的问题的更好解决方案吗?
该解决方案不一定是可移植的 - 我完全可以使用仅 Linux 的方法。
提前致谢...
编辑:
非常感谢你们俩,Lars 和 ryran。两种解决方案都可以完美运行!可惜我只能接受一个
最佳答案
下面是我在 Obnam 中使用的代码运行 gpg,或许能对您有所帮助。
def _gpg_pipe(args, data, passphrase):
'''Pipe things through gpg.
With the right args, this can be either an encryption or a decryption
operation.
For safety, we give the passphrase to gpg via a file descriptor.
The argument list is modified to include the relevant options for that.
The data is fed to gpg via a temporary file, readable only by
the owner, to avoid congested pipes.
'''
# Open pipe for passphrase, and write it there. If passphrase is
# very long (more than 4 KiB by default), this might block. A better
# implementation would be to have a loop around select(2) to do pipe
# I/O when it can be done without blocking. Patches most welcome.
keypipe = os.pipe()
os.write(keypipe[1], passphrase + '\n')
os.close(keypipe[1])
# Actually run gpg.
argv = ['gpg', '--passphrase-fd', str(keypipe[0]), '-q', '--batch'] + args
tracing.trace('argv=%s', repr(argv))
p = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate(data)
os.close(keypipe[0])
# Return output data, or deal with errors.
if p.returncode: # pragma: no cover
raise obnamlib.Error(err)
return out
def encrypt_symmetric(cleartext, key):
'''Encrypt data with symmetric encryption.'''
return _gpg_pipe(['-c'], cleartext, key)
def decrypt_symmetric(encrypted, key):
'''Decrypt encrypted data with symmetric encryption.'''
return _gpg_pipe(['-d'], encrypted, key)
关于 python /POpen/gpg : Supply passphrase and encryption text both through stdin or file descriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367140/
(kubuntu,但在我的方法中试图保持平台独立。) 概念解释或必要步骤都值得赞赏。 我正在阅读文档,并试图获取 gpg-agent工作一次,但我感到气馁,因为每次尝试都需要重新启动,而且我并不真正理
记录遇到的问题; 在aliyun上安装MySQL时由于上次错误卸载mysql 导致校验文件出问题; 处理方式有几种 1到mysql官网下载校验文件 2跳过校验,记录第二种 编
我想为一个 atm 创建一个自动 GnuPG key 生成脚本,尽管他们运行 ubuntu,但不习惯使用 CLI。此外,其他人管理他们的计算机,使其保持最新状态并处于良好的运行状态,因此他们也没有 r
我刚刚不小心丢失了旧的 gpg key 。我想知道我是否可以删除每个提交的 gpg 标志或使用我的新 gpg key 重新签名? 最佳答案 我知道这是一个老问题,但我遇到了类似的情况,我不得不签署(实
作为 perl 脚本的一部分,我根据从电子邮件中获得的周期数从网站下载一些文件。我使用正则表达式找到合适的周期数,将其附加到 url 并从所述 url 获取加密文件。然后我将加密文件打印到临时文件,并
查看Subj:如何在不使用本地存储(在〜/.gpg下)的情况下从gpg中的私有(private)获取公钥? 此解决方案不满足要求: $ gpg --import priv.key $ gpg --ex
我有 1 个 github 用户和另一个 gitlab 用户,我为每个用户创建了 1 个 gpg key ,因为我的电子邮件地址不同。 问题是我必须执行 git config --global use
我在运行这段代码时遇到了这个错误。 gpg --fingerprint gpg: WARNING: unsafe ownership on configuration file `/home/dyla
我遵循本教程:https://help.github.com/articles/signing-commits-using-gpg/ 我尝试像这样提交时签名: https://github.com/s
我从 brew 安装了 GPG。 brew install gpg 它是 gnupg2-2.0.30_2。 当我提交时,我确实收到一条错误消息: You need a passphrase to un
在我添加 docker GPG key 时设置 docker 存储库 cmd- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | su
我全新安装了 Linux Mint 14。安装了 Thunderbird 和 Enigmail。 生成我的 key ,得到一个 friend 的公钥,导入它。 给我的 friend 发了一封加密的电子
我是 Unix 和 ksh 脚本编写的新手。我写了一个解密 gpg 消息的脚本。我收到此错误,我不知道如何解决。我希望有人可以查看我的脚本并帮助我弄清楚发生了什么。感谢您提供的任何帮助。 这是错误:
我正在尝试在我的 ubuntu 机器上安装 chirpstack。尝试设置 key 时出现以下错误。 jonny@jonny-ubuntu:~$ sudo gpg --keyserver keyser
我正在为 Web 开发设置新机器 (macOS Sierra),我已经完成 brew install gpg 安装了 gpg2 和 gpg-agent。我已经从旧 Mac 上的 ~.gnupg 复制了
我正在使用以下命令 gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 按照本文的指导https://getstream.io/blo
尝试将我们的基本镜像迁移到稳定的 Ubuntu 18.04,当我们尝试添加我们的 gpg key 时,出现此错误: root@77ff14f29cab:/# apt-key add apt-key.g
我运行的是GitHub工作流,过去我曾多次使用该工作流将代码发布到Nexus.它现在正在失败。我已经有一段时间没有使用它了,所以我的原始密钥可能已经过期了……自从我创建它以来,已经超过3年了,我不记得
我收到一个错误: gpg: no default secret key: No secret key gpg: [stdin]: clearsign failed: No secret key GPG
如何在 Scala 中解密 OpenPGP 加密文件?我有公钥和私钥,并且 gpg --output file.txt --decrypt file.txt.gpg 有效。 最佳答案 从 PR 到 h
我是一名优秀的程序员,十分优秀!