解密目录中的多个OpenPGP文件

use*_*498 19 encryption bash gnupg

我在目录中有几百个gpg加密文件,格式为filename.xyz.gpg,其中"xyz"是一些任意扩展名.我需要解密所有文件以生成filename.xyz解密,这样我就不必手动输入每个文件的密码.

我已尝试以下目录"测试":

for file in 'ls Testing'; do (echo <password>|gpg --passphrase-fd 0 -d $file 
--output     $file.decrypted);
Run Code Online (Sandbox Code Playgroud)

我结束了命令提示>,没有任何反应.

我的语法有什么问题?没有bash shell循环,有没有更有效的方法来做到这一点?

dog*_*ane 23

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

请尝试以下方法.您需要输入一次密码.

gpg --passphrase-fd 0 --decrypt-files *.gpg 
Run Code Online (Sandbox Code Playgroud)

  • 使用我当前版本的gpg,我只需要`gpg --decrypt-files*.gpg`\o / (3认同)

kon*_*box 15

正如手册中所说,您需要添加--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.
Run Code Online (Sandbox Code Playgroud)

您可以使用以下两种形式之一:

echo "passphrase" | gpg --passphrase-fd 0 --batch -d --output "decrypted.file" "file.gpg"
Run Code Online (Sandbox Code Playgroud)

或者更简单:

gpg --passphrase "passphrase" --batch -d --output "decrypted.file" "file.gpg"
Run Code Online (Sandbox Code Playgroud)

您可以尝试这样的脚本来提取文件:

#!/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
Run Code Online (Sandbox Code Playgroud)

  • 不确定将这样的密码短语放在命令行中是否很好,因为使用ps和类似工具,密码短语对系统上的所有用户都可见 (2认同)