简体   繁体   English

是否可以将消息和公钥从stdin传递到gpg / gpg2?

[英]Is it possible to pass both a message and a public key from stdin to gpg/gpg2?

If I have both a message (M) and a public key (P) in a process memory, what would be the way to encrypt M using P without writing either to a temporary intermediate file? 如果我在过程存储器中既有消息(M)又有公共密钥(P),那么用P加密M而不写入任何临时中间文件的方式是什么?

Any chance I can pass both into stdin and use some tricky protocol gpg(2) supports to accept both from pipe? 我是否有可能将两者都传递到stdin并使用一些棘手的协议gpg(2)支持从管道接受两者?

If no - what would be other the alternatives for php (keeping in mind there is no native binding and using the 3rd party extensions is not possible (it's not in the standard ubuntu repository and supporting a custom build and a custom repository is too costy))? 如果没有-php的其他替代方案是什么(请记住没有本机绑定,并且不可能使用第三者扩展名(它不在标准的ubuntu存储库中,并且支持自定义版本且自定义存储库过于昂贵)) )?

I don't believe such mechanism exists. 我认为这种机制不存在。 GnuPG expects to have the keyring as a file in order to perform a pubkey operation -- I don't believe it is able to accept actual public keys for a one-time encryption operation without them being in the keyring. GnuPG希望将密钥环作为文件来执行pubkey操作-我不认为它可以接受一次加密操作的实际公共密钥,而无需将其置于密钥环中。

You would first have to import that public key into a keyring, and then pass that keyring location to GnuPG -- in other words, you must be able to write it out somewhere. 首先,您必须将该公钥导入密钥环,然后将该密钥环位置传递给GnuPG-换句话说,您必须能够将其写出。 If you don't want to put them on disk, you can use /dev/shm , which is a ramdisk present on most Linux systems. 如果您不想将它们放在磁盘上,则可以使用/dev/shm ,这是大多数Linux系统上都存在的ramdisk。

To large parts, this is my answer from a similar question posted on Server Fault . 在很大程度上,这是我在Server Fault上发布类似问题的答案。 Replicated as cross-site duplicates are not possible. 无法复制,因为跨站点重复项是不可能的。

GnuPG requires all keys you want to use to be imported into a keyring. GnuPG要求将要使用的所有密钥导入密钥环。

If you don't want to import it to your normal keyring, either use another (temporary) keyring, or even a temporary GnuPG home directory (which will also bypass any configuration). 如果您不想将其导入到常规密钥环,请使用另一个(临时)密钥环,甚至使用临时GnuPG主目录(也将绕过任何配置)。 If you do not want to store the key on your hard disk, consider using a memdisk. 如果您不想将密钥存储在硬盘上,请考虑使用内存磁盘。

Temporary Keyring 临时钥匙圈

Set --primary-keyring temporary.gpg to use (and create if necessary) a temporary keyring as default. --primary-keyring temporary.gpg设置为默认使用(并根据需要创建)临时密钥环。 It will be created in your GnuPG home directory ( ~/.gnupg/temporary.gpg by default). 它将在您的GnuPG主目录中创建(默认情况下为~/.gnupg/temporary.gpg )。 Your normal keyring will still be available, but imports will go to the temporary one. 您的常规密钥环仍将可用,但导入将进入临时密钥环。 Delete it as you want to. 根据需要将其删除。

For example: 例如:

gpg --primary-keyring temporary.gpg --import key.asc
gpg --primary-keyring temporary.gpg --recipient 0xDEADBEEF --encrypt
rm ~/.gnupg/temporary.gpg # can be omitted, not loaded by default

Temporary GnuPG Home Directory 临时GnuPG主目录

This will also reset all configuration, and might be helpful for testing some stuff. 这还将重置所有配置,并且可能有助于测试某些内容。 Set --homedir [folder] or the environment variable $GNUPGHOME , import the key, perform any operations and then delete the folder as you wish to. 设置--homedir [folder]或环境变量$GNUPGHOME ,导入密钥,执行任何操作,然后根据需要删除文件夹。

For example: 例如:

export GNUPGHOME=/tmp/gnupg # Or apply --homedir on each invocation
gpg --import key.asc
gpg --recipient 0xDEADBEEF --encrypt
rm -r $GNUPGHOME # Can be omitted
unset $GNUPGHOME

GnuPG is very picky regarding permissions, you might need to apply stricter permissions to the $GNUPGHOME folder before being able to perform all operations. GnuPG在权限方面非常挑剔,您可能需要对$GNUPGHOME文件夹应用更严格的权限,然后才能执行所有操作。 Might very well be an option to keep some playground- $GNUPGHOME around. 可以选择保留一些游乐场- $GNUPGHOME

GnuPG for PHP in Ubuntu 适用于Ubuntu的PHP的GnuPG

There is an official PHP PEAR module for GnuPG, which is also packaged for Ubuntu in the official repositories , and I would strongly recommend for using this module instead of manually building an interface to GnuPG. 有一个用于GnuPG的官方PHP PEAR模块,该模块也在官方存储库中为Ubuntu打包了 ,我强烈建议您使用此模块,而不是手动构建与GnuPG的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM