bash 解密目录中的多个 OpenPGP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18769290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:11:16  来源:igfitidea点击:

decrypt multiple OpenPGP files in a directory

bashencryptiongnupg

提问by user1815498

I have several hundred gpg encrypted files in a directory, of the format filename.xyz.gpg where "xyz" is some arbitrary extension. I need to decrypt all of the files to generate filename.xyz decrypted in such a way that I don't have to manually enter the password for each file.

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

I have tried the following for directory "Testing":

我已经尝试了以下目录“测试”:

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

I just wind up with a command prompt >, and nothing happens.

我只是以命令提示符 > 结束,然后什么也没有发生。

What is the matter with my syntax? Is there some more efficient way to do this without a bash shell loop?

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

采纳答案by konsolebox

As it is said in the manual you need to add --batchoption:

正如手册中所说,您需要添加--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.

You can have either of these two forms:

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

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

Or simpler:

或者更简单:

gpg --passphrase "passphrase" --batch -d --output "decrypted.file" "file.gpg"

You can try a script like this to extract your files:

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

#!/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

回答by dogbane

gpgcan decrypt multiple files so you shouldn't need to write a loop.

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

Try the following. You will need to enter your password once.

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

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

回答by Gabe Kopley

I had success with

我成功了

gpg --decrypt-files *.gpg

cf. https://serverfault.com/a/388068/103585

参见 https://serverfault.com/a/388068/103585

回答by Ong Ming Soon

I had success with gpg --decrypt-files * but not *.gpg

我在 gpg --decrypt-files * 上取得了成功,但 *.gpg 没有成功

回答by Harika Boggavarapu

It worked with below commands for me:

它适用于我的以下命令:

For single file: gpg --decrypt --input C:\PGPFiles\[encryptedfilename.pgp] --passphrase [yourpassphrase]

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

For multiple files: gpg --decrypt --input C:\PGPFiles\* --passphrase [yourpassphrase]

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