我在密钥环中维护了多个密钥,并且我经常想用密钥环中的每个密钥作为收件人来加密文件。如果我不想使用--recipient
或-r
选项单独指定每个收件人,那么实现此目的的最佳方法是什么?
答案1
我将从类似这样的操作开始,自动检测密钥环中的所有公钥:
[rsaw ~]$ gpg --list-keys --with-colons --fast-list-mode | awk -F: '/^pub/{printf "-r %s ", $5}'
-r 8364FDE4388D0935 -r 8F5B84CD292F9E32 -r EE3656DD5B0E2954 -r F576F163ABF75D93
这应该足够了——有很多选择。一种方式:创建一个包装器,使用它以及您传递给它的任何参数来运行 gpg,例如将以下内容添加到~/.bashrc
:
g(){ gpg $(gpg --list-keys --with-colons --fast-list-mode | awk -F: '/^pub/{printf "-r %s ", $5}') "$@"; }
运行示例:
[rsaw ~]$ g -aveo /tmp/issue.asc /etc/issue
gpg: using subkey BDE204E4 instead of primary key ABF75D93
gpg: using PGP trust model
gpg: This key belongs to us
gpg: using subkey E2E92BD7 instead of primary key 5B0E2954
gpg: This key belongs to us
gpg: using subkey 0EAE2434 instead of primary key 292F9E32
gpg: This key belongs to us
gpg: using subkey 9E0428AA instead of primary key 388D0935
gpg: This key belongs to us
gpg: reading from `/etc/issue'
gpg: writing to `/tmp/issue.asc'
gpg: RSA/AES256 encrypted for: "9E0428AA Ryan Sawhill <rsaw@devnull>"
gpg: RSA/AES256 encrypted for: "0EAE2434 Obama Mo <[email protected]>"
gpg: RSA/AES256 encrypted for: "E2E92BD7 Mitt Rom <rom@devnull>"
gpg: ELG/AES256 encrypted for: "BDE204E4 Bill C <clinton@nowhere>"
如果您始终加密文件并将输出保存到文件,那么这很好;否则,让您的脚本/函数仅运行原始检测命令可能会更简单 - 这将提供更大的灵活性,例如:
gpg $(g) -aveo </tmp/myfile