在一个目录中解密多个OpenPGP文件

21

我有一个目录里面有几百个gpg加密文件,文件格式为 filename.xyz.gpg,其中“xyz”是任意扩展名。我需要解密所有文件以生成 filename.xyz 解密文件,而不必手动输入每个文件的密码。

对于目录“Testing”,我尝试了以下方法:

for file in 'ls Testing'; do (echo <password>|gpg --passphrase-fd 0 -d $file 
--output     $file.decrypted);
我只是最后停留在命令提示符,什么都没有发生。我的语法有问题吗?有没有更高效的方法可以在没有bash shell循环的情况下完成这个任务?

也许可以用gpg命令交换echo密码?在执行命令之前回显密码可以吗? - aso
5个回答

28

gpg可以解密多个文件,因此您不需要编写循环。

请尝试以下操作。您将需要输入密码一次。

gpg --passphrase-fd 0 --decrypt-files *.gpg 

谢谢,这确实让事情变得更容易了。我还找到了一些文档,建议在这种情况下使用--multifile,这样只需要输入一次密码。顺便说一句,我喜欢你的叶甲虫头像! - user1815498
1
谢谢。如果您感兴趣,这是一种狗毒藤叶甲 - dogbane
"--decrypt-files" 应该与 "--multifile --decrypt" 相同。 - Ben
3
我目前使用的 gpg 版本只需运行 gpg --decrypt-files *.gpg 即可解密文件。 - phunehehe

17

如手册所述,您需要添加--batch选项:

   --passphrase-fd n
          Read the passphrase from file descriptor n. Only the first line will be read from file descriptor n. If you use 0 for n, the passphrase will be read from
          STDIN. This can only be used if only one passphrase is supplied.  Note that this passphrase is only used if the option --batch has also been given.  This is
          different from gpg.

   --passphrase string
          Use string as the passphrase. This can only be used if only one passphrase is supplied. Obviously, this is of very questionable security on a multi-user sys‐
          tem. Don't use this option if you can avoid it.  Note that this passphrase is only used if the option --batch has also been given.  This is different from
          gpg.

您可以选择以下两种表单:

echo "passphrase" | gpg --passphrase-fd 0 --batch -d --output "decrypted.file" "file.gpg"

或者更简单:
gpg --passphrase "passphrase" --batch -d --output "decrypted.file" "file.gpg"

您可以尝试使用如下脚本来提取您的文件:
#!/bin/bash

read -rsp "Enter passphrase: " PASSPHRASE

for FILE in *.*.gpg; do
    echo "Extracting $FILE to ${FILE%.gpg}."
    echo "$PASSPHRASE" | gpg --passphrase-fd 0 --batch -d --output "${FILE%.gpg}" "$FILE"
done

3
在命令行中使用密码短语可能不是明智之举,因为这会使所有使用 ps 等工具的系统用户都能看到它。 - BenKennish

5

0

我使用 gpg --decrypt-files * 成功了,但不是 *.gpg


0

以下命令对我有效:

对于单个文件: gpg --decrypt --input C:\PGPFiles\[encryptedfilename.pgp] --passphrase [yourpassphrase]

对于多个文件: gpg --decrypt --input C:\PGPFiles\* --passphrase [yourpassphrase]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接